Skip to content

Instantly share code, notes, and snippets.

@shabarin
Created July 20, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shabarin/dfe1918f90975dda9eeb11f5b6b00453 to your computer and use it in GitHub Desktop.
Save shabarin/dfe1918f90975dda9eeb11f5b6b00453 to your computer and use it in GitHub Desktop.
umi selector
<?php
class catalog_custom extends def_module
{
public function mysearch()
{
$searchStr = getRequest('search_str');
$pages = new selector_custom('pages');
$pages->types('hierarchy-type')->name('catalog', 'object');
$pages->where('description')->cslike('%'.$searchStr.'%');
$pages->where('photo')->regexp('jpg$');
$items = [];
// die ($pages->query());
foreach ($pages->result() as $page)
{
$items[] = [
'@id' => $page->getId(),
'@name' => $page->getName(),
'@photo' => $page->getValue('photo'),
'node:value' => $page->getValue('description'),
];
}
return def_module::parseTemplate(null, [
'subnodes:items' => $items,
]);
}
}
class selector_custom extends selector
{
protected function createSelectorWhereSysProp($fieldName)
{
return new selectorWhereSysProp_custom($fieldName);
}
protected function createSelectorWhereFieldProp(array $fieldIdList, $searchInRelatedObject = true)
{
return new selectorWhereFieldProp_custom($fieldIdList, $searchInRelatedObject);
}
protected function executor()
{
if (!$this->executor) {
$this->executor = new selectorExecutor_custom($this);
}
return $this->executor;
}
}
class selectorWhereSysProp_custom extends selectorWhereSysProp
{
public function __construct($name)
{
parent::__construct($name);
if (!in_array('cslike', $this->modes)) {
$this->modes[] = 'cslike';
}
if (!in_array('regexp', $this->modes)) {
$this->modes[] = 'regexp';
}
}
}
class selectorWhereFieldProp_custom extends selectorWhereFieldProp
{
public function __construct(array $fieldIdList, $searchInRelatedObject = true)
{
parent::__construct($fieldIdList, $searchInRelatedObject);
if (!in_array('cslike', $this->modes)) {
$this->modes[] = 'cslike';
}
if (!in_array('regexp', $this->modes)) {
$this->modes[] = 'regexp';
}
}
}
class selectorExecutor_custom extends selectorExecutor
{
protected function parseValue($mode, $value, $column = false, $fieldId = false)
{
switch ($mode) {
case "cslike":
return ' LIKE ' . $this->escapeValue($value) . ' COLLATE utf8_bin';
case "regexp":
return ' REGEXP BINARY ' . $this->escapeValue($value);
default:
return parent::parseValue($mode, $value, $column, $fieldId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment