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