| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Create a page for displaying Custom Options. |
| 38 | * |
| 39 | * Heart of this class is the run method which checks |
| 40 | * for action type and then displays the appropriate |
| 41 | * page. |
| 42 | * |
| 43 | */ |
| 44 | class CRM_Custom_Page_Option extends CRM_Core_Page { |
| 45 | |
| 46 | public $useLivePageJS = TRUE; |
| 47 | |
| 48 | /** |
| 49 | * The Group id of the option |
| 50 | * |
| 51 | * @var int |
| 52 | */ |
| 53 | protected $_gid; |
| 54 | |
| 55 | /** |
| 56 | * The field id of the option |
| 57 | * |
| 58 | * @var int |
| 59 | */ |
| 60 | protected $_fid; |
| 61 | |
| 62 | /** |
| 63 | * The action links that we need to display for the browse screen |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | private static $_actionLinks; |
| 68 | |
| 69 | /** |
| 70 | * Get the action links for this page. |
| 71 | * |
| 72 | * @return array |
| 73 | * array of action links that we need to display for the browse screen |
| 74 | */ |
| 75 | public function &actionLinks() { |
| 76 | if (!isset(self::$_actionLinks)) { |
| 77 | self::$_actionLinks = array( |
| 78 | CRM_Core_Action::UPDATE => array( |
| 79 | 'name' => ts('Edit Option'), |
| 80 | 'url' => 'civicrm/admin/custom/group/field/option', |
| 81 | 'qs' => 'reset=1&action=update&id=%%id%%&fid=%%fid%%&gid=%%gid%%', |
| 82 | 'title' => ts('Edit Multiple Choice Option'), |
| 83 | ), |
| 84 | CRM_Core_Action::VIEW => array( |
| 85 | 'name' => ts('View'), |
| 86 | 'url' => 'civicrm/admin/custom/group/field/option', |
| 87 | 'qs' => 'action=view&id=%%id%%&fid=%%fid%%', |
| 88 | 'title' => ts('View Multiple Choice Option'), |
| 89 | ), |
| 90 | CRM_Core_Action::DISABLE => array( |
| 91 | 'name' => ts('Disable'), |
| 92 | 'ref' => 'crm-enable-disable', |
| 93 | 'title' => ts('Disable Mutliple Choice Option'), |
| 94 | ), |
| 95 | CRM_Core_Action::ENABLE => array( |
| 96 | 'name' => ts('Enable'), |
| 97 | 'ref' => 'crm-enable-disable', |
| 98 | 'title' => ts('Enable Mutliple Choice Option'), |
| 99 | ), |
| 100 | CRM_Core_Action::DELETE => array( |
| 101 | 'name' => ts('Delete'), |
| 102 | 'url' => 'civicrm/admin/custom/group/field/option', |
| 103 | 'qs' => 'action=delete&id=%%id%%&fid=%%fid%%', |
| 104 | 'title' => ts('Disable Multiple Choice Option'), |
| 105 | ), |
| 106 | ); |
| 107 | } |
| 108 | return self::$_actionLinks; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Browse all custom group fields. |
| 113 | * |
| 114 | * @return void |
| 115 | */ |
| 116 | public function browse() { |
| 117 | |
| 118 | // get the option group id |
| 119 | $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', |
| 120 | $this->_fid, |
| 121 | 'option_group_id' |
| 122 | ); |
| 123 | |
| 124 | $query = " |
| 125 | SELECT id, label |
| 126 | FROM civicrm_custom_field |
| 127 | WHERE option_group_id = %1"; |
| 128 | $params = array( |
| 129 | 1 => array($optionGroupID, 'Integer'), |
| 130 | 2 => array($this->_fid, 'Integer'), |
| 131 | ); |
| 132 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 133 | $reusedNames = array(); |
| 134 | if ($dao->N > 1) { |
| 135 | while ($dao->fetch()) { |
| 136 | $reusedNames[] = $dao->label; |
| 137 | } |
| 138 | $reusedNames = implode(', ', $reusedNames); |
| 139 | $newTitle = ts('%1 - Multiple Choice Options', |
| 140 | array(1 => $reusedNames) |
| 141 | ); |
| 142 | CRM_Utils_System::setTitle($newTitle); |
| 143 | $this->assign('reusedNames', $reusedNames); |
| 144 | } |
| 145 | $this->assign('optionGroupID', $optionGroupID); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Edit custom Option. |
| 150 | * |
| 151 | * editing would involved modifying existing fields + adding data to new fields. |
| 152 | * |
| 153 | * @param string $action |
| 154 | * The action to be invoked. |
| 155 | * |
| 156 | * @return void |
| 157 | */ |
| 158 | public function edit($action) { |
| 159 | // create a simple controller for editing custom data |
| 160 | $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', ts('Custom Option'), $action); |
| 161 | |
| 162 | // set the userContext stack |
| 163 | $session = CRM_Core_Session::singleton(); |
| 164 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field/option', |
| 165 | "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}" |
| 166 | )); |
| 167 | $controller->setEmbedded(TRUE); |
| 168 | $controller->process(); |
| 169 | $controller->run(); |
| 170 | $this->browse(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Run the page. |
| 175 | * |
| 176 | * This method is called after the page is created. It checks for the |
| 177 | * type of action and executes that action. |
| 178 | * |
| 179 | * @return void |
| 180 | */ |
| 181 | public function run() { |
| 182 | |
| 183 | // get the field id |
| 184 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', |
| 185 | $this, FALSE, 0 |
| 186 | ); |
| 187 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', |
| 188 | $this, FALSE, 0 |
| 189 | ); |
| 190 | |
| 191 | if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) { |
| 192 | CRM_Core_Error::fatal("You cannot add or edit muliple choice options in a reserved custom field-set."); |
| 193 | } |
| 194 | |
| 195 | //as url contain $gid so append breadcrumb dynamically. |
| 196 | $breadcrumb = array( |
| 197 | array( |
| 198 | 'title' => ts('Custom Data Fields'), |
| 199 | 'url' => CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid), |
| 200 | ), |
| 201 | ); |
| 202 | CRM_Utils_System::appendBreadCrumb($breadcrumb); |
| 203 | |
| 204 | if ($this->_fid) { |
| 205 | $fieldTitle = CRM_Core_BAO_CustomField::getTitle($this->_fid); |
| 206 | $this->assign('fid', $this->_fid); |
| 207 | $this->assign('gid', $this->_gid); |
| 208 | $this->assign('fieldTitle', $fieldTitle); |
| 209 | CRM_Utils_System::setTitle(ts('%1 - Multiple Choice Options', array(1 => $fieldTitle))); |
| 210 | } |
| 211 | |
| 212 | // get the requested action |
| 213 | $action = CRM_Utils_Request::retrieve('action', 'String', |
| 214 | // default to 'browse' |
| 215 | $this, FALSE, 'browse' |
| 216 | ); |
| 217 | |
| 218 | // assign vars to templates |
| 219 | $this->assign('action', $action); |
| 220 | |
| 221 | $id = CRM_Utils_Request::retrieve('id', 'Positive', |
| 222 | $this, FALSE, 0 |
| 223 | ); |
| 224 | |
| 225 | // what action to take ? |
| 226 | if (($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | |
| 227 | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE |
| 228 | ) |
| 229 | ) || |
| 230 | !empty($_POST) |
| 231 | ) { |
| 232 | // no browse for edit/update/view |
| 233 | $this->edit($action); |
| 234 | } |
| 235 | else { |
| 236 | $this->browse(); |
| 237 | } |
| 238 | // Call the parents run method |
| 239 | return parent::run(); |
| 240 | } |
| 241 | |
| 242 | } |