| 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 | * Base class for admin forms. |
| 20 | */ |
| 21 | class CRM_Admin_Form extends CRM_Core_Form { |
| 22 | |
| 23 | /** |
| 24 | * The id of the object being edited / created |
| 25 | * |
| 26 | * @var int |
| 27 | */ |
| 28 | protected $_id; |
| 29 | |
| 30 | /** |
| 31 | * The default values for form fields. |
| 32 | * |
| 33 | * @var int |
| 34 | */ |
| 35 | protected $_values; |
| 36 | |
| 37 | /** |
| 38 | * The name of the BAO object for this form. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | protected $_BAOName; |
| 43 | |
| 44 | /** |
| 45 | * Explicitly declare the form context. |
| 46 | */ |
| 47 | public function getDefaultContext() { |
| 48 | return 'create'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Basic setup. |
| 53 | */ |
| 54 | public function preProcess() { |
| 55 | Civi::resources()->addStyleFile('civicrm', 'css/admin.css'); |
| 56 | Civi::resources()->addScriptFile('civicrm', 'js/jquery/jquery.crmIconPicker.js'); |
| 57 | |
| 58 | $this->_id = $this->get('id'); |
| 59 | $this->_BAOName = $this->get('BAOName'); |
| 60 | $this->_values = []; |
| 61 | if (isset($this->_id)) { |
| 62 | $params = ['id' => $this->_id]; |
| 63 | // this is needed if the form is outside the CRM name space |
| 64 | $baoName = $this->_BAOName; |
| 65 | $baoName::retrieve($params, $this->_values); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set default values for the form. Note that in edit/view mode |
| 71 | * the default values are retrieved from the database |
| 72 | * |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | public function setDefaultValues() { |
| 77 | // Fetch defaults from the db |
| 78 | if (!empty($this->_id) && empty($this->_values) && CRM_Utils_Rule::positiveInteger($this->_id)) { |
| 79 | $this->_values = []; |
| 80 | $params = ['id' => $this->_id]; |
| 81 | $baoName = $this->_BAOName; |
| 82 | $baoName::retrieve($params, $this->_values); |
| 83 | } |
| 84 | $defaults = $this->_values; |
| 85 | |
| 86 | // Allow defaults to be set from the url |
| 87 | if (empty($this->_id) && $this->_action & CRM_Core_Action::ADD) { |
| 88 | foreach ($_GET as $key => $val) { |
| 89 | if ($this->elementExists($key)) { |
| 90 | $defaults[$key] = $val; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if ($this->_action == CRM_Core_Action::DELETE && |
| 96 | isset($defaults['name']) |
| 97 | ) { |
| 98 | $this->assign('delName', $defaults['name']); |
| 99 | } |
| 100 | |
| 101 | // its ok if there is no element called is_active |
| 102 | $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1; |
| 103 | if (!empty($defaults['parent_id'])) { |
| 104 | $this->assign('is_parent', TRUE); |
| 105 | } |
| 106 | return $defaults; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add standard buttons. |
| 111 | */ |
| 112 | public function buildQuickForm() { |
| 113 | if ($this->_action & CRM_Core_Action::VIEW || $this->_action & CRM_Core_Action::PREVIEW) { |
| 114 | $this->addButtons([ |
| 115 | [ |
| 116 | 'type' => 'cancel', |
| 117 | 'name' => ts('Done'), |
| 118 | 'isDefault' => TRUE, |
| 119 | ], |
| 120 | ]); |
| 121 | } |
| 122 | else { |
| 123 | $this->addButtons([ |
| 124 | [ |
| 125 | 'type' => 'next', |
| 126 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), |
| 127 | 'isDefault' => TRUE, |
| 128 | ], |
| 129 | [ |
| 130 | 'type' => 'cancel', |
| 131 | 'name' => ts('Cancel'), |
| 132 | ], |
| 133 | ]); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | } |