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