Пример #1
0
 public function fetch($query, $mixed = null)
 {
     // default options
     $class = false;
     $justFirst = false;
     $params = array();
     // unravel options
     if (is_array($mixed)) {
         if (is_int(key($mixed))) {
             $params = $mixed;
         } else {
             $class = Options::one($mixed, 'class', false);
             $justFirst = $mixed->get('single', $mixed->get('first', false));
             $params = $mixed->get('params', array());
         }
     } else {
         if (is_bool($mixed)) {
             $justFirst = $mixed;
         } else {
             if (is_string($mixed)) {
                 $class = $mixed;
             }
         }
     }
     // build SQL
     if (is_array($query)) {
         $query = $this->buildSQL($query);
     }
     // apply params
     if ($params) {
         $query = $this->replaceholders($query, $params);
     }
     $result = $this->result($query);
     Vendors::class_exists($class) or $class = false;
     if ($justFirst) {
         if ($class) {
             return $result->nextObject($class, array(true));
         }
         return $result->nextAssocArray();
     }
     if ($class) {
         return $result->allObjects($class, array(true));
     }
     return $result->allAssocArrays();
 }
Пример #2
0
 public function index()
 {
     $postsPerPage = $this->_config('posts_on_index');
     // Way 1
     // Define which get method to use to fetch Posts by checking ACL
     // Use that function and the Model's logic to get those posts.
     $unpub = $this->user->hasAccess('blog read unpublished');
     $method = $unpub ? 'newest' : 'newestPublished';
     $posts = models\Post::$method($postsPerPage);
     // The quick 'n dirty //
     $page = Options::one($_GET, 'page', 1, false);
     $start = ($page - 1) * $postsPerPage;
     $conditions = $unpub ? '1' : 'is_published = 1';
     $posts = models\Post::all($conditions . ' ORDER BY created_on DESC LIMIT ' . $start . ', ' . $postsPerPage . '');
     // Don't do it! //
     // Way 2
     // Define the difference in conditions here (instead of in the Model)
     $conditions = $unpub ? '' : array('is_published' => true);
     $numAllPosts = models\Post::count($conditions);
     // Way 3
     // A third way would be a combination like this:
     /*
     			$access = $this->user->hasAccess('blog read unpublished');
     			$posts = model\Post::postsByAccess($access, $this->_config('posts_on_index'));
     */
     // That way you can check access in the Controller and have fetch logic in the Model
     $messages = Session::messages();
     $canCreatePosts = $this->user->hasAccess('blog create posts');
     return get_defined_vars();
     // view will be rendered by row\Controller->_post_action
     return $this->_display(__METHOD__, get_defined_vars(), !$this->AJAX);
     // view will be rendered by Output->display
     return $this->_display(get_defined_vars());
     // view will be rendered by Output->display
 }
Пример #3
0
 public static function paginate($total, $perPage, $name, $options = array())
 {
     $return = Options::one($options, 'return', false);
     $start = (int) (bool) $options->get('start', 1);
     // always 0 or 1
     $prevnext = $options->prevnext;
     $firstlast = $options->firstlast;
     $pages = ceil($total / $perPage);
     $current = isset($_GET[$name]) ? max($start, (int) $_GET[$name]) : $start;
     $end = $pages - 1 + $start;
     $g = $_GET;
     $html = '<ul class="pager">';
     if (true === $firstlast || null === $firstlast && $start < $current) {
         $page = $start;
         $g[$name] = $page;
         $html .= '<li class="first' . ($current == $start ? ' disabled' : '') . '">' . static::link('first', true, array('get' => $g)) . '</li>';
     }
     if (true === $prevnext || null === $prevnext && $start < $current) {
         $page = max($start, $current - 1);
         $g[$name] = $page;
         $html .= '<li class="prev' . ($current == $g[$name] ? ' disabled' : '') . '">' . static::link('prev', true, array('get' => $g)) . '</li>';
     }
     for ($i = 0; $i < $pages; $i++) {
         $page = $i + $start;
         $g[$name] = $page;
         $html .= '<li class="page page-' . $page . ($current == $page ? ' current' : '') . ($start == $page ? ' first-page' : ($end == $page ? ' last-page' : '')) . '">' . static::link($page, true, array('get' => $g)) . '</li>';
     }
     if (true === $prevnext || null === $prevnext && $end > $current) {
         $page = min($end, $current + 1);
         $g[$name] = $page;
         $html .= '<li class="prev' . ($current == $g[$name] ? ' disabled' : '') . '">' . static::link('next', true, array('get' => $g)) . '</li>';
     }
     if (true === $firstlast || null === $firstlast && $end > $current) {
         $page = $end;
         $g[$name] = $page;
         $html .= '<li class="last' . ($current == $end ? ' disabled' : '') . '">' . static::link('last', true, array('get' => $g)) . '</li>';
     }
     $html .= '</ul>';
     return $html;
 }