Example #1
0
 public function begin()
 {
     $termId = $this->getParams('term_id');
     $ordering = $this->getParams('ordering');
     $fetchChild = $this->getParams('fetch_child', false);
     $q = Posts::read()->where('`status`=:status AND `is_draft` = 0')->setParameter(':status', 'PUBLISH', \PDO::PARAM_STR);
     $term = Terms::retrieveById($termId);
     if (!$term) {
         return;
     }
     if ($fetchChild) {
         $child = $term->getDescendants();
         $ids = array($term->getId());
         foreach ($child as $_c) {
             $ids[] = $_c->getId();
         }
         $q->andWhere('`term_id` IN (' . implode(',', $ids) . ')');
     } else {
         $q->andWhere('`term_id`=:term_id')->setParameter(':term_id', $term->getId(), \PDO::PARAM_INT);
     }
     //limit
     $limit = $this->getParams('limit');
     if ($limit) {
         $q->setMaxResults((int) $limit);
     }
     if ($ordering) {
         foreach ($ordering as $_ordering) {
             $q->addOrderBy($_ordering['field'], $_ordering['order']);
         }
     } else {
         $q->addOrderBy('modified_time', 'DESC');
     }
     $this->list = $q->execute()->fetchAll(\PDO::FETCH_CLASS, 'Posts', array(null, false));
 }
Example #2
0
 /**
  * shortcut call \Flywheel\Router\WebRouter::createUrl() method
  * @see \Flywheel\Router\WebRouter::createUrl()
  * @param $route
  * @param array $params
  * @param bool $absolute
  * @param string $ampersand
  * @return mixed
  */
 public function createUrl($route, $params = array(), $ampersand = '&', $absolute = false)
 {
     $route = trim($route, '/');
     if ('post/detail' == $route) {
         if (isset($params['id']) && ($post = \Posts::retrieveById($params['id']))) {
             $params['slug'] = $post->getSlug();
         }
     } else {
         if ('category/default' == $route) {
             if (isset($params['id']) && ($term = \Terms::retrieveById($params['id']))) {
                 $params['slug'] = $term->getSlug();
             }
         } else {
             if ('events/default' == $route) {
                 if (isset($params['id']) && ($term = \Terms::retrieveById($params['id']))) {
                     $params['slug'] = $term->getSlug();
                 }
             } else {
                 if ('events/detail' == $route) {
                     if (isset($params['id']) && ($post = \Posts::retrieveById($params['id']))) {
                         $params['slug'] = $post->getSlug();
                     }
                 }
             }
         }
     }
     $params = Plugin::applyFilters('custom_router_param', $route, $params);
     if ($this->currentLang && sizeof($this->languages) > 1) {
         $params['lang'] = $this->currentLang->getLangCode();
     }
     return parent::createUrl($route, $params, $ampersand, $absolute);
 }
Example #3
0
 public function executeCategory()
 {
     $id = $this->get('id');
     if (!$id || !($term = \Terms::retrieveById($id))) {
         return $this->raise404(t('Product Category not found!'));
     }
     $cats = [$term->getId()];
     $descendants = (array) $term->getDescendants();
     foreach ($descendants as $d) {
         $cats[] = $d->getId();
     }
     //create query
     $q = \Items::select();
     if (sizeof($cats) > 1) {
         $q->where('`cat_id` = :cat_id')->setParameter(':cat_id', $term->getId(), \PDO::PARAM_INT);
     } else {
         $q->where('`cat_id` IN (' . implode(', ', $cats) . ')');
     }
     $q->andWhere('`status` = "ACTIVE"')->andWhere('`is_draft` = 0');
     //filter
     $price_from = floatval(str_replace(',', '.', str_replace('.', '', $this->get('price_from'))));
     $price_to = floatval(str_replace(',', '.', str_replace('.', '', $this->get('price_to'))));
     if ($price_from > $price_to) {
         $t = $price_from;
         $price_from = $price_to;
         $price_to = $t;
     }
     if ($price_from) {
         $q->andWhere('`regular_price` >= :pf OR `sale_price` >= :pf')->setParameter(':pf', $price_from, \PDO::PARAM_STR);
     }
     if ($price_to) {
         $q->andWhere('`regular_price` <= :pf OR `sale_price` <= :pf')->setParameter(':pf', $price_to, \PDO::PARAM_STR);
     }
     $ordering = $this->get('ordering');
     switch ($ordering) {
         case 'PRICE_DESCENDING':
             $q->orderBy('regular_price', 'DESC')->addOrderBy('sale_price', 'DESC');
             break;
         case 'PRICE_ASCENDING':
             $q->orderBy('regular_price', 'ASC')->addOrderBy('sale_price', 'ASC');
             break;
         default:
             $q->orderBy('public_time', 'DESC');
     }
     $cq = clone $q;
     $total = $cq->count('`id`')->execute();
     $page = abs($this->get('page', 'INT', 1));
     $items = $q->execute();
     $this->setView('Products/category');
     $this->view()->assign(['term' => $term, 'items' => $items, 'total' => $total, 'page' => $page, 'page_size' => $this->pageSize, 'price_form' => $price_from, 'price_to' => $price_to, 'ordering' => $ordering]);
     return $this->renderComponent();
 }
Example #4
0
 public function begin()
 {
     $terms = $this->getParams('terms');
     $ordering = $this->getParams('ordering');
     $fetchChild = $this->getParams('fetch_child', false);
     $limit = $this->getParams('limit');
     $q = \Items::select()->where('`is_draft` = 0 AND `status` = :status')->setParameter(':status', 'ACTIVE', \PDO::PARAM_STR);
     if (!empty($ordering)) {
         foreach ($ordering as $_o) {
             $q->addOrderBy(@$_o['field'], @$_o['order']);
         }
     } else {
         $q->orderBy('created_time', 'DESC');
     }
     if ($limit) {
         $q->setMaxResults((int) $limit);
     }
     if (is_array($terms) && !empty($terms)) {
         $t = $terms;
         if ($fetchChild) {
             foreach ($terms as $term_id) {
                 if ($term = \Terms::retrieveById($term_id)) {
                     $descendants = (array) $term->getDescendants();
                     foreach ($descendants as $d) {
                         $t[] = $d->getId();
                     }
                     unset($d);
                 }
             }
         }
         $terms = $t;
         $q->andWhere('`cat_id` IN (' . implode(', ', $terms) . ')');
     } elseif (is_scalar($terms) && is_numeric($terms)) {
         $t = [$terms];
         if ($term = \Terms::retrieveById($terms)) {
             if ($fetchChild) {
                 $descendants = (array) $term->getDescendants();
                 foreach ($descendants as $d) {
                     $t[] = $d->getId();
                 }
                 unset($d);
             }
         }
         if (sizeof($t) > 1) {
             $q->andWhere('`cat_id` IN (' . implode(', ', $t) . ')');
         } else {
             $q->andWhere('`cat_id` = :term_id')->setParameter(':term_id', $terms, \PDO::PARAM_INT);
         }
     }
     $this->items = $q->execute();
 }
Example #5
0
 public function executeDefault()
 {
     $keyword = $this->get('keyword');
     $cat_id = $this->get('cat_id');
     $is_hot = $this->get('is_hot');
     $promotion = $this->get('promotion');
     $page = $this->get('page', 'INT', 1);
     if ($this->request()->isXmlHttpRequest()) {
         $ajax = new \AjaxResponse();
         $q = \Items::read();
         if ($keyword) {
             $q->where('`name` LIKE :k OR `slug` LIKE :k')->setParameter(':k', "%{$keyword}%", \PDO::PARAM_STR);
         }
         if ($cat_id) {
             $q->andWhere('`cat_id` = :cat_id')->setParameter(':cat_id', $cat_id, \PDO::PARAM_INT);
         }
         if ($is_hot) {
             $q->andWhere('`pin` = 1');
         }
         if ($promotion) {
             $q->andWhere('`promotion` = 1');
         }
         $cq = clone $q;
         $total = $cq->count('`id`')->execute();
         $stmt = $q->setMaxResults(30)->setFirstResult(30 * ($page - 1))->execute();
         $result = [];
         while ($om = $stmt->fetchObject(\Items::getPhpName(), [null, false])) {
             /** @var \Items $om */
             $t = $om->toArray();
             $t['cat'] = \Terms::retrieveById($om->getCatId());
             if ($t['cat']) {
                 $t['cat'] = $t['cat']->toArray();
             }
             $t['thumb_url'] = $om->getMainImgThumb(96);
             $t['edit_link'] = $this->createUrl('items/edit', ['id' => $om->getId()]);
             $t['remove_link'] = $this->createUrl('items/remove', ['id' => $om->getId()]);
             $result[] = $t;
         }
         $ajax->type = \AjaxResponse::SUCCESS;
         $ajax->items = $result;
         $ajax->current_page = $page;
         $ajax->total_page = ceil($total / 30);
         return $this->renderText($ajax->toString());
     }
     $this->document()->addJsVar('items_list_url', $this->createUrl('items'));
     $this->setView('Items/list_items');
     $this->view()->assign(['keyword' => $keyword, 'cat_id' => $cat_id, 'is_hot' => $is_hot, 'promotion' => $promotion, 'page' => $page]);
     return $this->renderComponent();
 }
Example #6
0
 public function executeDetail()
 {
     if (!($post = \Posts::retrieveById($this->request()->get('id')))) {
         $this->raise404();
     }
     $post->setHits($post->getHits() + 1);
     $post->save(false);
     $term = \Terms::retrieveById($post->getTermId());
     if (($viewProp = $term->getProperty('post_view')) && $this->view()->checkViewFileExist($this->getTemplatePath() . '/Controller/Post/' . $this->_path . $viewProp->getPropertyValue())) {
         $this->setView('Post/' . $viewProp->getPropertyValue());
     } else {
         $this->setView('Post/default');
     }
     $this->view()->assign(array('post' => $post, 'term' => $term));
     return $this->renderComponent();
 }
Example #7
0
 public function beforeExecute()
 {
     parent::beforeExecute();
     // TODO: Change the autogenerated stub
     $document = $this->document();
     $document->title = \Setting::get('site_name');
     $this->_initTemplate();
     $this->_initLanguages();
     Plugin::addFilter('custom_router_param', function ($route, $params) {
         if ('products/category' == $route) {
             if (isset($params['id']) && ($term = \Terms::retrieveById($params['id']))) {
                 $params['slug'] = $term->getSlug();
             }
         }
         if ('products/detail' == $route) {
             if (isset($params['id']) && ($item = \Items::retrieveById($params['id']))) {
                 $params['slug'] = $item->getSlug();
             }
         }
         return $params;
     }, 1, 2);
 }
Example #8
0
 public function executeCustomField()
 {
     $term = \Terms::retrieveById($this->request()->get('id'));
     $this->setView('custom_fields');
     $session = Session::getInstance();
     if (!$term) {
         $session->setFlash('term_message', t('Term not found with' . $this->request()->get('id')));
         $this->redirect($this->createUrl('category', array('taxonomy' => $this->request()->get('taxonomy'))));
     }
     $error = array();
     $message = array();
     $input = new \TermCustomFields();
     if ($this->request()->isPostRequest()) {
         $input->hydrate($this->request()->post('custom_fields'));
         $input->setTermId($term->getId());
         if (!($acceptValues = $this->request()->post('accept_values'))) {
             $acceptValues = explode("\n", $acceptValues);
             $input->setAcceptValue(json_encode($acceptValues));
         }
         if ($input->save()) {
             $message = t('Save new custom fields success!');
         } else {
             if (!$input->isValid()) {
                 foreach ($input->getValidationFailures() as $validationFailures) {
                     $error[$validationFailures->getColumn()] = $validationFailures->getMessage();
                 }
             }
         }
     }
     $customFields = \TermCustomFields::findByTermId($term->getId());
     $this->setView('custom_fields');
     $this->view()->assign(array('error' => $error, 'message' => $message, 'term' => $term, 'input' => $input, 'custom_fields' => $customFields));
     return $this->renderComponent();
 }
Example #9
0
 public function executeLoadCustomFieldFrm()
 {
     $category_id = $this->request()->post('category_id');
     $category = \Terms::retrieveById($category_id);
     if (!$category) {
         //error
         return $this->renderText('');
     }
     $post_id = $this->request()->post('post_id');
     //load category custom fields
     $categoryCfs = \TermCustomFields::findByTermId($category_id);
     if (empty($categoryCfs)) {
         return $this->renderText('');
     }
     /** @var \PostCustomFields[] $postCfs */
     $postCfs = array();
     //load item custom field value if exist
     if ($post_id) {
         $_postCfs = \PostCustomFields::findByPostId($post_id);
         for ($i = 0, $size = sizeof($_postCfs); $i < $size; ++$i) {
             $postCfs[$_postCfs[$i]->getCfId()] = $_postCfs;
         }
         unset($_postCfs);
     }
     //end load item custom field value
     $data = array();
     foreach ($categoryCfs as $catCf) {
         $d = (object) $catCf->toArray();
         $d->value = '';
         if (isset($postCfs[$catCf->getId()])) {
             //exist items
             $i = $postCfs[$catCf->getId()];
             switch ($catCf->getFormat()) {
                 case 'NUMBER':
                     $d->value = (double) $i->getNumberValue();
                     break;
                 case 'BOOL':
                     $d->value = (bool) $i->getBoolValue();
                     break;
                 case 'DATETIME':
                     $d->value = $i->getDatetimeValue();
                     break;
                 default:
                     $d->value = $i->getTextValue();
             }
         }
         $data[] = $d;
     }
     $data = Plugin::applyFilters('custom_' . $category->getTaxonomy() . '_cf_form_data', $data);
     $buf = $this->renderPartial(array('data' => $data));
     $buf = Plugin::applyFilters('custom_' . $category->getTaxonomy() . '_cf_form', $buf);
     return $buf;
 }
Example #10
0
 protected function _columnCategory($item)
 {
     /** @var \Posts $item */
     $s = '';
     if ($item->getTermId()) {
         $category = \Terms::retrieveById($item->getTermId());
         if ($category) {
             $eLink = Factory::getRouter()->createUrl('category/edit', array('id' => $category->getId(), 'taxonomy' => $category->getTaxonomy()));
             $s .= '<a href="' . $eLink . '">' . $category->getName() . '</a>';
         }
     }
     return $s;
 }
Example #11
0
 public function executeDefault()
 {
     if (!($cat = \Terms::retrieveById($this->request()->get('id')))) {
         $this->raise404();
     }
     $viewProp = $cat->getProperty('cat_view');
     if ($viewProp) {
         $this->setView('Category/' . $viewProp->getPropertyValue());
     } else {
         $this->setView('Category/default');
     }
     $child = $cat->getDescendants();
     $q = \Posts::select()->where('`status` = :status AND `is_draft` = 0')->setParameter(':status', 'PUBLISH', \PDO::PARAM_STR);
     //Filter
     $catId = array($cat->getId());
     foreach ($child as $c) {
         $catId[] = $c->getId();
     }
     $q->andWhere('`term_id` IN (' . implode(',', $catId) . ')');
     //Filter by time
     $day = $this->request()->get('day');
     $month = $this->request()->get('month');
     $year = $this->request()->get('year');
     if ($day || $month || $year) {
         if ($day) {
             $q->andWhere('DAY(`created_time`) = :day')->setParameter(':day', $day, \PDO::PARAM_STR);
         }
         if ($month) {
             $q->andWhere('MONTH(`created_time`) = :month')->setParameter(':month', $month, \PDO::PARAM_STR);
         }
         if ($year) {
             $q->andWhere('YEAR(`created_time`) = :year')->setParameter(':year', $year, \PDO::PARAM_STR);
         }
     }
     //Keyword
     if ($keyword = $this->request()->get('keyword')) {
         $q->andWhere('`title` LIKE "%' . $keyword . '%"');
     }
     //Ordering
     $orderingProp = $cat->getProperty('post_ordering');
     if ($orderingProp) {
         switch ($orderingProp->getPropertyValue()) {
             case 'created_time':
                 $q->addOrderBy('created_time');
                 break;
             case 'ordering':
                 $q->addOrderBy('ordering');
                 break;
             case 'modified_time':
             case 'default':
             default:
                 $q->addOrderBy('created_time', 'DESC');
                 break;
         }
     } else {
         $q->addOrderBy('created_time', 'DESC');
     }
     $qCount = clone $q;
     $total = $qCount->count()->execute();
     //Paging
     $pageSizeProp = $cat->getProperty('page_size');
     if ($pageSizeProp) {
         $page_size = $cat->getProperty('page_size')->getPropertyValue();
         if (-1 != $pageSizeProp->getPropertyValue()) {
             $q->setMaxResults($pageSizeProp->getPropertyValue());
             $page = $this->request()->get('page', 'INT', 1);
             $q->setFirstResult(($page - 1) * $pageSizeProp->getPropertyValue());
         }
     } else {
         $page_size = 25;
         $q->setMaxResults(25);
         $page = $this->request()->get('page', 'INT', 1);
         $q->setFirstResult(($page - 1) * $page_size);
     }
     $posts = $q->execute();
     $this->view()->assign(array('page_size' => $page_size, 'total' => $total, 'cat' => $cat, 'child' => $child, 'posts' => $posts));
     return $this->renderComponent();
 }
Example #12
0
 /**
  * Get Cat
  * @return bool|Terms
  */
 public function getCat()
 {
     if ($this->getCatId()) {
         return \Terms::retrieveById($this->getCatId());
     }
     return false;
 }
Example #13
0
 /**
  * @param \Menus $menu
  * @param array $lists
  * @param $deep
  */
 protected function _getItems($menu, &$lists, $deep)
 {
     $deep--;
     /** @var \Menus[] $child */
     $child = $menu->getChildren();
     if ($menu->getType() == \Menus::INTERNAL) {
         $param = json_decode($menu->getRouteParam());
     }
     if (!$child && $deep > 0 && $menu->getType() == \Menus::INTERNAL && $menu->getRoute() == 'category/default' && @$param->fetch_child) {
         if (@$param->id) {
             $cat = Terms::retrieveById(@$param->id);
             if ($cat) {
                 $childCat = $cat->getChildren();
                 foreach ($childCat as $cc) {
                     $c = new \Menus();
                     $c->setType(\Menus::INTERNAL);
                     $c->setName($cc->getName());
                     $c->setRoute('category/default');
                     $c->setRouteParam('{"id":' . $cc->getId() . '}');
                     if (!is_array($child)) {
                         $child = array();
                     }
                     $child[] = $c;
                 }
             } else {
                 return;
             }
         }
     }
     if (!$child && $deep > 0 && $menu->getType() == \Menus::INTERNAL && $menu->getRoute() == 'products/category' && @$param->fetch_child) {
         if (@$param->id) {
             $cat = Terms::retrieveById(@$param->id);
             if ($cat) {
                 $childCat = $cat->getChildren();
                 foreach ($childCat as $cc) {
                     $c = new \Menus();
                     $c->setType(\Menus::INTERNAL);
                     $c->setName($cc->getName());
                     $c->setRoute('products/category');
                     $c->setRouteParam('{"id":' . $cc->getId() . '}');
                     if (!is_array($child)) {
                         $child = array();
                     }
                     $child[] = $c;
                 }
             } else {
                 return;
             }
         }
     }
     if (!$child || $deep < 0) {
         return;
     }
     if (isset($lists['items'])) {
         $lists['items'] = array();
     }
     foreach ($child as $c) {
         if ($c->getType() == \Menus::SEPARATE) {
             $url = array('#');
         } else {
             if ($c->getType() == \Menus::EXTERNAL) {
                 $url = array($c->getLink());
             } else {
                 if ($c->getType() == \Menus::INTERNAL) {
                     $param = $c->getRouteParam() ? json_decode($c->getRouteParam(), true) : array();
                     $url = array($c->getRoute());
                     foreach ($param as $k => $v) {
                         $url[$k] = $v;
                     }
                 }
             }
         }
         $_item = array('label' => $c->getName(), 'url' => $url);
         $this->_getItems($c, $_item, $deep);
         $lists['items'][] = $_item;
     }
 }