示例#1
0
    /**
     * Gets every menu item for a menu category. Can also return all children
     * that the menu item has as well.
     *
     * Can also check permission of the menu items, if user does not
     * have permission they wont be returned.
     *
     * @param int $cid
     * @param bool $flat			Flattern the 'children' key?
     * @param bool $withChildren
     * @param $aclCheck
     * @return array
     */
    public function getAllItems($cid, $flat = false, $withChildren = true, $aclCheck = true)
    {
        $category = $this->getCategory($cid, $aclCheck);
        if (!($items = $this->_cache->get('menu_items_' . $category['id']))) {
            $items = array();
            $pdoSt = $this->_sql->prepare('SELECT * FROM {PREFIX}mod_menu
												WHERE cat_id = ? AND heading_id = 0 ORDER BY `order` ASC ');
            $pdoSt->execute(array($category['id']));
            $tmpItems = $pdoSt->fetchAll(PDO::FETCH_ASSOC);
            $count = count($tmpItems);
            for ($i = 0; $i < $count; $i++) {
                // Add in some more array keys
                $tmpItems[$i]['depth'] = 0;
                $tmpItems[$i]['order_range'] = $count;
                $items[$tmpItems[$i]['id']] = $tmpItems[$i];
            }
            $this->_cache->add('menu_items_' . $category['id'], $items);
        }
        // Gather all subitems/children for this menu item
        foreach ($items as &$item) {
            $resource = 'menu-item-' . $item['id'];
            if ($aclCheck && (!$this->_acl->resourceExists($resource) || !$this->_acl->check($resource))) {
                unset($items[$item['id']]);
            } else {
                if ($withChildren) {
                    $item['children'] = $this->getChildItems($item['id'], $aclCheck);
                }
            }
        }
        return $flat ? zula_array_flatten($items, 'children') : $items;
    }
示例#2
0
 /**
  * Builds the correct view form for either adding or editing
  * a menu item, can also add in default values.
  *
  * @param int $cid
  * @param string $name
  * @param string $parentHeading
  * @param string $url
  * @param string $attrTitle
  * @param string $itemId
  * @return object
  */
 protected function buildItemForm($cid, $name = null, $parentHeading = 0, $url = null, $attrTitle = null, $itemId = null)
 {
     if (is_null($itemId)) {
         $op = 'add';
         $aclResource = 'menu-item';
     } else {
         $op = 'edit';
         $aclResource = 'menu-item-' . $itemId;
     }
     // Build form and validation
     $form = new View_Form('config/form_item.html', 'menu', is_null($itemId));
     $form->addElement('menu/id', $itemId, 'ID', new Validator_Int(), $op == 'edit');
     $form->addElement('menu/cat_id', $cid, 'Cat ID', new Validator_Int(), $op == 'add');
     $form->addElement('menu/name', $name, t('Name'), new Validator_Length(1, 64));
     $form->addElement('menu/parent', $parentHeading, t('Parent'), new Validator_Numeric());
     $form->addElement('menu/url', $url, 'URL', new Validator_Length(0, 255));
     $form->addElement('menu/attr_title', $attrTitle, t('Attribute Text'), new Validator_Length(0, 255));
     // Add additional vars
     $form->assign(array('OP' => $op, 'HEADINGS' => zula_array_flatten($this->_model()->getAllItems($cid), 'children')));
     $form->assignHtml(array('ACL_FORM' => $this->_acl->buildForm(array(t('View menu item') => $aclResource))));
     return $form;
 }
示例#3
0
/**
 * Flattens an array based upon a key. This is useful for a multidimensional
 * array that recursively contains, for example - children items, in a menu.
 *
 * It will flatten it into a simply 2 dimension array.
 *
 * @param array $array
 * @param string $key
 * @return array
 */
function zula_array_flatten(array $array, $flattenKey)
{
    $newArray = array();
    foreach ($array as $val) {
        if (empty($val[$flattenKey])) {
            unset($val[$flattenKey]);
            $newArray[] = $val;
        } else {
            $flattenValue = $val[$flattenKey];
            unset($val[$flattenKey]);
            // Attach the value (without the children)
            $newArray[] = $val;
            foreach ($flattenValue as $tmpVal) {
                if (!empty($tmpVal[$flattenKey])) {
                    $children = $tmpVal[$flattenKey];
                    unset($tmpVal[$flattenKey]);
                    $newArray[] = $tmpVal;
                    $newArray = array_merge($newArray, zula_array_flatten($children, $flattenKey));
                } else {
                    unset($tmpVal[$flattenKey]);
                    $newArray[] = $tmpVal;
                }
            }
        }
    }
    return $newArray;
}