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