province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Custom / Page / Field.php
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 * Create a page for displaying Custom Fields.
20 *
21 * Heart of this class is the run method which checks
22 * for action type and then displays the appropriate
23 * page.
24 *
25 */
26 class CRM_Custom_Page_Field extends CRM_Core_Page {
27
28 public $useLivePageJS = TRUE;
29
30 /**
31 * The group id of the field.
32 *
33 * @var int
34 */
35 protected $_gid;
36
37 /**
38 * The action links that we need to display for the browse screen.
39 *
40 * @var array
41 */
42 private static $_actionLinks;
43
44 /**
45 * Get the action links for this page.
46 *
47 * @return array
48 * array of action links that we need to display for the browse screen
49 */
50 public static function &actionLinks() {
51 if (!isset(self::$_actionLinks)) {
52 self::$_actionLinks = [
53 CRM_Core_Action::UPDATE => [
54 'name' => ts('Edit Field'),
55 'url' => 'civicrm/admin/custom/group/field/update',
56 'qs' => 'action=update&reset=1&gid=%%gid%%&id=%%id%%',
57 'title' => ts('Edit Custom Field'),
58 ],
59 CRM_Core_Action::BROWSE => [
60 'name' => ts('Edit Multiple Choice Options'),
61 'url' => 'civicrm/admin/custom/group/field/option',
62 'qs' => 'reset=1&action=browse&gid=%%gid%%&fid=%%id%%',
63 'title' => ts('List Custom Options'),
64 ],
65 CRM_Core_Action::PREVIEW => [
66 'name' => ts('Preview Field Display'),
67 'url' => 'civicrm/admin/custom/group/preview',
68 'qs' => 'action=preview&reset=1&fid=%%id%%',
69 'title' => ts('Preview Custom Field'),
70 ],
71 CRM_Core_Action::DISABLE => [
72 'name' => ts('Disable'),
73 'ref' => 'crm-enable-disable',
74 'title' => ts('Disable Custom Field'),
75 ],
76 CRM_Core_Action::ENABLE => [
77 'name' => ts('Enable'),
78 'ref' => 'crm-enable-disable',
79 'title' => ts('Enable Custom Field'),
80 ],
81 CRM_Core_Action::EXPORT => [
82 'name' => ts('Move'),
83 'url' => 'civicrm/admin/custom/group/field/move',
84 'class' => 'small-popup',
85 'qs' => 'reset=1&fid=%%id%%',
86 'title' => ts('Move Custom Field'),
87 ],
88 CRM_Core_Action::DELETE => [
89 'name' => ts('Delete'),
90 'url' => 'civicrm/admin/custom/group/field/delete',
91 'qs' => 'reset=1&id=%%id%%',
92 'title' => ts('Delete Custom Field'),
93 ],
94 ];
95 }
96 return self::$_actionLinks;
97 }
98
99 /**
100 * Browse all custom group fields.
101 *
102 * @return void
103 */
104 public function browse() {
105 $resourceManager = CRM_Core_Resources::singleton();
106 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
107 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
108 }
109
110 $customField = [];
111 $customFieldBAO = new CRM_Core_BAO_CustomField();
112
113 // fkey is gid
114 $customFieldBAO->custom_group_id = $this->_gid;
115 $customFieldBAO->orderBy('weight, label');
116 $customFieldBAO->find();
117
118 while ($customFieldBAO->fetch()) {
119 $customField[$customFieldBAO->id] = [];
120 CRM_Core_DAO::storeValues($customFieldBAO, $customField[$customFieldBAO->id]);
121 $action = array_sum(array_keys(self::actionLinks()));
122 if ($customFieldBAO->is_active) {
123 $action -= CRM_Core_Action::ENABLE;
124 }
125 else {
126 $action -= CRM_Core_Action::DISABLE;
127 }
128
129 switch ($customFieldBAO->data_type) {
130 case "String":
131 case "Int":
132 case "Float":
133 case "Money":
134 // if Multi Select field is selected in custom field
135 if ($customFieldBAO->html_type == 'Text') {
136 $action -= CRM_Core_Action::BROWSE;
137 }
138 break;
139
140 case "ContactReference":
141 case "Memo":
142 case "Date":
143 case "Boolean":
144 case "StateProvince":
145 case "Country":
146 case "File":
147 case "Link":
148 $action -= CRM_Core_Action::BROWSE;
149 break;
150 }
151
152 $customFieldDataType = array_column(CRM_Core_BAO_CustomField::dataType(), 'label', 'id');
153 $customField[$customFieldBAO->id]['data_type'] = $customFieldDataType[$customField[$customFieldBAO->id]['data_type']];
154 $customField[$customFieldBAO->id]['order'] = $customField[$customFieldBAO->id]['weight'];
155 $customField[$customFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
156 [
157 'id' => $customFieldBAO->id,
158 'gid' => $this->_gid,
159 ],
160 ts('more'),
161 FALSE,
162 'customField.row.actions',
163 'CustomField',
164 $customFieldBAO->id
165 );
166 }
167
168 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_gid}");
169 $filter = "custom_group_id = {$this->_gid}";
170 CRM_Utils_Weight::addOrder($customField, 'CRM_Core_DAO_CustomField',
171 'id', $returnURL, $filter
172 );
173
174 $this->assign('customField', $customField);
175 }
176
177 /**
178 * Run the page.
179 *
180 * This method is called after the page is created. It checks for the
181 * type of action and executes that action.
182 *
183 * @return void
184 */
185 public function run() {
186 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, TRUE);
187
188 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved')) {
189 CRM_Core_Error::statusBounce("You cannot add or edit fields in a reserved custom field-set.");
190 }
191
192 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_gid);
193 $this->assign('gid', $this->_gid);
194 $this->assign('groupTitle', $groupTitle);
195
196 // assign vars to templates
197 $this->assign('action', 'browse');
198
199 $this->browse();
200
201 return parent::run();
202 }
203
204 }