| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
| 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 https://civicrm.org/licensing |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Create a page for displaying Price Sets. |
| 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_Price_Page_Set extends CRM_Core_Page { |
| 45 | |
| 46 | /** |
| 47 | * The action links that we need to display for the browse screen. |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | private static $_actionLinks; |
| 52 | |
| 53 | /** |
| 54 | * Get the action links for this page. |
| 55 | * |
| 56 | * @return array |
| 57 | * array of action links that we need to display for the browse screen |
| 58 | */ |
| 59 | public function &actionLinks() { |
| 60 | // check if variable _actionsLinks is populated |
| 61 | if (!isset(self::$_actionLinks)) { |
| 62 | // helper variable for nicer formatting |
| 63 | $deleteExtra = ts('Are you sure you want to delete this price set?'); |
| 64 | $copyExtra = ts('Are you sure you want to make a copy of this price set?'); |
| 65 | self::$_actionLinks = [ |
| 66 | CRM_Core_Action::BROWSE => [ |
| 67 | 'name' => ts('View and Edit Price Fields'), |
| 68 | 'url' => 'civicrm/admin/price/field', |
| 69 | 'qs' => 'reset=1&action=browse&sid=%%sid%%', |
| 70 | 'title' => ts('View and Edit Price Fields'), |
| 71 | ], |
| 72 | CRM_Core_Action::PREVIEW => [ |
| 73 | 'name' => ts('Preview'), |
| 74 | 'url' => 'civicrm/admin/price', |
| 75 | 'qs' => 'action=preview&reset=1&sid=%%sid%%', |
| 76 | 'title' => ts('Preview Price Set'), |
| 77 | ], |
| 78 | CRM_Core_Action::UPDATE => [ |
| 79 | 'name' => ts('Settings'), |
| 80 | 'url' => 'civicrm/admin/price', |
| 81 | 'qs' => 'action=update&reset=1&sid=%%sid%%', |
| 82 | 'title' => ts('Edit Price Set'), |
| 83 | ], |
| 84 | CRM_Core_Action::DISABLE => [ |
| 85 | 'name' => ts('Disable'), |
| 86 | 'ref' => 'crm-enable-disable', |
| 87 | 'title' => ts('Disable Price Set'), |
| 88 | ], |
| 89 | CRM_Core_Action::ENABLE => [ |
| 90 | 'name' => ts('Enable'), |
| 91 | 'ref' => 'crm-enable-disable', |
| 92 | 'title' => ts('Enable Price Set'), |
| 93 | ], |
| 94 | CRM_Core_Action::DELETE => [ |
| 95 | 'name' => ts('Delete'), |
| 96 | 'url' => 'civicrm/admin/price', |
| 97 | 'qs' => 'action=delete&reset=1&sid=%%sid%%', |
| 98 | 'title' => ts('Delete Price Set'), |
| 99 | 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"', |
| 100 | ], |
| 101 | CRM_Core_Action::COPY => [ |
| 102 | 'name' => ts('Copy Price Set'), |
| 103 | 'url' => CRM_Utils_System::currentPath(), |
| 104 | 'qs' => 'action=copy&sid=%%sid%%', |
| 105 | 'title' => ts('Make a Copy of Price Set'), |
| 106 | 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"', |
| 107 | ], |
| 108 | ]; |
| 109 | } |
| 110 | return self::$_actionLinks; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Run the page. |
| 115 | * |
| 116 | * This method is called after the page is created. It checks for the |
| 117 | * type of action and executes that action. |
| 118 | * Finally it calls the parent's run method. |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function run() { |
| 123 | // get the requested action |
| 124 | $action = CRM_Utils_Request::retrieve('action', 'String', |
| 125 | // default to 'browse' |
| 126 | $this, FALSE, 'browse' |
| 127 | ); |
| 128 | |
| 129 | // assign vars to templates |
| 130 | $this->assign('action', $action); |
| 131 | $sid = CRM_Utils_Request::retrieve('sid', 'Positive', |
| 132 | $this, FALSE, 0 |
| 133 | ); |
| 134 | |
| 135 | if ($sid) { |
| 136 | CRM_Price_BAO_PriceSet::checkPermission($sid); |
| 137 | } |
| 138 | // what action to take ? |
| 139 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { |
| 140 | $this->edit($sid, $action); |
| 141 | } |
| 142 | elseif ($action & CRM_Core_Action::PREVIEW) { |
| 143 | $this->preview($sid); |
| 144 | } |
| 145 | elseif ($action & CRM_Core_Action::COPY) { |
| 146 | CRM_Core_Session::setStatus(ts('A copy of the price set has been created'), ts('Saved'), 'success'); |
| 147 | $this->copy(); |
| 148 | } |
| 149 | else { |
| 150 | |
| 151 | // if action is delete do the needful. |
| 152 | if ($action & (CRM_Core_Action::DELETE)) { |
| 153 | $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($sid); |
| 154 | |
| 155 | if (empty($usedBy)) { |
| 156 | // prompt to delete |
| 157 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse')); |
| 158 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_DeleteSet', 'Delete Price Set', NULL); |
| 159 | $controller->set('sid', $sid); |
| 160 | $controller->setEmbedded(TRUE); |
| 161 | $controller->process(); |
| 162 | $controller->run(); |
| 163 | } |
| 164 | else { |
| 165 | // add breadcrumb |
| 166 | $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1'); |
| 167 | CRM_Utils_System::appendBreadCrumb(ts('Price Sets'), $url); |
| 168 | $this->assign('usedPriceSetTitle', CRM_Price_BAO_PriceSet::getTitle($sid)); |
| 169 | $this->assign('usedBy', $usedBy); |
| 170 | |
| 171 | $comps = [ |
| 172 | 'Event' => 'civicrm_event', |
| 173 | 'Contribution' => 'civicrm_contribution_page', |
| 174 | 'EventTemplate' => 'civicrm_event_template', |
| 175 | ]; |
| 176 | $priceSetContexts = []; |
| 177 | foreach ($comps as $name => $table) { |
| 178 | if (array_key_exists($table, $usedBy)) { |
| 179 | $priceSetContexts[] = $name; |
| 180 | } |
| 181 | } |
| 182 | $this->assign('contexts', $priceSetContexts); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // finally browse the price sets |
| 187 | $this->browse(); |
| 188 | } |
| 189 | // parent run |
| 190 | return parent::run(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Edit price set. |
| 195 | * |
| 196 | * @param int $sid |
| 197 | * Price set id. |
| 198 | * @param string $action |
| 199 | * The action to be invoked. |
| 200 | * |
| 201 | * @return void |
| 202 | */ |
| 203 | public function edit($sid, $action) { |
| 204 | // create a simple controller for editing price sets |
| 205 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Set', ts('Price Set'), $action); |
| 206 | |
| 207 | // set the userContext stack |
| 208 | $session = CRM_Core_Session::singleton(); |
| 209 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse')); |
| 210 | $controller->set('sid', $sid); |
| 211 | $controller->setEmbedded(TRUE); |
| 212 | $controller->process(); |
| 213 | $controller->run(); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Preview price set. |
| 218 | * |
| 219 | * @param int $sid |
| 220 | * Price set id. |
| 221 | * |
| 222 | * @return void |
| 223 | */ |
| 224 | public function preview($sid) { |
| 225 | $controller = new CRM_Core_Controller_Simple('CRM_Price_Form_Preview', ts('Preview Price Set'), NULL); |
| 226 | $session = CRM_Core_Session::singleton(); |
| 227 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
| 228 | if ($context == 'field') { |
| 229 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price/field', "action=browse&sid={$sid}")); |
| 230 | } |
| 231 | else { |
| 232 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/price', 'action=browse')); |
| 233 | } |
| 234 | $controller->set('groupId', $sid); |
| 235 | $controller->setEmbedded(TRUE); |
| 236 | $controller->process(); |
| 237 | $controller->run(); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Browse all price sets. |
| 242 | * |
| 243 | * @param string $action |
| 244 | * The action to be invoked. |
| 245 | * |
| 246 | * @return void |
| 247 | */ |
| 248 | public function browse($action = NULL) { |
| 249 | // get all price sets |
| 250 | $priceSet = []; |
| 251 | $comps = [ |
| 252 | 'CiviEvent' => ts('Event'), |
| 253 | 'CiviContribute' => ts('Contribution'), |
| 254 | 'CiviMember' => ts('Membership'), |
| 255 | ]; |
| 256 | |
| 257 | $dao = new CRM_Price_DAO_PriceSet(); |
| 258 | if (CRM_Price_BAO_PriceSet::eventPriceSetDomainID()) { |
| 259 | $dao->domain_id = CRM_Core_Config::domainID(); |
| 260 | } |
| 261 | $dao->is_quick_config = 0; |
| 262 | $dao->find(); |
| 263 | while ($dao->fetch()) { |
| 264 | $priceSet[$dao->id] = []; |
| 265 | CRM_Core_DAO::storeValues($dao, $priceSet[$dao->id]); |
| 266 | |
| 267 | $compIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 268 | CRM_Utils_Array::value('extends', $priceSet[$dao->id]) |
| 269 | ); |
| 270 | $extends = []; |
| 271 | //CRM-10225 |
| 272 | foreach ($compIds as $compId) { |
| 273 | if (!empty($comps[CRM_Core_Component::getComponentName($compId)])) { |
| 274 | $extends[] = $comps[CRM_Core_Component::getComponentName($compId)]; |
| 275 | } |
| 276 | } |
| 277 | $priceSet[$dao->id]['extends'] = implode(', ', $extends); |
| 278 | |
| 279 | // form all action links |
| 280 | $action = array_sum(array_keys($this->actionLinks())); |
| 281 | |
| 282 | // update enable/disable links depending on price_set properties. |
| 283 | if ($dao->is_reserved) { |
| 284 | $action -= CRM_Core_Action::UPDATE + CRM_Core_Action::DISABLE + CRM_Core_Action::ENABLE + CRM_Core_Action::DELETE + CRM_Core_Action::COPY; |
| 285 | } |
| 286 | else { |
| 287 | if ($dao->is_active) { |
| 288 | $action -= CRM_Core_Action::ENABLE; |
| 289 | } |
| 290 | else { |
| 291 | $action -= CRM_Core_Action::DISABLE; |
| 292 | } |
| 293 | } |
| 294 | $actionLinks = self::actionLinks(); |
| 295 | //CRM-10117 |
| 296 | if ($dao->is_reserved) { |
| 297 | $actionLinks[CRM_Core_Action::BROWSE]['name'] = ts('View Price Fields'); |
| 298 | } |
| 299 | $priceSet[$dao->id]['action'] = CRM_Core_Action::formLink($actionLinks, $action, |
| 300 | ['sid' => $dao->id], |
| 301 | ts('more'), |
| 302 | FALSE, |
| 303 | 'priceSet.row.actions', |
| 304 | 'PriceSet', |
| 305 | $dao->id |
| 306 | ); |
| 307 | } |
| 308 | $this->assign('rows', $priceSet); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * make a copy of a price set, including |
| 313 | * all the fields in the page |
| 314 | * |
| 315 | * @return void |
| 316 | */ |
| 317 | public function copy() { |
| 318 | $id = CRM_Utils_Request::retrieve('sid', 'Positive', |
| 319 | $this, TRUE, 0, 'GET' |
| 320 | ); |
| 321 | |
| 322 | CRM_Price_BAO_PriceSet::copy($id); |
| 323 | |
| 324 | CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1')); |
| 325 | } |
| 326 | |
| 327 | } |