Merge pull request #22267 from eileenmcnaughton/cont_test2
[civicrm-core.git] / CRM / ACL / Form / ACLBasic.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 class CRM_ACL_Form_ACLBasic extends CRM_Admin_Form {
18
19 /**
20 * @var bool
21 */
22 public $submitOnce = TRUE;
23
24 /**
25 * Set default values for the form.
26 */
27 public function setDefaultValues() {
28 $defaults = [];
29
30 if ($this->_id ||
31 $this->_id === '0'
32 ) {
33 $defaults['entity_id'] = $this->_id;
34
35 $query = "
36 SELECT object_table
37 FROM civicrm_acl
38 WHERE entity_id = %1
39 AND ( object_table NOT IN ( 'civicrm_saved_search', 'civicrm_uf_group', 'civicrm_custom_group' ) )
40 ";
41 $params = [1 => [$this->_id, 'Integer']];
42 $dao = CRM_Core_DAO::executeQuery($query, $params);
43 $defaults['object_table'] = [];
44 while ($dao->fetch()) {
45 $defaults['object_table'][$dao->object_table] = 1;
46 }
47 }
48
49 return $defaults;
50 }
51
52 /**
53 * Build the form object.
54 */
55 public function buildQuickForm() {
56 parent::buildQuickForm();
57
58 if ($this->_action & CRM_Core_Action::DELETE) {
59 return;
60 }
61
62 $permissions = array_flip(CRM_Core_Permission::basicPermissions());
63 $this->addCheckBox('object_table',
64 ts('ACL Type'),
65 $permissions,
66 NULL, NULL, TRUE, NULL,
67 ['</td><td>', '</td></tr><tr><td>']
68 );
69
70 $label = ts('Role');
71 $role = [
72 '-1' => ts('- select role -'),
73 '0' => ts('Everyone'),
74 ] + CRM_Core_OptionGroup::values('acl_role');
75 $entityID = &$this->add('select', 'entity_id', $label, $role, TRUE);
76
77 if ($this->_id) {
78 $entityID->freeze();
79 }
80 $this->add('checkbox', 'is_active', ts('Enabled?'));
81
82 $this->addFormRule(['CRM_ACL_Form_ACLBasic', 'formRule']);
83 }
84
85 /**
86 * @param array $params
87 *
88 * @return array|bool
89 */
90 public static function formRule($params) {
91 if ($params['entity_id'] == -1) {
92 $errors = ['entity_id' => ts('Role is a required field')];
93 return $errors;
94 }
95
96 return TRUE;
97 }
98
99 /**
100 * Process the form submission.
101 */
102 public function postProcess() {
103 CRM_ACL_BAO_Cache::resetCache();
104
105 $params = $this->controller->exportValues($this->_name);
106 if ($this->_id ||
107 $this->_id === '0'
108 ) {
109 $query = "
110 DELETE
111 FROM civicrm_acl
112 WHERE entity_id = %1
113 AND ( object_table NOT IN ( 'civicrm_saved_search', 'civicrm_uf_group', 'civicrm_custom_group' ) )
114 ";
115 $deleteParams = [1 => [$this->_id, 'Integer']];
116 CRM_Core_DAO::executeQuery($query, $deleteParams);
117
118 if ($this->_action & CRM_Core_Action::DELETE) {
119 CRM_Core_Session::setStatus(ts('Selected ACL has been deleted.'), ts('Record Deleted'), 'success');
120 return;
121 }
122 }
123
124 $params['operation'] = 'All';
125 $params['deny'] = 0;
126 $params['is_active'] = 1;
127 $params['entity_table'] = 'civicrm_acl_role';
128 $params['name'] = 'Core ACL';
129
130 foreach ($params['object_table'] as $object_table => $value) {
131 if ($value) {
132 $newParams = $params;
133 unset($newParams['object_table']);
134 $newParams['object_table'] = $object_table;
135 CRM_ACL_BAO_ACL::create($newParams);
136 }
137 }
138 }
139
140 }