Merge pull request #6664 from eileenmcnaughton/comments-3
[civicrm-core.git] / CRM / Contribute / Page / Premium.php
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 */
33
34 /**
35 * Page for displaying list of Premiums.
36 */
37 class CRM_Contribute_Page_Premium extends CRM_Core_Page_Basic {
38
39 /**
40 * The action links that we need to display for the browse screen.
41 *
42 * @var array
43 */
44 static $_links = NULL;
45
46 /**
47 * Get BAO Name.
48 *
49 * @return string
50 * Classname of BAO.
51 */
52 public function getBAOName() {
53 return 'CRM_Contribute_BAO_Premium';
54 }
55
56 /**
57 * Get action Links.
58 *
59 * @return array
60 * (reference) of action links
61 */
62 public function &links() {
63 if (!(self::$_links)) {
64 // helper variable for nicer formatting
65 $deleteExtra = ts('Are you sure you want to remove this product form this page?');
66
67 self::$_links = array(
68 CRM_Core_Action::UPDATE => array(
69 'name' => ts('Edit'),
70 'url' => 'civicrm/admin/contribute/addProductToPage',
71 'qs' => 'action=update&id=%%id%%&pid=%%pid%%&reset=1',
72 'title' => ts('Edit Premium'),
73 ),
74 CRM_Core_Action::PREVIEW => array(
75 'name' => ts('Preview'),
76 'url' => 'civicrm/admin/contribute/addProductToPage',
77 'qs' => 'action=preview&id=%%id%%&pid=%%pid%%',
78 'title' => ts('Preview Premium'),
79 ),
80 CRM_Core_Action::DELETE => array(
81 'name' => ts('Remove'),
82 'url' => 'civicrm/admin/contribute/addProductToPage',
83 'qs' => 'action=delete&id=%%id%%&pid=%%pid%%',
84 'extra' => 'onclick = "if (confirm(\'' . $deleteExtra . '\') ) this.href+=\'&amp;confirmed=1\'; else return false;"',
85 'title' => ts('Disable Premium'),
86 ),
87 );
88 }
89 return self::$_links;
90 }
91
92 /**
93 * Run the page.
94 *
95 * This method is called after the page is created. It checks for the
96 * type of action and executes that action.
97 * Finally it calls the parent's run method.
98 */
99 public function run() {
100 // get the requested action
101 $action = CRM_Utils_Request::retrieve('action', 'String',
102 // default to 'browse'
103 $this, FALSE, 'browse'
104 );
105
106 // assign vars to templates
107 $this->assign('action', $action);
108 $id = CRM_Utils_Request::retrieve('id', 'Positive',
109 $this, FALSE, 0
110 );
111 $this->assign('id', $id);
112
113 $this->edit($action, $id, FALSE, FALSE);
114
115 // this is special case where we need to call browse to list premium
116 if ($action == CRM_Core_Action::UPDATE) {
117 $this->browse();
118 }
119
120 // parent run
121 return parent::run();
122 }
123
124 /**
125 * Browse function.
126 */
127 public function browse() {
128 // get all custom groups sorted by weight
129 $premiums = array();
130 $pageID = CRM_Utils_Request::retrieve('id', 'Positive',
131 $this, FALSE, 0
132 );
133 $dao = new CRM_Contribute_DAO_Premium();
134 $dao->entity_table = 'civicrm_contribution_page';
135 $dao->entity_id = $pageID;
136 $dao->find(TRUE);
137 $premiumID = $dao->id;
138 $this->assign('products', FALSE);
139 $this->assign('id', $pageID);
140 if (!$premiumID) {
141 return;
142 }
143
144 $dao = new CRM_Contribute_DAO_PremiumsProduct();
145 $dao->premiums_id = $premiumID;
146 $dao->orderBy('weight');
147 $dao->find();
148
149 while ($dao->fetch()) {
150 $productDAO = new CRM_Contribute_DAO_Product();
151 $productDAO->id = $dao->product_id;
152 $productDAO->is_active = 1;
153
154 if ($productDAO->find(TRUE)) {
155 $premiums[$productDAO->id] = array();
156 $premiums[$productDAO->id]['weight'] = $dao->weight;
157 CRM_Core_DAO::storeValues($productDAO, $premiums[$productDAO->id]);
158
159 $action = array_sum(array_keys($this->links()));
160
161 $premiums[$dao->product_id]['action'] = CRM_Core_Action::formLink(self::links(), $action,
162 array('id' => $pageID, 'pid' => $dao->id),
163 ts('more'),
164 FALSE,
165 'premium.contributionpage.row',
166 'Premium',
167 $dao->id
168 );
169 //Financial Type
170 if (!empty($dao->financial_type_id)) {
171 $premiums[$productDAO->id]['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $dao->financial_type_id, 'name');
172 }
173 }
174 }
175
176 if (count(CRM_Contribute_PseudoConstant::products($pageID)) == 0) {
177 $this->assign('products', FALSE);
178 }
179 else {
180 $this->assign('products', TRUE);
181 }
182
183 // Add order changing widget to selector
184 $returnURL = CRM_Utils_System::url('civicrm/admin/contribute/premium', "reset=1&action=update&id={$pageID}");
185 $filter = "premiums_id = {$premiumID}";
186 CRM_Utils_Weight::addOrder($premiums, 'CRM_Contribute_DAO_PremiumsProduct',
187 'id', $returnURL, $filter
188 );
189 $this->assign('rows', $premiums);
190 }
191
192 /**
193 * Get name of edit form.
194 *
195 * @return string
196 * Classname of edit form.
197 */
198 public function editForm() {
199 return 'CRM_Contribute_Form_ContributionPage_Premium';
200 }
201
202 /**
203 * Get edit form name.
204 *
205 * @return string
206 * name of this page.
207 */
208 public function editName() {
209 return 'Configure Premiums';
210 }
211
212 /**
213 * Get user context.
214 *
215 * @param null $mode
216 *
217 * @return string
218 * user context.
219 */
220 public function userContext($mode = NULL) {
221 return CRM_Utils_System::currentPath();
222 }
223
224 }