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