Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1.     <!-- Подбор объекта -->
  2.     <?php echo $form->typeAheadRow($model, 'object_id', array(
  3.         'source' => 'js:function (query, process) {
  4.                 clearTimeout(typeaheadtimer);
  5.                 if (query.length >= 2) {
  6.                     typeaheadtimer = setTimeout(function(){
  7.                         $.ajax({
  8.                             url: "/obj/typeahead",
  9.                             type: "POST",
  10.                             dataType: "JSON",
  11.                             data: "query=" + query,
  12.                             success: function(response) {
  13.                                      var data = new Array();
  14.                                       $.each(response, function(i, title){
  15.                                         data.push(title);
  16.                                       })
  17.                                       return process(data);
  18.                             }
  19.                         });
  20.                     },300);
  21.                 }
  22.             }',
  23.         'items' => 10,
  24.         'matcher' => "js:function(item) {
  25.                        return ~0;
  26.                    }",
  27.         'updater' => 'js:function(item) {
  28.             var parts = item.split("(#");
  29.             var object_id = parseInt(parts[1]);
  30.             $("#request___create_form_object_id").val(object_id);
  31.             return item;
  32.          }'
  33.     ), array(
  34.        
  35.         'name' => 'object_typeahead',
  36.         'placeholder' => 'Название объекта (ID, адрес)...',
  37.         'value' => isset($model->object) ? $model->object->getName() : ""
  38.     )); ?>
  39.  
  40.  
  41.  
  42.  
  43.  
  44. // SERVER
  45. /**
  46.      * Автоподбор объектов для плагина Typeahead
  47.      */
  48.     public function actionTypeahead()
  49.     {
  50.         //Поисковой запрос
  51.         $query = $_POST['query'];
  52.  
  53.         $data = array();
  54.         //Если это ИД объекта - просто отдадим сам объект
  55.         // TODO: is_number(numeric)
  56.         if ((int)$query > 0 && $query == (int)$query) {
  57.             $data[] = Object::model()->findByPk($query)->getName();
  58.         }
  59.         else {
  60.             // Разобъем строку на массив
  61.             $query = str_replace(",", " ", $query);
  62.             $search = explode(" ", $query);
  63.             $criteria = new CDbCriteria();
  64.             $criteria->with = 'city';
  65.             if (count($search)) {
  66.                 foreach ($search as $word) {
  67.                     if (strlen($word) > 3) {
  68.                         $criteria->addCondition("concat_ws(' ',city.title,t.address,t.title) ilike '%{$word}%'");
  69.                     }
  70.                 }
  71.             }
  72.             $criteria->limit = 50;
  73.             $objects = Object::model()->findAll($criteria);
  74.  
  75.             if (count($objects)) {
  76.                 foreach ($objects as $object) {
  77.                     $data[] = $object->getName();
  78.                 }
  79.             }
  80.         }
  81.         if (!count($data)) {
  82.             $data[] = "Ничего не найдено (#0)";
  83.         }
  84.         echo CJSON::encode($data);
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement