Hawk - PHP documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • Hawk
    • View
      • Plugins

Classes

  • Hawk\App
  • Hawk\ButtonInput
  • Hawk\Cache
  • Hawk\CheckboxInput
  • Hawk\ColorInput
  • Hawk\Conf
  • Hawk\Controller
  • Hawk\Crypto
  • Hawk\DatabaseSessionHandler
  • Hawk\DatetimeInput
  • Hawk\DB
  • Hawk\DBExample
  • Hawk\DeleteInput
  • Hawk\Dialogbox
  • Hawk\EmailInput
  • Hawk\ErrorHandler
  • Hawk\Event
  • Hawk\FileInput
  • Hawk\FileSystem
  • Hawk\FloatInput
  • Hawk\Form
  • Hawk\FormFieldset
  • Hawk\FormInput
  • Hawk\GenericModel
  • Hawk\GifImage
  • Hawk\HawkApi
  • Hawk\HawkUpdater
  • Hawk\HiddenInput
  • Hawk\HtmlInput
  • Hawk\HTTPRequest
  • Hawk\Icon
  • Hawk\Image
  • Hawk\IntegerInput
  • Hawk\ItemList
  • Hawk\ItemListField
  • Hawk\JpegImage
  • Hawk\Lang
  • Hawk\Language
  • Hawk\LeftSidebarTab
  • Hawk\Less
  • Hawk\Logger
  • Hawk\Mail
  • Hawk\MenuItem
  • Hawk\Model
  • Hawk\NoSidebarTab
  • Hawk\NumberInput
  • Hawk\ObjectInput
  • Hawk\Option
  • Hawk\Panel
  • Hawk\PasswordInput
  • Hawk\Permission
  • Hawk\Plugin
  • Hawk\PluginInstaller
  • Hawk\PngImage
  • Hawk\ProfileQuestion
  • Hawk\ProfileQuestionValue
  • Hawk\RadioInput
  • Hawk\Request
  • Hawk\Response
  • Hawk\RightSidebarTab
  • Hawk\Role
  • Hawk\RolePermission
  • Hawk\Route
  • Hawk\Router
  • Hawk\SelectInput
  • Hawk\Session
  • Hawk\Singleton
  • Hawk\SubmitInput
  • Hawk\Tabs
  • Hawk\TextareaInput
  • Hawk\TextInput
  • Hawk\Theme
  • Hawk\TimeInput
  • Hawk\Upload
  • Hawk\User
  • Hawk\View
  • Hawk\View\Plugins\Accordion
  • Hawk\View\Plugins\Button
  • Hawk\View\Plugins\Form
  • Hawk\View\Plugins\Icon
  • Hawk\View\Plugins\Import
  • Hawk\View\Plugins\Panel
  • Hawk\View\Plugins\Tabs
  • Hawk\View\Plugins\Text
  • Hawk\View\Plugins\Uri
  • Hawk\View\Plugins\Widget
  • Hawk\ViewPlugin
  • Hawk\Widget
  • Hawk\WysiwygInput

Traits

  • Hawk\Utils

Exceptions

  • Hawk\AppStopException
  • Hawk\DBExampleException
  • Hawk\DBException
  • Hawk\FileSystemException
  • Hawk\HawkApiException
  • Hawk\ImageException
  • Hawk\MailException
  • Hawk\UploadException
  • Hawk\ViewException
  1 <?php
  2 /**
  3  * MenuItem.php
  4  *
  5  * @author  Elvyrra SAS
  6  * @license http://rem.mit-license.org/ MIT
  7  */
  8 namespace Hawk;
  9 
 10 
 11 /**
 12  * This class describes the MenuItem model
 13  *
 14  * @package BaseModels
 15  */
 16 class MenuItem extends Model{
 17     /**
 18      * The associated table
 19      *
 20      * @var sting
 21      */
 22     public static $tablename = "MenuItem";
 23 
 24     /**
 25      * The primary column
 26      *
 27      * @var string
 28      */
 29     protected static $primaryColumn = 'id';
 30 
 31     /**
 32      * The id of the user menu
 33      */
 34     const USER_ITEM_ID = 1;
 35 
 36     /**
 37      * The id of the admin menu
 38      */
 39     const ADMIN_ITEM_ID = 2;
 40 
 41     /**
 42      * Registered items
 43      *
 44      * @var array
 45      */
 46     private static $instances = array();
 47 
 48     /**
 49      * The menu item label
 50      *
 51      * @var string
 52      */
 53     public $label = '';
 54 
 55 
 56     /**
 57      * Constructor
 58      *
 59      * @param array $data The data to set on the instance properties
 60      */
 61     public function __construct($data = array()){
 62         parent::__construct($data);
 63 
 64         $this->visibleItems = array();
 65 
 66         if(empty($this->label) && !empty($this->labelKey)) {
 67             $this->label = Lang::get($this->labelKey);
 68         }
 69 
 70         if(!empty($this->action)) {
 71             $params = !empty($this->actionParameters) ? json_decode($this->actionParameters, true) : array();
 72             $this->url = App::router()->getUri($this->action, $params);
 73 
 74             if($this->url == Router::INVALID_URL) {
 75                 $this->url = $this->action;
 76             }
 77         }
 78 
 79         self::$instances[$this->plugin . '.' . $this->name] = $this;
 80     }
 81 
 82 
 83     /**
 84      * Get the items available for a specific user
 85      *
 86      * @param User $user The user. If not set, the current session user is set
 87      *
 88      * @return array The list of items
 89      */
 90     public static function getAvailableItems($user = null){
 91         if($user == null) {
 92             $user = App::session()->getUser();
 93         }
 94 
 95         // Get all items
 96         $items = self::getListByExample(
 97             new DBExample(
 98                 array(
 99                 'active' => 1
100                 )
101             ),
102             self::$primaryColumn,
103             array(),
104             array(
105             'parentId' => 'ASC',
106             'order' => 'ASC'
107             )
108         );
109 
110         // Filter unavailable items (that are not active or not accessible)
111         $items = array_filter(
112             $items, function ($item) use ($user) {
113                 $route = App::router()->getRouteByName($item->action);
114                 if($route) {
115                     return $route->isAccessible();
116                 }
117                 else{
118                     return !$item->permissionId || $user->isAllowed($item->permissionId);
119                 }
120             }
121         );
122 
123         // Put the sub items under their parent item
124         foreach($items as $item){
125             if($item->parentId) {
126                 $items[$item->parentId]->visibleItems[$item->order] = $item;
127                 unset($items[$item->id]);
128             }
129         }
130 
131         return $items;
132     }
133 
134 
135     /**
136      * Add a new item in the database
137      *
138      * @param array $data The item data to insert
139      *
140      * @return MenuItem The created item
141      */
142     public static function add($data){
143         if(empty($data['parentId'])) {
144             $data['parentId'] = 0;
145         }
146 
147         $data['order'] = App::db()->select(
148             array(
149             'fields' => array('COALESCE(MAX(`order`), 0) + 1' => 'newOrder'),
150             'from' => self::getTable(),
151             'where' => new DBExample(array('parentId' => $data['parentId'])),
152             'one' => true,
153             'return' => DB::RETURN_OBJECT
154             )
155         )->newOrder;
156 
157         // Insert the menu item
158         $item = parent::add($data);
159 
160         $event = new Event('menuitem.added', array('item' => $item));
161         $event->trigger();
162 
163         return $item;
164     }
165 
166     /**
167      * Find a menu item by it name, formatted as "<plugin>.<name>"
168      *
169      * @param string $name The item name
170      *
171      * @return MenuItem The found item
172      */
173     public static function getByName($name){
174         if(isset(self::$instances[$name])) {
175             return self::$instances[$name];
176         }
177         else{
178             list($plugin, $name) = explode('.', $name, 2);
179 
180             return self::getByExample(
181                 new DBExample(
182                     array(
183                     'plugin' => $plugin,
184                     'name' => $name
185                     )
186                 )
187             );
188         }
189     }
190 
191 
192     /**
193      * Get all the menu items of a plugin
194      *
195      * @param string $plugin The plugin to get
196      *
197      * @return array
198      */
199     public static function getPluginMenuItems($plugin){
200         return self::getListByExample(
201             new DBExample(
202                 array(
203                 'plugin' => $plugin
204                 )
205             )
206         );
207     }
208 
209 
210     /**
211      * Delete the menu item
212      */
213     public function delete(){
214         App::db()->update(
215             self::getTable(),
216             new DBExample(array('parentId' => $this->id)),
217             array('parentId' => 0)
218         );
219 
220         parent::delete();
221 
222         // Send an event to compute actions on menu item deletion
223         $event = new Event('menuitem.deleted', array('item' => $this));
224         $event->trigger();
225     }
226 }
Hawk - PHP documentation API documentation generated by ApiGen