ACLBasic.php 4.81 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
kurund's avatar
kurund committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
32 33 34 35 36 37 38
 * $Id$
 *
 */

/**
 *
 * @package CRM
kurund's avatar
kurund committed
39
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
40 41 42 43 44 45
 * $Id$
 *
 */
class CRM_ACL_Form_ACLBasic extends CRM_Admin_Form {

  /**
46
   * Set default values for the form.
totten's avatar
totten committed
47 48
   *
   *
colemanw's avatar
colemanw committed
49
   * @return void
totten's avatar
totten committed
50
   */
51
  public function setDefaultValues() {
totten's avatar
totten committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    $defaults = array();

    if ($this->_id ||
      $this->_id === '0'
    ) {
      $defaults['entity_id'] = $this->_id;

      $query = "
SELECT object_table
  FROM civicrm_acl
 WHERE entity_id = %1
   AND ( object_table NOT IN ( 'civicrm_saved_search', 'civicrm_uf_group', 'civicrm_custom_group' ) )
";
      $params = array(1 => array($this->_id, 'Integer'));
      $dao = CRM_Core_DAO::executeQuery($query, $params);
      $defaults['object_table'] = array();
      while ($dao->fetch()) {
        $defaults['object_table'][$dao->object_table] = 1;
      }
    }

    return $defaults;
  }

  /**
77
   * Build the form object
totten's avatar
totten committed
78
   *
colemanw's avatar
colemanw committed
79
   * @return void
totten's avatar
totten committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
   */
  public function buildQuickForm() {
    parent::buildQuickForm();

    if ($this->_action & CRM_Core_Action::DELETE) {
      return;
    }

    $permissions = array_flip(CRM_Core_Permission::basicPermissions());
    $this->addCheckBox('object_table',
      ts('ACL Type'),
      $permissions,
      NULL, NULL, TRUE, NULL,
      array('</td><td>', '</td></tr><tr><td>')
    );

    $label = ts('Role');
totten's avatar
totten committed
97
    $role = array(
pratikshad's avatar
pratikshad committed
98 99 100
      '-1' => ts('- select role -'),
      '0' => ts('Everyone'),
    ) + CRM_Core_OptionGroup::values('acl_role');
totten's avatar
totten committed
101 102 103 104 105 106 107 108 109 110
    $entityID = &$this->add('select', 'entity_id', $label, $role, TRUE);

    if ($this->_id) {
      $entityID->freeze();
    }
    $this->add('checkbox', 'is_active', ts('Enabled?'));

    $this->addFormRule(array('CRM_ACL_Form_ACLBasic', 'formRule'));
  }

111
  /**
112
   * @param array $params
113 114 115
   *
   * @return array|bool
   */
116
  public static function formRule($params) {
totten's avatar
totten committed
117 118 119 120 121 122 123 124 125
    if ($params['entity_id'] == -1) {
      $errors = array('entity_id' => ts('Role is a required field'));
      return $errors;
    }

    return TRUE;
  }

  /**
126
   * Process the form submission
totten's avatar
totten committed
127 128
   *
   *
colemanw's avatar
colemanw committed
129
   * @return void
totten's avatar
totten committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
   */
  public function postProcess() {
    CRM_ACL_BAO_Cache::resetCache();

    $params = $this->controller->exportValues($this->_name);
    if ($this->_id ||
      $this->_id === '0'
    ) {
      $query = "
DELETE
  FROM civicrm_acl
 WHERE entity_id = %1
   AND ( object_table NOT IN ( 'civicrm_saved_search', 'civicrm_uf_group', 'civicrm_custom_group' ) )
";
      $deleteParams = array(1 => array($this->_id, 'Integer'));
145
      CRM_Core_DAO::executeQuery($query, $deleteParams);
totten's avatar
totten committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

      if ($this->_action & CRM_Core_Action::DELETE) {
        CRM_Core_Session::setStatus(ts('Selected ACL has been deleted.'), ts('Record Deleted'), 'success');
        return;
      }
    }

    $params['operation'] = 'All';
    $params['deny'] = 0;
    $params['is_active'] = 1;
    $params['entity_table'] = 'civicrm_acl_role';
    $params['name'] = 'Core ACL';

    foreach ($params['object_table'] as $object_table => $value) {
      if ($value) {
        $newParams = $params;
        unset($newParams['object_table']);
        $newParams['object_table'] = $object_table;
        CRM_ACL_BAO_ACL::create($newParams);
      }
    }
  }
168

totten's avatar
totten committed
169
}