public static function one(&$options, $name, $alt = null, $convert = true) { if (!$convert && is_array($options)) { return isset($options[$name]) ? $options[$name] : $alt; } $options = Options::make($options); return $options->get($name, $alt); }
protected function _init() { // overridable _POST and _GET $this->post = Options::make($_POST); $this->get = Options::make($_GET); $this->user = SessionUser::user(); // overridable Output / View / Template engine $O = Output::$class; $this->tpl = new $O($this); }
public function resolveRoute($route, $uri) { // routes have a leading / $path = '/' . $uri; // make a nice little regex $from = $route->from; $from = trim($from, '^ /'); if (!$this->dispatcher->options->case_sensitive_paths) { $from = strtolower($from); $path = strtolower($path); } $from = $this->routeToRegex($from, $route); $from = '^/' . $from; $from = '#' . $from . '#'; // regex the path if (0 < preg_match($from, $path, $match)) { // NULL, Array, String or Closure $to = $route->to; if (null === $to) { // Array $to = $match; } else { if (is_callable($to)) { // String or Array $to = $to($match, $uri); } } if (is_string($to)) { $options = Options::make($route->options); $match[0] = preg_replace('/%(\\d+)/', '%\\1$s', $to); $goto = call_user_func_array('sprintf', $match); if ($options->redirect) { return $this->redirect($goto, $options->redirect); } return $goto; } else { if (is_array($to)) { // any implicit arguments? if (!isset($to['arguments'])) { // arguments from regex if (1 < count($match)) { $to['arguments'] = array_slice($match, 1); } } // return Location Array return $to; } } } }
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 }
public function getOptions() { return Options::make(array('default_action' => 'index', 'not_found_exception' => 'row\\http\\NotFoundException', 'module_class_prefix' => '', 'module_class_postfix' => 'Controller', 'action_name_prefix' => '', 'action_name_postfix' => 'Action', 'action_path_wildcards' => array('#' => '(\\d+)', '%' => '([^/]+)', '*' => '(.+)', 'DATE' => '(\\d{4}-\\d\\d?\\-\\d\\d?)'), 'case_sensitive_paths' => false)); }
public function __construct($rules, $options = array()) { $this->rules = $rules; $this->options = Options::make($options, Options::make(array('errors' => Options::make(array('notEmpty' => 'Must submit value', 'regex' => 'Invalid value format'))))); }
public final function __construct(\row\Controller $application, $options = array()) { $this->application = $application; $this->options = Options::make($options); $this->_fire('init'); }
function options($options) { return Options::make($options); }
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(); }
public function &useElements() { if (!$this->_elements) { if (!is_a($this->defaults, 'row\\database\\Model') && !is_a($this->defaults, 'row\\core\\Options')) { $this->defaults = Options::make((array) $this->defaults); } $elements = array(); $index = 0; foreach ($this->elements($this->defaults) as $name => $element) { $element['_name'] = $name; $element['_index'] = $index++; $this->elementTitle($element); $elements[$name] = $element; } $this->_elements = $elements; } return $this->_elements; }
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; }