Merge pull request #16004 from civicrm/5.20
[civicrm-core.git] / CRM / ACL / Form / ACLBasic.php
CommitLineData
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
17class CRM_ACL_Form_ACLBasic extends CRM_Admin_Form {
18
19 /**
c490a46a 20 * Set default values for the form.
6a488035 21 */
00be9182 22 public function setDefaultValues() {
cf0d1c08 23 $defaults = [];
6a488035
TO
24
25 if ($this->_id ||
26 $this->_id === '0'
27 ) {
28 $defaults['entity_id'] = $this->_id;
29
30 $query = "
31SELECT 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";
cf0d1c08 36 $params = [1 => [$this->_id, 'Integer']];
6a488035 37 $dao = CRM_Core_DAO::executeQuery($query, $params);
cf0d1c08 38 $defaults['object_table'] = [];
6a488035
TO
39 while ($dao->fetch()) {
40 $defaults['object_table'][$dao->object_table] = 1;
41 }
42 }
43
44 return $defaults;
45 }
46
47 /**
d2e5d2ce 48 * Build the form object.
6a488035
TO
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,
cf0d1c08 62 ['</td><td>', '</td></tr><tr><td>']
6a488035
TO
63 );
64
6a488035 65 $label = ts('Role');
cf0d1c08 66 $role = [
0d48f1cc
TO
67 '-1' => ts('- select role -'),
68 '0' => ts('Everyone'),
69 ] + CRM_Core_OptionGroup::values('acl_role');
6a488035
TO
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
cf0d1c08 77 $this->addFormRule(['CRM_ACL_Form_ACLBasic', 'formRule']);
6a488035
TO
78 }
79
28518c90 80 /**
c490a46a 81 * @param array $params
28518c90
EM
82 *
83 * @return array|bool
84 */
00be9182 85 public static function formRule($params) {
6a488035 86 if ($params['entity_id'] == -1) {
cf0d1c08 87 $errors = ['entity_id' => ts('Role is a required field')];
6a488035
TO
88 return $errors;
89 }
90
91 return TRUE;
92 }
93
94 /**
d2e5d2ce 95 * Process the form submission.
6a488035
TO
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 = "
105DELETE
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";
cf0d1c08 110 $deleteParams = [1 => [$this->_id, 'Integer']];
7e1f3142 111 CRM_Core_DAO::executeQuery($query, $deleteParams);
6a488035
TO
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 }
96025800 134
6a488035 135}