示例#1
0
 /**
  * Make search
  */
 public function make_search($key)
 {
     $id = false;
     $psearchs = $this->_controller->get_context()->get_search_handle();
     $psearch_results = $this->_controller->get_context()->get_search_result_handle();
     $core = core::get_instance();
     $site_id = $this->_controller->get_context()->get_current_site_id();
     // check key exists
     if ($search_item = $psearchs->get_by_key($key, $site_id)) {
         $id = $search_item->id;
         $this->_found = $search_item->c_count;
     }
     // if too old, clean search results, make it new
     if ($search_item) {
         if ($search_item->time + $this->_expire_time < time()) {
             // clear
             $search_item->remove();
             $search_item = null;
         }
     }
     if (!$search_item) {
         $this->_found = 0;
         $cdata = core::module('sat')->get_node_handle();
         $orig_key = $key;
         # remove this for non test
         # $key = strings::convert_from($key);
         $key = core::lib('db')->escape(strings::strtolower($key));
         $cdata->set_where("site_id = {$site_id} AND active AND LCASE(title) LIKE '%{$key}%'")->load();
         $result = array();
         $ctype = $core->get_ctype('sat.node')->get_id();
         $this->_found = $cdata->count();
         if (!$cdata->is_empty()) {
             foreach ($cdata as $item) {
                 $result[] = array('title' => $item->title, 'description' => strip_tags($item->description), 'time' => $item->updated_at, 'url' => $item->get_url(), 'ctype' => $ctype, 'post_id' => $item->id);
             }
         }
         // create search history item
         $id = $psearchs->create(array('uid' => $this->_controller->get_user()->id, 'keyword' => $key, 'c_count' => $this->_found, 'site_id' => $site_id));
         // fill results
         foreach ($result as $v) {
             $v['pid'] = $id;
             $psearch_results->create($v);
         }
     }
     return $id;
 }
示例#2
0
 /**
  * поиск элемента по значению свойства
  * @param string property name
  * @param mixed val
  * @param bool no_case |casesensitive, default true
  * @return abs_collection_item or false
  */
 function get_item_by_prop($name, $val = null, $nc = false)
 {
     $tmp = false;
     // no items?
     if (empty($this->items)) {
         return $tmp;
     }
     if (is_array($name)) {
         return $this->get_item_by_prop_a($name);
     }
     if ($nc) {
         $val = strings::strtolower($val);
     }
     // get cache
     if ('id' == $name && null !== ($k = $this->_cache_id($val)) && isset($this->items[$k])) {
         return $this->items[$k];
     }
     foreach ($this->items as $k => $v) {
         if ($v->isset_data($name)) {
             $_val = $v->get_data($name);
             $_val = $nc ? strings::strtolower($_val) : $_val;
             if ($_val == $val) {
                 $tmp = $this->items[$k];
                 break;
             }
         }
     }
     // set cache
     if ('id' == $name) {
         $this->_cache_id($val, $k);
     }
     return $tmp;
 }
示例#3
0
 /**
  * Format all fields
  * Calls to container method
  * @param array data
  * @param string type {@see format_field}
  * @throws tf_exception
  */
 public function format_fields(&$data, $type = false)
 {
     if (!$type) {
         throw new collection_exception('Undefined format type');
     }
     if (!is_array($data)) {
         core::dprint_r($data);
         throw new collection_exception('Data wrong type: ' . gettype($data));
     }
     $container = $this->get_container();
     $vfs_keys = $this->get_fields_keys();
     foreach ($vfs_keys as $k) {
         if (!isset($data[$k])) {
             $data[$k] = '';
         }
         $v =& $data[$k];
         $current = $this->get_data($k);
         /* 
             Fields modificators:
               + autosave
               + default 
               + make_seo
         */
         $skip = false;
         if ('modify' == $type) {
             if (!$this->in_working_set($k)) {
                 $skip = true;
             } else {
                 if ($vf = $this->field($k)) {
                     // make seo
                     if (isset($vf['make_seo']) && !empty($vf['make_seo'])) {
                         $with_translit = false;
                         $with_strict = false;
                         if (is_array($vf['make_seo'])) {
                             if (isset($vf['make_seo']['key'])) {
                                 // new syntax: key, translit, strict (without spaces)
                                 $with_strict = @$vf['make_seo']['strict'];
                                 $with_translit = @$vf['make_seo']['translit'];
                                 $vf['make_seo'] = $vf['make_seo']['key'];
                             } else {
                                 $with_translit = $vf['make_seo'][1];
                                 $vf['make_seo'] = $vf['make_seo'][0];
                             }
                             // fill url, if empty
                             if (empty($v) && $vf['make_seo'] && isset($data[$vf['make_seo']])) {
                                 $v = $data[$vf['make_seo']];
                             }
                         } else {
                             // oldstyle: make_seo = 1|true switch
                             $with_translit = true;
                         }
                         if ($with_translit && !empty($v)) {
                             $v = functions::translit($v);
                         }
                         // something bad trigger this code
                         if (empty($v)) {
                             $v = uniqid();
                         } else {
                             $v = preg_replace('/[^а-яёa-z\\-\\_0-9[:space:]]/ui', '', $v);
                             if ($with_strict) {
                                 $v = preg_replace('/\\s{1,}/u', '-', $v);
                             }
                             $v = strings::strtolower(trim($v));
                             // @todo fix name spaces
                             if (($this->is_new() || empty($v)) && !empty($data[$vf['make_seo']])) {
                                 $v = $this->_unique_alias($k, @$vf['space'], $data);
                             }
                         }
                     }
                     // autosave
                     if (isset($vf['autosave']) && $vf['autosave'] && !$this->is_new()) {
                         $data[$k] = $this->get_data($k);
                     }
                     // default
                     if (isset($vf['default']) && $this->is_new() && empty($data[$k])) {
                         $data[$k] = $vf['default'];
                         // for unixtime 'now' set, parsed in collection::on_modify to time()
                     }
                 }
             }
         }
         if (!$skip) {
             $v = $container->format_field($k, $v, $type, $current, $this);
         }
     }
 }