示例#1
0
文件: twig_bolt.php 项目: LeonB/site
 /**
  * Output a menu..
  *
  */
 public function menu(Twig_Environment $env, $identifier = "")
 {
     global $app;
     $menus = $app['config']['menu'];
     if (!empty($identifier) && isset($menus[$identifier])) {
         $name = strtolower($identifier);
         $menu = $menus[$identifier];
     } else {
         $name = strtolower(util::array_first_key($menus));
         $menu = util::array_first($menus);
     }
     foreach ($menu as $key => $item) {
         $menu[$key] = $this->menu_helper($item);
         if (isset($item['submenu'])) {
             foreach ($item['submenu'] as $subkey => $subitem) {
                 $menu[$key]['submenu'][$subkey] = $this->menu_helper($subitem);
             }
         }
     }
     // echo "<pre>\n" . util::var_dump($menu, true) . "</pre>\n";
     echo $env->render('_sub_menu.twig', array('name' => $name, 'menu' => $menu));
 }
示例#2
0
文件: storage.php 项目: LeonB/site
 public function getContent($contenttypeslug, $parameters = "", &$pager = array())
 {
     global $app;
     $returnsingle = false;
     // Some special cases, like 'entry/1' or 'page/about' need to be caught before further processing.
     if (preg_match('#^([a-z0-9_-]+)/([0-9]+)$#i', $contenttypeslug, $match)) {
         // like 'entry/12'
         $contenttypeslug = $match[1];
         $parameters['id'] = $match[2];
         $returnsingle = true;
     } else {
         if (preg_match('#^([a-z0-9_-]+)/([a-z0-9_-]+)$#i', $contenttypeslug, $match)) {
             // like 'page/lorem-ipsum-dolor'
             $contenttypeslug = $match[1];
             $parameters['slug'] = $match[2];
             $returnsingle = true;
         } else {
             if (preg_match('#^([a-z0-9_-]+)/(latest|first)/([0-9]+)$#i', $contenttypeslug, $match)) {
                 // like 'page/lorem-ipsum-dolor'
                 $contenttypeslug = $match[1];
                 $parameters['order'] = 'datecreated ' . ($match[2] == "latest" ? "DESC" : "ASC");
                 $parameters['limit'] = $match[3];
             }
         }
     }
     $limit = !empty($parameters['limit']) ? $parameters['limit'] : 100;
     $page = !empty($parameters['page']) ? $parameters['page'] : 1;
     // If we're allowed to use pagination, use the 'page' parameter.
     if (!empty($parameters['paging'])) {
         $page = !empty($_REQUEST['page']) ? $_REQUEST['page'] : $page;
     }
     $contenttype = $this->getContentType($contenttypeslug);
     // If we can't match to a valid contenttype, return (undefined) content;
     if (!$contenttype) {
         $emptycontent = new Content('', $contenttypeslug);
         $app['log']->add("Storage: No valid contenttype '{$contenttypeslug}'");
         return $emptycontent;
     }
     // If requesting something with a content-type slug in singular, return only the first item.
     if ($contenttype['singular_slug'] == $contenttypeslug || isset($parameters['returnsingle'])) {
         $returnsingle = true;
     }
     $tablename = $this->prefix . $contenttype['slug'];
     // for all the non-reserved parameters that are fields, we assume people want to do a 'where'
     foreach ($parameters as $key => $value) {
         if (in_array($key, array('order', 'where', 'limit', 'offset'))) {
             continue;
             // Skip this one..
         }
         if (!in_array($key, $this->getContentTypeFields($contenttype['slug'])) && !in_array($key, array("id", "slug", "datecreated", "datechanged", "username", "status"))) {
             continue;
             // Also skip if 'key' isn't a field in the contenttype.
         }
         $where[] = $this->parseWhereParameter($key, $value);
     }
     // If we need to filter, add the WHERE for that.
     // InnoDB doesn't support full text search. WTF is up with that shit?
     if (!empty($parameters['filter'])) {
         $filter = safeString($parameters['filter']);
         $filter_where = array();
         foreach ($contenttype['fields'] as $key => $value) {
             if (in_array($value['type'], array('text', 'textarea', 'html'))) {
                 $filter_where[] = sprintf("`%s` LIKE '%%%s%%'", $key, $filter);
             }
         }
         if (!empty($filter_where)) {
             $where[] = "(" . implode(" OR ", $filter_where) . ")";
         }
     }
     $queryparams = "";
     // implode 'where'
     if (!empty($where)) {
         $queryparams .= " WHERE (" . implode(" AND ", $where) . ")";
     }
     // Order
     if (!empty($parameters['order'])) {
         $order = safeString($parameters['order']);
         if ($order[0] == "-") {
             $order = substr($order, 1) . " DESC";
         }
         $queryparams .= " ORDER BY " . $order;
     }
     // Make the query for the pager..
     $pagerquery = "SELECT COUNT(*) AS count FROM {$tablename}" . $queryparams;
     // Add the limit
     $queryparams .= sprintf(" LIMIT %s, %s;", ($page - 1) * $limit, $limit);
     // Make the query to get the results..
     $query = "SELECT * FROM {$tablename}" . $queryparams;
     if (!$returnsingle) {
         //     echo "<pre>" . util::var_dump($query, true) . "</pre>";
     }
     $rows = $this->db->fetchAll($query);
     // Make sure content is set, and all content has information about its contenttype
     $content = array();
     foreach ($rows as $key => $value) {
         $content[$value['id']] = new Content($value, $contenttype);
     }
     // Make sure all content has their taxonomies
     $this->getTaxonomy($content);
     // Iterate over the contenttype's taxonomy, check if there's one we can use for grouping.
     // If so, iterate over the content, and set ['grouping'] for each unit of content.
     // But only if we're not sorting manually (i.e. have a ?order=.. parameter or $parameter['order'] )
     if (empty($_GET['order']) && empty($parameters['order']) || $contenttype['sort'] == $parameters['order']) {
         $have_grouping = false;
         $taxonomy = $this->getContentTypeTaxonomy($contenttypeslug);
         foreach ($taxonomy as $taxokey => $taxo) {
             if ($taxo['behaves_like'] == "grouping") {
                 $have_grouping = true;
                 break;
             }
         }
         if ($have_grouping) {
             uasort($content, function ($a, $b) {
                 if ($a->group == $b->group) {
                     return 0;
                 }
                 return $a->group < $b->group ? -1 : 1;
             });
         }
     }
     if (!$returnsingle) {
         // Set up the $pager array with relevant values..
         $rowcount = $this->db->executeQuery($pagerquery)->fetch();
         $pager = array('for' => $contenttypeslug, 'count' => $rowcount['count'], 'totalpages' => ceil($rowcount['count'] / $limit), 'current' => $page, 'showing_from' => ($page - 1) * $limit + 1, 'showing_to' => ($page - 1) * $limit + count($content));
         $GLOBALS['pager'][$contenttypeslug] = $pager;
     }
     // If we requested a singular item..
     if ($returnsingle) {
         if (util::array_first_key($content)) {
             return util::array_first($content);
         } else {
             $msg = sprintf("Storage: requested specific single content '%s%s%s', not found.", $contenttypeslug, isset($match[2]) ? "/" . $match[2] : "", isset($match[3]) ? "/" . $match[3] : "");
             $app['log']->add($msg);
             return false;
         }
     } else {
         return $content;
     }
 }
示例#3
0
 public function test_array_first()
 {
     $test = array('a' => array('a', 'b', 'c'));
     $this->assertEquals('a', util::array_first(util::array_get($test, 'a')));
 }