Skip to content

Commit

Permalink
Ticket unacms#715 - Search engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLV committed Jun 20, 2017
1 parent dc78f5f commit 56ac100
Show file tree
Hide file tree
Showing 34 changed files with 1,101 additions and 4 deletions.
3 changes: 3 additions & 0 deletions inc/classes/BxDol.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,8 @@ public function serviceGetThumb ($iContentId);
public function serviceGetInfo ($iContentId, $bSearchableFieldsOnly = true);
public function serviceGetSearchResultUnit ($iContentId, $sUnitTemplate = '');
public function serviceGetAll ($aParams = array());

public function serviceGetSearchableFieldsExtended();
public function serviceGetSearchResultExtended($aParams);
}
/** @} */
39 changes: 39 additions & 0 deletions inc/classes/BxDolCmts.php
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,45 @@ public function serviceGetAll ($aParams = array())
return $this->_oQuery->getCommentsBy($aParams);
}

public function serviceGetSearchableFieldsExtended()
{
$oForm = BxDolForm::getObjectInstance('sys_comment', 'sys_comment_post', $this->_oTemplate);
if(!$oForm)
return array();

$aSrchNamesExcept = array();
$aSrchCaptions = array(
'cmt_author_id' => '_sys_form_comment_input_caption_cmt_author_id',
'cmt_text' => '_sys_form_comment_input_caption_cmt_text'
);

$aResult = array(
'cmt_author_id' => array(
'type' => 'text_auto',
'caption' => $aSrchCaptions['cmt_author_id'],
'values' => ''
)
);

foreach ($oForm->aInputs as $aInput)
if (in_array($aInput['type'], BxDolSearchExtended::$SEARCHABLE_TYPES) && !in_array($aInput['name'], $aSrchNamesExcept))
$aResult[$aInput['name']] = array(
'type' => $aInput['type'],
'caption' => !empty($aInput['caption_src']) ? $aInput['caption_src'] : (!empty($aSrchCaptions[$aInput['name']]) ? $aSrchCaptions[$aInput['name']] : ''),
'values' => !empty($aInput['values_src']) ? $aInput['values_src'] : '',
);

return $aResult;
}

public function serviceGetSearchResultExtended($aParams)
{
if(empty($aParams) || !is_array($aParams))
return array();

return $this->_oQuery->getCommentsBy(array('type' => 'search_ids', 'search_params' => $aParams));
}

/**
* Internal functions
*/
Expand Down
30 changes: 30 additions & 0 deletions inc/classes/BxDolCmtsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,36 @@ function getCommentsBy($aParams = array())

break;

case 'search_ids':
$aMethod['name'] = 'getColumn';

$sSelectClause = "`{$this->_sTable}`.`cmt_id`";

$sWhereConditions = "1";
foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
$sSearchValue = "";
switch ($aSearchParam['operator']) {
case 'like':
$sSearchValue = " LIKE " . $this->escape("%" . $aSearchParam['value'] . "%");
break;

case 'in':
$sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
break;

default:
$sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
$aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
}

$sWhereConditions .= " AND `{$this->_sTable}`.`" . $sSearchParam . "`" . $sSearchValue;
}

$sWhereClause .= " AND (" . $sWhereConditions . ")";

$sOrderClause .= "`{$this->_sTable}`.`cmt_time` ASC";
break;

case 'all_ids':
$aMethod['name'] = 'getColumn';

Expand Down
10 changes: 10 additions & 0 deletions inc/classes/BxDolContentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ public function getAll ($aParams = array())
return $this->_call('get_all', $aParams);
}

public function getSearchableFieldsExtended ()
{
return $this->_call('get_searchable_fields_extended');
}

public function getSearchResultExtended ($aParams)
{
return $this->_call('get_search_result_extended', $aParams);
}

protected function _call($sMethod)
{
if(!BxDolRequest::serviceExists($this->_sSystem, $sMethod))
Expand Down
10 changes: 10 additions & 0 deletions inc/classes/BxDolContentInfoCmts.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public function getAll ($aParams = array())
{
return $this->_oCmts->serviceGetAll($aParams);
}

public function getSearchableFieldsExtended ()
{
return $this->_oCmts->serviceGetSearchableFieldsExtended();
}

public function getSearchResultExtended ($aParams)
{
return $this->_oCmts->serviceGetSearchResultExtended($aParams);
}
}

/** @} */
4 changes: 4 additions & 0 deletions inc/classes/BxDolFormQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ static public function getFormArray ($sObject, $sDisplayName)
$aInput = array (
'type' => $a['type'],
'name' => $a['name'],
'caption_src' => $a['caption'],
'caption' => _t($a['caption']),
'info_src' => $a['info'] ? $a['info'] : '',
'info' => $a['info'] ? _t($a['info']) : '',
'required' => $a['required'] ? true : false,
'unique' => $a['unique'] ? true : false,
Expand All @@ -118,6 +120,8 @@ static public function getFormArray ($sObject, $sDisplayName)
$aInput['value'] = isset(self::$TYPES_TRANSLATABLE[$aInput['type']]) ? _t($a['value']) : $a['value'];

if (!empty($a['values'])) {
$aInput['values_src'] = $a['values'];

if (0 === strncmp(BX_DATA_LISTS_KEY_PREFIX, $a['values'], 2)) {
$aInput['values_list_name'] = trim($a['values'], BX_DATA_LISTS_KEY_PREFIX . ' ');
$aInput['values'] = self::getDataItems(trim($a['values'], BX_DATA_LISTS_KEY_PREFIX . ' '), isset(self::$TYPES_SET[$aInput['type']]));
Expand Down
109 changes: 109 additions & 0 deletions inc/classes/BxDolSearchExtended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php defined('BX_DOL') or die('hack attempt');
/**
* Copyright (c) UNA, Inc - https://una.io
* MIT License - https://opensource.org/licenses/MIT
*
* @defgroup UnaCore UNA Core
* @{
*/

class BxDolSearchExtended extends BxDolFactory implements iBxDolFactoryObject
{
public static $SEARCHABLE_TYPES = array(
'text', 'textarea', 'number',
'datepicker', 'date_time', 'datetime',
'select', 'radio_set',
'checkbox', 'switcher'
);

public static $TYPE_TO_TYPE_SEARCH = array(
'text' => array('text'),
'textarea' => array('text'),
'number' => array('text'),
'text_auto' => array('text_auto'),
'select' => array('checkbox_set', 'select_multiple', 'select'),
'radio_set' => array('checkbox_set', 'select_multiple', 'select'),
'checkbox' => array('checkbox', 'switcher'),
'switcher' => array('checkbox', 'switcher'),
);

public static $TYPE_TO_OPERATOR = array(
'text' => array('like'),
'textarea' => array('like'),
'number' => array('='),
'text_auto' => array('in'),
'select' => array('in'),
'radio_set' => array('in'),
'checkbox' => array('='),
'switcher' => array('='),
);

protected $_oDb;

protected $_sObject;
protected $_aObject;


/**
* Constructor
* @param $aObject array of search options
*/
protected function __construct($aObject)
{
parent::__construct();

$this->_oDb = new BxDolSearchExtendedQuery($this->_aObject);

$this->_sObject = $aObject['object'];
$this->_aObject = $aObject;
}

/**
* Get editor object instance by object name
* @param $sObject object name
* @return object instance or false on error
*/
static public function getObjectInstance($sObject, $oTemplate = false)
{
if(isset($GLOBALS['bxDolClasses']['BxDolSearchExtended!' . $sObject]))
return $GLOBALS['bxDolClasses']['BxDolSearchExtended!' . $sObject];

$aObject = BxDolSearchExtendedQuery::getSearchObject($sObject);
if(!$aObject || !is_array($aObject))
return false;

$sClass = 'BxTemplSearchExtended';
if(!empty($aObject['class_name'])) {
$sClass = $aObject['class_name'];
if(!empty($aObject['class_file']))
require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
}

$o = new $sClass($aObject, $oTemplate);
return ($GLOBALS['bxDolClasses']['BxDolSearchExtended!' . $sObject] = $o);
}

static public function actionGetAuthors()
{
$aResult = BxDolService::call('system', 'profiles_search', array(bx_get('term')), 'TemplServiceProfiles');

header('Content-Type:text/javascript; charset=utf-8');
echo json_encode($aResult);
}

public function isEnabled()
{
return isset($this->_aObject['active']) && (int)$this->_aObject['active'] != 0;
}

public function reset()
{
if($this->_oDb->deleteFields(array('object' => $this->_sObject)) === false)
return false;

$this->_aObject['fields'] = BxDolSearchExtendedQuery::getSearchFields($this->_aObject);
return true;
}
}

/** @} */
94 changes: 94 additions & 0 deletions inc/classes/BxDolSearchExtendedQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php defined('BX_DOL') or die('hack attempt');
/**
* Copyright (c) UNA, Inc - https://una.io
* MIT License - https://opensource.org/licenses/MIT
*
* @defgroup UnaCore UNA Core
* @{
*/

class BxDolSearchExtendedQuery extends BxDolDb
{
protected static $TYPES_SET = array (
'select_multiple' => 1,
'checkbox_set' => 1,
);

public function __construct($aObject = array())
{
parent::__construct();
}

static public function getSearchObject($sObject)
{
$oDb = BxDolDb::getInstance();

$sQuery = $oDb->prepare("SELECT * FROM `sys_objects_search_extended` WHERE `object` = ?", $sObject);

$aObject = $oDb->getRow($sQuery);
if(!$aObject || !is_array($aObject))
return false;

$aObject['fields'] = self::getSearchFields($aObject);
return $aObject;
}

static public function getSearchFields($aObject)
{
$oDb = BxDolDb::getInstance();

$sQueryFields = "SELECT * FROM `sys_search_extended_fields` WHERE `object` = :object ORDER BY `order`";
$aQueryFieldsBindings = array('object' => $aObject['object']);
$aFields = $oDb->getAll($sQueryFields, $aQueryFieldsBindings);

//--- Get fields
if(empty($aFields) || !is_array($aFields)) {
$aFields = BxDolContentInfo::getObjectInstance($aObject['object_content_info'])->getSearchableFieldsExtended();
if(empty($aFields) || !is_array($aFields))
return array();

$iOrder = 0;
foreach($aFields as $sField => $aField)
$oDb->query("INSERT INTO `sys_search_extended_fields`(`object`, `name`, `type`, `caption`, `values`, `search_type`, `search_operator`, `active`, `order`) VALUES(:object, :name, :type, :caption, :values, :search_type, :search_operator, '1', :order)", array(
'object' => $aObject['object'],
'name' => $sField,
'type' => $aField['type'],
'caption' => $aField['caption'],
'values' => $aField['values'],
'search_type' => reset(BxDolSearchExtended::$TYPE_TO_TYPE_SEARCH[$aField['type']]),
'search_operator' => reset(BxDolSearchExtended::$TYPE_TO_OPERATOR[$aField['type']]),
'order' => $iOrder++
));

$aFields = $oDb->getAll($sQueryFields, $aQueryFieldsBindings);
}

//--- Process fields
foreach ($aFields as $iIndex => $aField) {
//--- Process field Values
if(!empty($aField['values'])) {
if(strncmp(BX_DATA_LISTS_KEY_PREFIX, $aField['values'], 2) === 0) {
$sList = trim($aField['values'], BX_DATA_LISTS_KEY_PREFIX . ' ');
$aFields[$iIndex] = array_merge($aField, array(
'values' => BxDolFormQuery::getDataItems($sList, isset(self::$TYPES_SET[$aField['search_type']])),
'values_list_name' => $sList,
));
}
else
$aFields[$iIndex]['values'] = unserialize($aField['values']);
}
}

return $aFields;
}

public function deleteFields($aWhere)
{
if(empty($aWhere))
return false;

return $this->query("DELETE FROM `sys_search_extended_fields` WHERE " . $this->arrayToSQL($aWhere, ' AND '));
}
}

/** @} */
Loading

0 comments on commit 56ac100

Please sign in to comment.