| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * This class generates form components for Navigation. |
| 20 | */ |
| 21 | class CRM_Admin_Form_Navigation extends CRM_Admin_Form { |
| 22 | |
| 23 | /** |
| 24 | * The parent id of the navigation menu. |
| 25 | * @var int |
| 26 | */ |
| 27 | protected $_currentParentID = NULL; |
| 28 | |
| 29 | /** |
| 30 | * Build the form object. |
| 31 | */ |
| 32 | public function buildQuickForm() { |
| 33 | parent::buildQuickForm(); |
| 34 | |
| 35 | $this->setPageTitle(ts('Menu Item')); |
| 36 | |
| 37 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | if (isset($this->_id)) { |
| 42 | $params = ['id' => $this->_id]; |
| 43 | CRM_Core_BAO_Navigation::retrieve($params, $this->_defaults); |
| 44 | } |
| 45 | |
| 46 | $this->applyFilter('__ALL__', 'trim'); |
| 47 | $this->add('text', |
| 48 | 'label', |
| 49 | ts('Title'), |
| 50 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'label'), |
| 51 | TRUE |
| 52 | ); |
| 53 | |
| 54 | $this->add('text', 'url', ts('Url'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Navigation', 'url')); |
| 55 | |
| 56 | $this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]); |
| 57 | |
| 58 | $permissions = []; |
| 59 | foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $id => $vals) { |
| 60 | $permissions[] = ['id' => $id, 'text' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals)]; |
| 61 | } |
| 62 | $this->add('select2', 'permission', ts('Permission'), $permissions, FALSE, |
| 63 | ['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE] |
| 64 | ); |
| 65 | |
| 66 | $operators = ['AND' => ts('AND'), 'OR' => ts('OR')]; |
| 67 | $this->add('select', 'permission_operator', NULL, $operators); |
| 68 | |
| 69 | //make separator location configurable |
| 70 | $separator = [ts('None'), ts('After menu element'), ts('Before menu element')]; |
| 71 | $this->add('select', 'has_separator', ts('Separator'), $separator); |
| 72 | |
| 73 | $active = $this->add('advcheckbox', 'is_active', ts('Enabled')); |
| 74 | |
| 75 | if (CRM_Utils_Array::value('name', $this->_defaults) == 'Home') { |
| 76 | $active->freeze(); |
| 77 | } |
| 78 | else { |
| 79 | $parentMenu = CRM_Core_BAO_Navigation::getNavigationList(); |
| 80 | |
| 81 | if (isset($this->_id)) { |
| 82 | unset($parentMenu[$this->_id]); |
| 83 | } |
| 84 | |
| 85 | // also unset home. |
| 86 | $homeMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Home', 'id', 'name'); |
| 87 | unset($parentMenu[$homeMenuId]); |
| 88 | |
| 89 | $this->add('select', 'parent_id', ts('Parent'), ['' => ts('Top level')] + $parentMenu, FALSE, ['class' => 'crm-select2']); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @return array |
| 95 | */ |
| 96 | public function setDefaultValues() { |
| 97 | $defaults = parent::setDefaultValues(); |
| 98 | if (isset($this->_id)) { |
| 99 | //Take parent id in object variable to calculate the menu |
| 100 | //weight if menu parent id changed |
| 101 | $this->_currentParentID = CRM_Utils_Array::value('parent_id', $this->_defaults); |
| 102 | } |
| 103 | else { |
| 104 | $defaults['permission'] = "access CiviCRM"; |
| 105 | } |
| 106 | |
| 107 | // its ok if there is no element called is_active |
| 108 | $defaults['is_active'] = ($this->_id) ? $this->_defaults['is_active'] : 1; |
| 109 | |
| 110 | if (!empty($defaults['icon'])) { |
| 111 | $defaults['icon'] = trim(str_replace('crm-i', '', $defaults['icon'])); |
| 112 | } |
| 113 | |
| 114 | return $defaults; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Process the form submission. |
| 119 | */ |
| 120 | public function postProcess() { |
| 121 | // get the submitted form values. |
| 122 | $params = $this->controller->exportValues($this->_name); |
| 123 | |
| 124 | if (isset($this->_id)) { |
| 125 | $params['id'] = $this->_id; |
| 126 | $params['current_parent_id'] = $this->_currentParentID; |
| 127 | } |
| 128 | |
| 129 | if (!empty($params['icon'])) { |
| 130 | $params['icon'] = 'crm-i ' . $params['icon']; |
| 131 | } |
| 132 | |
| 133 | $navigation = CRM_Core_BAO_Navigation::add($params); |
| 134 | |
| 135 | // also reset navigation |
| 136 | CRM_Core_BAO_Navigation::resetNavigation(); |
| 137 | |
| 138 | CRM_Core_Session::setStatus(ts('Menu \'%1\' has been saved.', |
| 139 | [1 => $navigation->label] |
| 140 | ), ts('Saved'), 'success'); |
| 141 | } |
| 142 | |
| 143 | } |