| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for previewing custom data |
| 38 | * |
| 39 | * It delegates the work to lower level subclasses and integrates the changes |
| 40 | * back in. It also uses a lot of functionality with the CRM API's, so any change |
| 41 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. |
| 42 | * |
| 43 | */ |
| 44 | class CRM_Price_Form_Preview extends CRM_Core_Form { |
| 45 | |
| 46 | /** |
| 47 | * the group tree data |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | protected $_groupTree; |
| 52 | |
| 53 | /** |
| 54 | * pre processing work done here. |
| 55 | * |
| 56 | * gets session variables for group or field id |
| 57 | * |
| 58 | * @param null |
| 59 | * |
| 60 | * @return void |
| 61 | * @access public |
| 62 | */ function preProcess() { |
| 63 | // get the controller vars |
| 64 | $groupId = $this->get('groupId'); |
| 65 | $fieldId = $this->get('fieldId'); |
| 66 | |
| 67 | if ($fieldId) { |
| 68 | $groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId); |
| 69 | $this->_groupTree[$groupId]['fields'][$fieldId] = $groupTree[$groupId]['fields'][$fieldId]; |
| 70 | $this->assign('preview_type', 'field'); |
| 71 | $url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$groupId}"); |
| 72 | $breadCrumb = array(array('title' => ts('Price Set Fields'), |
| 73 | 'url' => $url, |
| 74 | )); |
| 75 | } |
| 76 | else { |
| 77 | // group preview |
| 78 | $this->_groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId); |
| 79 | $this->assign('preview_type', 'group'); |
| 80 | $this->assign('setTitle', CRM_Price_BAO_PriceSet::getTitle($groupId)); |
| 81 | $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1'); |
| 82 | $breadCrumb = array(array('title' => ts('Price Sets'), |
| 83 | 'url' => $url, |
| 84 | )); |
| 85 | } |
| 86 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Set the default form values |
| 91 | * |
| 92 | * @param null |
| 93 | * |
| 94 | * @return array the default array reference |
| 95 | * @access protected |
| 96 | */ |
| 97 | function setDefaultValues() { |
| 98 | $defaults = array(); |
| 99 | $groupId = $this->get('groupId'); |
| 100 | $fieldId = $this->get('fieldId'); |
| 101 | if (!empty($this->_groupTree[$groupId]['fields'])) { |
| 102 | foreach ($this->_groupTree[$groupId]['fields'] as $key => $val) { |
| 103 | foreach ($val['options'] as $keys => $values) { |
| 104 | if ($values['is_default']) { |
| 105 | if ($val['html_type'] == 'CheckBox') { |
| 106 | $defaults["price_{$key}"][$keys] = 1; |
| 107 | } |
| 108 | else { |
| 109 | $defaults["price_{$key}"] = $keys; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return $defaults; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Build the form object |
| 120 | * |
| 121 | * @param null |
| 122 | * |
| 123 | * @return void |
| 124 | * @access public |
| 125 | */ |
| 126 | public function buildQuickForm() { |
| 127 | $this->assign('groupTree', $this->_groupTree); |
| 128 | |
| 129 | // add the form elements |
| 130 | |
| 131 | foreach ($this->_groupTree as $group) { |
| 132 | if (is_array($group['fields']) && !empty($group['fields'])) { |
| 133 | foreach ($group['fields'] as $field) { |
| 134 | $fieldId = $field['id']; |
| 135 | $elementName = 'price_' . $fieldId; |
| 136 | CRM_Price_BAO_PriceField::addQuickFormElement($this, $elementName, $fieldId, FALSE, $field['is_required']); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $this->addButtons(array( |
| 142 | array( |
| 143 | 'type' => 'cancel', |
| 144 | 'name' => ts('Done with Preview'), |
| 145 | 'isDefault' => TRUE, |
| 146 | ), |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | |