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