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); }
/** * Get the relations for one or more units of content, return the array with the taxonomy attached. * * @param array $content * * @return array $content */ protected function getRelation($content) { $tablename = $this->getTablename("relations"); $ids = util::array_pluck($content, 'id'); if (empty($ids)) { return; } // Get the contenttype from first $content $contenttype = $content[util::array_first_key($content)]->contenttype['slug']; $query = sprintf("SELECT * FROM %s WHERE from_contenttype=? AND from_id IN (?) ORDER BY id", $tablename); $params = array($contenttype, $ids); $paramTypes = array(\PDO::PARAM_STR, DoctrineConn::PARAM_INT_ARRAY); $rows = $this->app['db']->executeQuery($query, $params, $paramTypes)->fetchAll(); foreach ($rows as $row) { $content[$row['from_id']]->setRelation($row['to_contenttype'], $row['to_id']); } // switch it, flip it and reverse it. wop wop wop. $query = sprintf("SELECT * FROM %s WHERE to_contenttype=? AND to_id IN (?) ORDER BY id", $tablename); $params = array($contenttype, $ids); $paramTypes = array(\PDO::PARAM_STR, DoctrineConn::PARAM_INT_ARRAY); $rows = $this->app['db']->executeQuery($query, $params, $paramTypes)->fetchAll(); foreach ($rows as $row) { $content[$row['to_id']]->setRelation($row['from_contenttype'], $row['from_id']); } }
public function getRepeaters($content) { $ids = util::array_pluck($content, 'id'); if (empty($ids)) { return; } // Get the contenttype from first $content $contenttypeslug = $content[util::array_first_key($content)]->contenttype['slug']; $contenttype = $this->getContentType($contenttypeslug); $repo = $this->app['storage']->getRepository('Bolt\\Storage\\Entity\\FieldValue'); foreach ($ids as $id) { foreach ($contenttype['fields'] as $fieldkey => $field) { if ($field['type'] == 'repeater') { $collection = new RepeatingFieldCollection($this->app['storage'], $field); $existingFields = $repo->getExistingFields($id, $contenttypeslug, $fieldkey) ?: []; foreach ($existingFields as $group => $ids) { $collection->addFromReferences($ids, $group); } $content[$id]->setValue($fieldkey, $collection); } } } }
/** * 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 = [], $whereparameters = []) { // Start the 'stopwatch' for the profiler. $this->app['stopwatch']->start('bolt.getcontent', 'blimp-http'); // $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}'", ['event' => 'storage']); $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 (!empty($results) && 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, ['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 = ['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; }
/** * 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); }
public function test_array_first_key() { $test = array('a' => array('a' => 'b', 'c' => 'd')); $this->assertEquals('a', util::array_first_key(util::array_get($test['a']))); }