Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 | 16 | */ |
6a488035 TO |
17 | class CRM_ACL_Form_ACLBasic extends CRM_Admin_Form { |
18 | ||
88aae6d4 A |
19 | /** |
20 | * @var bool | |
21 | */ | |
22 | public $submitOnce = TRUE; | |
23 | ||
6a488035 | 24 | /** |
c490a46a | 25 | * Set default values for the form. |
6a488035 | 26 | */ |
00be9182 | 27 | public function setDefaultValues() { |
cf0d1c08 | 28 | $defaults = []; |
6a488035 TO |
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 | "; | |
cf0d1c08 | 41 | $params = [1 => [$this->_id, 'Integer']]; |
6a488035 | 42 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
cf0d1c08 | 43 | $defaults['object_table'] = []; |
6a488035 TO |
44 | while ($dao->fetch()) { |
45 | $defaults['object_table'][$dao->object_table] = 1; | |
46 | } | |
47 | } | |
48 | ||
49 | return $defaults; | |
50 | } | |
51 | ||
52 | /** | |
d2e5d2ce | 53 | * Build the form object. |
6a488035 TO |
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, | |
cf0d1c08 | 67 | ['</td><td>', '</td></tr><tr><td>'] |
6a488035 TO |
68 | ); |
69 | ||
6a488035 | 70 | $label = ts('Role'); |
cf0d1c08 | 71 | $role = [ |
0d48f1cc TO |
72 | '-1' => ts('- select role -'), |
73 | '0' => ts('Everyone'), | |
74 | ] + CRM_Core_OptionGroup::values('acl_role'); | |
6a488035 TO |
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 | ||
cf0d1c08 | 82 | $this->addFormRule(['CRM_ACL_Form_ACLBasic', 'formRule']); |
6a488035 TO |
83 | } |
84 | ||
28518c90 | 85 | /** |
c490a46a | 86 | * @param array $params |
28518c90 EM |
87 | * |
88 | * @return array|bool | |
89 | */ | |
00be9182 | 90 | public static function formRule($params) { |
6a488035 | 91 | if ($params['entity_id'] == -1) { |
cf0d1c08 | 92 | $errors = ['entity_id' => ts('Role is a required field')]; |
6a488035 TO |
93 | return $errors; |
94 | } | |
95 | ||
96 | return TRUE; | |
97 | } | |
98 | ||
99 | /** | |
d2e5d2ce | 100 | * Process the form submission. |
6a488035 TO |
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 | "; | |
cf0d1c08 | 115 | $deleteParams = [1 => [$this->_id, 'Integer']]; |
7e1f3142 | 116 | CRM_Core_DAO::executeQuery($query, $deleteParams); |
6a488035 TO |
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 | } | |
96025800 | 139 | |
6a488035 | 140 | } |