示例#1
0
 public function menu($identifier = null, $resolved = true)
 {
     $menus = $this->app['config']->get('menu');
     if (!empty($identifier) && isset($menus[$identifier])) {
         $name = strtolower($identifier);
         $menu = $menus[$identifier];
     } else {
         $name = strtolower(\utilphp\util::array_first_key($menus));
         $menu = \utilphp\util::array_first($menus);
     }
     if (!is_array($menu)) {
         $menu = array();
     }
     if (!$resolved) {
         return new Menu($name, $menu);
     }
     return new Menu($name, $this->resolve($menu), true);
 }
示例#2
0
 /**
  * Output a menu.
  *
  * @param \Twig_Environment $env
  * @param string            $identifier Identifier for a particular menu
  * @param string            $template   The template to use.
  * @param array             $params     Extra parameters to pass on to the menu template.
  *
  * @return null
  */
 public function menu(\Twig_Environment $env, $identifier = '', $template = '_sub_menu.twig', $params = array())
 {
     if ($this->safe) {
         return null;
     }
     $menus = $this->app['config']->get('menu');
     if (!empty($identifier) && isset($menus[$identifier])) {
         $name = strtolower($identifier);
         $menu = $menus[$identifier];
     } else {
         $name = strtolower(\utilphp\util::array_first_key($menus));
         $menu = \utilphp\util::array_first($menus);
     }
     // If the menu loaded is null, replace it with an empty array instead of
     // throwing an error.
     if (!is_array($menu)) {
         $menu = array();
     }
     $menu = $this->menuBuilder($menu);
     $twigvars = array('name' => $name, 'menu' => $menu);
     // If $params is not empty, merge it with twigvars.
     if (!empty($params) && is_array($params)) {
         $twigvars = $twigvars + $params;
     }
     return $env->render($template, $twigvars);
 }
示例#3
0
 /**
  * getContent based on a 'human readable query'.
  *
  * Used directly by {% setcontent %} but also in other parts.
  *
  * This code has been split into multiple methods in the spirit of
  * separation of concerns, but the situation is still far from ideal.
  *
  * Where applicable each 'concern' notes the coupling in the local
  * documentation.
  *
  * @param string $textquery
  * @param string $parameters
  * @param array  $pager
  * @param array  $whereparameters
  *
  * @return array
  */
 public function getContent($textquery, $parameters = '', &$pager = array(), $whereparameters = array())
 {
     // Start the 'stopwatch' for the profiler.
     $this->app['stopwatch']->start('bolt.getcontent', 'doctrine');
     // $whereparameters is passed if called from a compiled template. If present, merge it with $parameters.
     if (!empty($whereparameters)) {
         $parameters = array_merge((array) $parameters, (array) $whereparameters);
     }
     $logNotFound = false;
     if (isset($parameters['log_not_found'])) {
         $logNotFound = $parameters['log_not_found'];
         unset($parameters['log_not_found']);
     }
     // Decode this textquery
     $decoded = $this->decodeContentQuery($textquery, $parameters);
     if ($decoded === false) {
         $this->app['logger.system']->error("Not a valid query: '{$textquery}'", array('event' => 'storage'));
         $this->app['stopwatch']->stop('bolt.getcontent');
         return false;
     }
     // Run checks and some actions
     if (!$this->runContenttypeChecks($decoded['contenttypes'])) {
         $this->app['stopwatch']->stop('bolt.getcontent');
         return false;
     }
     // Run the actual queries
     list($results, $totalResults) = call_user_func($decoded['queries_callback'], $decoded, $parameters);
     // Perform post hydration ordering
     if ($decoded['order_callback'] !== false) {
         if (is_scalar($decoded['order_callback']) && $decoded['order_callback'] == 'RANDOM') {
             shuffle($results);
         } else {
             uasort($results, $decoded['order_callback']);
         }
     }
     // Perform pagination if necessary, but never paginate when 'returnsingle' is used.
     $offset = 0;
     $limit = false;
     if ($decoded['self_paginated'] === false && isset($decoded['parameters']['page']) && !$decoded['return_single']) {
         $offset = ($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'];
         $limit = $decoded['parameters']['limit'];
     }
     if ($limit !== false) {
         $results = array_slice($results, $offset, $limit);
     }
     // Return content
     if ($decoded['return_single']) {
         if (util::array_first_key($results)) {
             $this->app['stopwatch']->stop('bolt.getcontent');
             return util::array_first($results);
         }
         if ($logNotFound) {
             $msg = sprintf("Requested specific query '%s', not found.", $textquery);
             $this->app['logger.system']->error($msg, array('event' => 'storage'));
         }
         $this->app['stopwatch']->stop('bolt.getcontent');
         return false;
     }
     // Set up the $pager array with relevant values, but only if we requested paging.
     if (isset($decoded['parameters']['paging'])) {
         $pagerName = implode('_', $decoded['contenttypes']);
         $pager = array('for' => $pagerName, 'count' => $totalResults, 'totalpages' => ceil($totalResults / $decoded['parameters']['limit']), 'current' => $decoded['parameters']['page'], 'showing_from' => ($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'] + 1, 'showing_to' => ($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'] + count($results));
         $this->setPager($pagerName, $pager);
         $this->app['twig']->addGlobal('pager', $this->getPager());
     }
     $this->app['stopwatch']->stop('bolt.getcontent');
     return $results;
 }
示例#4
0
 public function test_array_first()
 {
     $test = array('a' => array('a', 'b', 'c'));
     $this->assertEquals('a', util::array_first(util::array_get($test['a'])));
 }