PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Custom/Page/Field.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 316 lines | 188 code | 31 blank | 97 comment | 17 complexity | a04815e5a07551d8cc18e4389cb8ccba MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. * $Id$
  32. *
  33. */
  34. /**
  35. * Create a page for displaying Custom Fields.
  36. *
  37. * Heart of this class is the run method which checks
  38. * for action type and then displays the appropriate
  39. * page.
  40. *
  41. */
  42. class CRM_Custom_Page_Field extends CRM_Core_Page {
  43. public $useLivePageJS = TRUE;
  44. /**
  45. * The group id of the field.
  46. *
  47. * @var int
  48. */
  49. protected $_gid;
  50. /**
  51. * The action links that we need to display for the browse screen.
  52. *
  53. * @var array
  54. */
  55. private static $_actionLinks;
  56. /**
  57. * Get the action links for this page.
  58. *
  59. * @return array
  60. * array of action links that we need to display for the browse screen
  61. */
  62. public static function &actionLinks() {
  63. if (!isset(self::$_actionLinks)) {
  64. self::$_actionLinks = [
  65. CRM_Core_Action::UPDATE => [
  66. 'name' => ts('Edit Field'),
  67. 'url' => 'civicrm/admin/custom/group/field/update',
  68. 'qs' => 'action=update&reset=1&gid=%%gid%%&id=%%id%%',
  69. 'title' => ts('Edit Custom Field'),
  70. ],
  71. CRM_Core_Action::BROWSE => [
  72. 'name' => ts('Edit Multiple Choice Options'),
  73. 'url' => 'civicrm/admin/custom/group/field/option',
  74. 'qs' => 'reset=1&action=browse&gid=%%gid%%&fid=%%id%%',
  75. 'title' => ts('List Custom Options'),
  76. ],
  77. CRM_Core_Action::PREVIEW => [
  78. 'name' => ts('Preview Field Display'),
  79. 'url' => 'civicrm/admin/custom/group/field',
  80. 'qs' => 'action=preview&reset=1&gid=%%gid%%&id=%%id%%',
  81. 'title' => ts('Preview Custom Field'),
  82. ],
  83. CRM_Core_Action::DISABLE => [
  84. 'name' => ts('Disable'),
  85. 'ref' => 'crm-enable-disable',
  86. 'title' => ts('Disable Custom Field'),
  87. ],
  88. CRM_Core_Action::ENABLE => [
  89. 'name' => ts('Enable'),
  90. 'ref' => 'crm-enable-disable',
  91. 'title' => ts('Enable Custom Field'),
  92. ],
  93. CRM_Core_Action::EXPORT => [
  94. 'name' => ts('Move'),
  95. 'url' => 'civicrm/admin/custom/group/field/move',
  96. 'class' => 'small-popup',
  97. 'qs' => 'reset=1&fid=%%id%%',
  98. 'title' => ts('Move Custom Field'),
  99. ],
  100. CRM_Core_Action::DELETE => [
  101. 'name' => ts('Delete'),
  102. 'url' => 'civicrm/admin/custom/group/field',
  103. 'qs' => 'action=delete&reset=1&gid=%%gid%%&id=%%id%%',
  104. 'title' => ts('Delete Custom Field'),
  105. ],
  106. ];
  107. }
  108. return self::$_actionLinks;
  109. }
  110. /**
  111. * Browse all custom group fields.
  112. *
  113. * @return void
  114. */
  115. public function browse() {
  116. $resourceManager = CRM_Core_Resources::singleton();
  117. if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
  118. $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
  119. }
  120. $customField = [];
  121. $customFieldBAO = new CRM_Core_BAO_CustomField();
  122. // fkey is gid
  123. $customFieldBAO->custom_group_id = $this->_gid;
  124. $customFieldBAO->orderBy('weight, label');
  125. $customFieldBAO->find();
  126. while ($customFieldBAO->fetch()) {
  127. $customField[$customFieldBAO->id] = [];
  128. CRM_Core_DAO::storeValues($customFieldBAO, $customField[$customFieldBAO->id]);
  129. $action = array_sum(array_keys(self::actionLinks()));
  130. if ($customFieldBAO->is_active) {
  131. $action -= CRM_Core_Action::ENABLE;
  132. }
  133. else {
  134. $action -= CRM_Core_Action::DISABLE;
  135. }
  136. switch ($customFieldBAO->data_type) {
  137. case "String":
  138. case "Int":
  139. case "Float":
  140. case "Money":
  141. // if Multi Select field is selected in custom field
  142. if ($customFieldBAO->html_type == 'Text') {
  143. $action -= CRM_Core_Action::BROWSE;
  144. }
  145. break;
  146. case "ContactReference":
  147. case "Memo":
  148. case "Date":
  149. case "Boolean":
  150. case "StateProvince":
  151. case "Country":
  152. case "File":
  153. case "Link":
  154. $action -= CRM_Core_Action::BROWSE;
  155. break;
  156. }
  157. $customFieldDataType = CRM_Core_BAO_CustomField::dataType();
  158. $customField[$customFieldBAO->id]['data_type'] = $customFieldDataType[$customField[$customFieldBAO->id]['data_type']];
  159. $customField[$customFieldBAO->id]['order'] = $customField[$customFieldBAO->id]['weight'];
  160. $customField[$customFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
  161. [
  162. 'id' => $customFieldBAO->id,
  163. 'gid' => $this->_gid,
  164. ],
  165. ts('more'),
  166. FALSE,
  167. 'customField.row.actions',
  168. 'CustomField',
  169. $customFieldBAO->id
  170. );
  171. }
  172. $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_gid}");
  173. $filter = "custom_group_id = {$this->_gid}";
  174. CRM_Utils_Weight::addOrder($customField, 'CRM_Core_DAO_CustomField',
  175. 'id', $returnURL, $filter
  176. );
  177. $this->assign('customField', $customField);
  178. }
  179. /**
  180. * Edit custom data.
  181. *
  182. * editing would involved modifying existing fields + adding data to new fields.
  183. *
  184. * @param string $action
  185. * The action to be invoked.
  186. *
  187. * @return void
  188. */
  189. public function edit($action) {
  190. // create a simple controller for editing custom dataCRM/Custom/Page/Field.php
  191. $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Field', ts('Custom Field'), $action);
  192. // set the userContext stack
  193. $session = CRM_Core_Session::singleton();
  194. $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
  195. $controller->set('gid', $this->_gid);
  196. $controller->setEmbedded(TRUE);
  197. $controller->process();
  198. $controller->run();
  199. }
  200. /**
  201. * Run the page.
  202. *
  203. * This method is called after the page is created. It checks for the
  204. * type of action and executes that action.
  205. *
  206. * @return void
  207. */
  208. public function run() {
  209. $id = CRM_Utils_Request::retrieve('id', 'Positive',
  210. $this, FALSE, 0
  211. );
  212. if ($id) {
  213. $values = civicrm_api3('custom_field', 'getsingle', ['id' => $id]);
  214. $this->_gid = $values['custom_group_id'];
  215. }
  216. // get the group id
  217. else {
  218. $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
  219. $this
  220. );
  221. }
  222. if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
  223. CRM_Core_Error::fatal("You cannot add or edit fields in a reserved custom field-set.");
  224. }
  225. $action = CRM_Utils_Request::retrieve('action', 'String',
  226. // default to 'browse'
  227. $this, FALSE, 'browse'
  228. );
  229. if ($action & CRM_Core_Action::DELETE) {
  230. $session = CRM_Core_Session::singleton();
  231. $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
  232. $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_DeleteField', "Delete Custom Field", '');
  233. $id = CRM_Utils_Request::retrieve('id', 'Positive',
  234. $this, FALSE, 0
  235. );
  236. $controller->set('id', $id);
  237. $controller->setEmbedded(TRUE);
  238. $controller->process();
  239. $controller->run();
  240. $fieldValues = ['custom_group_id' => $this->_gid];
  241. $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_CustomField', $id, $fieldValues);
  242. }
  243. if ($this->_gid) {
  244. $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_gid);
  245. $this->assign('gid', $this->_gid);
  246. $this->assign('groupTitle', $groupTitle);
  247. if ($action & CRM_Core_Action::BROWSE) {
  248. CRM_Utils_System::setTitle(ts('%1 - Custom Fields', [1 => $groupTitle]));
  249. }
  250. }
  251. // assign vars to templates
  252. $this->assign('action', $action);
  253. // what action to take ?
  254. if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
  255. // no browse for edit/update/view
  256. $this->edit($action);
  257. }
  258. elseif ($action & CRM_Core_Action::PREVIEW) {
  259. $this->preview($id);
  260. }
  261. else {
  262. $this->browse();
  263. }
  264. // Call the parents run method
  265. return parent::run();
  266. }
  267. /**
  268. * Preview custom field.
  269. *
  270. * @param int $id
  271. * Custom field id.
  272. *
  273. * @return void
  274. */
  275. public function preview($id) {
  276. $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW);
  277. $session = CRM_Core_Session::singleton();
  278. $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
  279. $controller->set('fieldId', $id);
  280. $controller->set('groupId', $this->_gid);
  281. $controller->setEmbedded(TRUE);
  282. $controller->process();
  283. $controller->run();
  284. }
  285. }