public function userlistAction()
 {
     $response = new ApiResponse();
     if ($this->request->isGet()) {
         $limit = $this->request->get('limit');
         $page = $this->request->get('page');
         $users_id = $this->request->get('users_id');
         $offers = Offers::find("users_id = '{$users_id}'");
         if (count($offers) == 0) {
             $response->setResponseError('No offers found!');
             return $response;
         } else {
             $paginator = new PaginatorModel(array("data" => $offers, "limit" => $limit, "page" => $page));
             $page = $paginator->getPaginate();
             $response->setResponse($page->items, count($offers));
             return $response;
         }
     } else {
         $response->setResponseError('Wrong HTTP Method');
     }
     return $response;
 }
Ejemplo n.º 2
0
 public function edit($id)
 {
     $offers = Offers::find($id);
     return View::make('admin.pages.offer.edit')->with(['offers' => $offers]);
 }
Ejemplo n.º 3
0
 public function indexAction()
 {
     if ($this->request->hasPost('up')) {
         $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
     }
     if ($this->request->hasPost('cat_id') && $this->request->isAjax()) {
         $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
         $cat_id = $this->request->getPost('cat_id');
     }
     if (!empty($_POST['page'])) {
         $currentPage = $_POST["page"];
     } else {
         $currentPage = 1;
     }
     if ($this->modelsCache->exists('filter-' . $this->session->get('user_id'))) {
         $filter = $this->modelsCache->get('filter-' . $this->session->get('user_id'));
         //$this->elements->var_print($filter);
         $aOff = $aDann = $aUser = $aTabs = array();
         // Следующая строка будет добавляться, видимо, всегда -- мы всегда будем искать в конкретной категории
         $aOff[] = "o.category_id = {$filter['cat_id']}";
         $rq_type = 0;
         $rq_type += isset($filter['price-from']) && is_numeric($filter['price-from']) && $filter['price-from'] >= 0 ? 1 : 0;
         $rq_type += isset($filter['price-to']) && is_numeric($filter['price-to']) && $filter['price-to'] >= 0 ? 2 : 0;
         $price_id = 5;
         switch ($rq_type) {
             case 0:
                 // Ничего не добавляет, пусто
                 break;
             case 1:
                 $aDann[] = "do.field_type_id = {$price_id} and do.dann >= {$filter['price-from']}";
                 break;
             case 2:
                 $aDann[] = "do.field_type_id = {$price_id} and do.dann <= {$filter['price-to']}";
                 break;
             case 3:
                 $aDann[] = "do.field_type_id = {$price_id} and do.dann between {$filter['price-from']} and {$filter['price-to']}";
                 break;
         }
         // 4 как код города мы здесь задаём вручную -- потом это можно будет поменять, если нужно
         if (isset($filter['city']) && strlen(trim($filter['city'])) > 0) {
             $aDann[] = "do.field_type_id = 4 and do.dann like '%{$filter['city']}%'";
         }
         // ICH -- все обработки полей вставляем здесь
         $aFromTo = array();
         $rRes = $rRes1 = array();
         foreach ($filter as $key => $val) {
             if (preg_match('/^(to|from)fiel-(\\d+)$/', $key, $rRes)) {
                 $aFromTo[$rRes[2]][$rRes[1]] = $val;
             } elseif ($key == $val) {
                 if (preg_match('/^fiel-(\\d+)$/', $key, $rRes1)) {
                     $aDann[] = "do.field_type_id = {$rRes1[1]} and do.dann = 'y'";
                 } else {
                     switch ($key) {
                         case 'tel':
                             $aUser[] = 'u.phone is not null';
                             break;
                         case 'image':
                             $aOff[] = 'o.image is not null';
                             break;
                         case 'trof':
                             break;
                         default:
                             // Какая-то непредусмотренная фигня
                             break;
                     }
                 }
                 // $rRes1[1] содержит интересующее нас число
             }
         }
         // $this->elements->var_print($aFromTo);
         foreach ($aFromTo as $key => $val) {
             $rq_type = 0;
             $rq_type += isset($filter["fromfiel-{$key}"]) && is_numeric($filter["fromfiel-{$key}"]) && $filter["fromfiel-{$key}"] >= 0 ? 1 : 0;
             $rq_type += isset($filter["tofiel-{$key}"]) && is_numeric($filter["tofiel-{$key}"]) && $filter["tofiel-{$key}"] >= 0 ? 2 : 0;
             switch ($rq_type) {
                 case 0:
                     // Ничего не добавляет, пусто
                     break;
                 case 1:
                     $aDann[] = "do.field_type_id = {$key} and DannOffers.dann >= " . $filter["fromfiel-{$key}"];
                     break;
                 case 2:
                     $aDann[] = "do.field_type_id = {$key} and do.dann <= " . $filter["tofiel-{$key}"];
                     break;
                 case 3:
                     $aDann[] = "do.field_type_id = {$key} and do.dann between " . $filter["fromfiel-{$key}"] . ' and ' . $filter["tofiel-{$key}"];
                     break;
             }
             // switch
         }
         // foreach
         // Тут начинается сборка запроса на основании подготовленных массивов
         $nDann = count($aDann);
         $hasDann = $nDann > 0;
         $hasUser = count($aUser) > 0;
         $aTabs[] = 'Offers o';
         if ($hasDann) {
             $aTabs[] = 'DannOffers do';
             $aOff[] = 'o.id = do.offers_id';
             $rqDann = $this->ic->fsis($aDann, NULL, '(%s)', "\n  or ", "\n and (%s)");
         }
         if ($hasUser) {
             $aTabs[] = 'User u';
             $aOff[] = 'o.user_id = u.id';
         }
         $rqFrom = implode(', ', $aTabs);
         //$rqDann = implode(' or ', $aDann);
         $rqSql = "select o.id from {$rqFrom}\n where ";
         $rqSql .= implode("\n and ", $aOff);
         if ($hasUser) {
             $rqSql .= "\n and " . implode(' and ', $aUser);
         }
         //$rqSql .= "\n and ($rqDann)\n";
         if ($hasDann) {
             $rqSql .= $rqDann;
         }
         // Здесь мы пройдёмся по юзерсу
         if ($hasDann) {
             $rqSql .= "\ngroup by o.id\nhaving count(do.id) = {$nDann}";
         }
         // limit можно добавить здесь, когда придёт время
         //        $this->elements->var_print(nl2br($rqSql));
         //  $this->elements->var_print($rqSql);
         ////// Начало SQL запроса
         $query = $this->modelsManager->createQuery($rqSql)->execute()->toArray();
         $a0 = array();
         foreach ($query as $val) {
             $a0[] = $val['id'];
         }
         $inClause = count($a0) > 0 ? 'id in (' . implode(', ', $a0) . ')' : '';
         //////  Конец SQL запроса дальше выборка.
         $paginator = new Phalcon\Paginator\Adapter\Model(array("data" => Offers::find(array("{$inClause}", 'order' => 'creation_date DESC')), "limit" => 10, "page" => $currentPage));
         $page = $paginator->getPaginate();
         foreach ($page->items as $offers) {
             if (isset($offers->image)) {
                 $im = 1;
             } else {
                 $im = 0;
             }
             $off[$offers->id]['name'] = array($offers->name, $im, $offers->status, $offers->user->phone, $offers->categories->name);
             foreach ($offers->dannoffers as $dan) {
                 $off[$offers->id][$dan->fieldtype->id] = $dan->dann;
             }
         }
         if (strlen($inClause) == 0) {
             $this->flash->error("Ничего не найдено фильтр сброшен");
             $this->modelsCache->delete('filter-' . $this->session->get('user_id'));
         }
         if ($currentPage <= 1) {
             $this->modelsCache->delete('filter-' . $this->session->get('user_id'));
         }
     } else {
         $zap = 0;
         if (isset($cat_id) && is_numeric($cat_id)) {
             if ($this->request->getPost('sub_cat') == 'y') {
                 $zap = "id = {$cat_id}";
             } else {
                 $zap = "id = {$cat_id} or id_sub = {$cat_id}";
             }
             $c_cat = 1;
             foreach (Categories::find(array("{$zap}")) as $csv) {
                 $c_cat = $c_cat + $csv->offers->count();
             }
             if ($c_cat == 1) {
                 $this->view->disable();
                 echo 1;
                 die;
             }
         }
         ///////////////////// Пагинация
         $this->cache->save('zap', $zap);
         if (empty($zap)) {
             $zaps = '';
         } else {
             $query = $this->modelsManager->createQuery("SELECT id FROM Categories WHERE " . $zap)->execute()->toArray();
             foreach ($query as $f) {
                 $cats_id[] = $f['id'];
             }
             // $this->elements->var_print($hj);
             $sre = implode(" ,", $cats_id);
             $zaps = " category_id IN ({$sre})";
         }
         $bield = $this->modelsManager->createBuilder()->from('Offers')->where($zaps)->orderBy('creation_date DESC');
         $paginator = new Phalcon\Paginator\Adapter\QueryBuilder(array("builder" => $bield, "limit" => 10, "page" => $currentPage));
         $page = $paginator->getPaginate();
         foreach ($page->items as $offers) {
             if (isset($offers->image)) {
                 $im = 1;
             } else {
                 $im = 0;
             }
             // Выдавать в таком виде результат
             $off[$offers->id]['name'] = array($offers->name, $im, $offers->status, $offers->user->phone, $offers->categories->name);
             foreach ($offers->dannoffers as $dan) {
                 $off[$offers->id][$dan->fieldtype->id] = $dan->dann;
             }
         }
     }
     $this->view->setVars(array("off" => $off = isset($off) ? $off : false, 'page_num' => $page->current, 'page_total' => $page->total_pages));
 }