Merge pull request #17221 from civicrm/5.25
[civicrm-core.git] / CRM / Contribute / Page / Premium.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
347e061b 19 * Page for displaying list of Premiums.
6a488035
TO
20 */
21class CRM_Contribute_Page_Premium extends CRM_Core_Page_Basic {
22
23 /**
fe482240 24 * The action links that we need to display for the browse screen.
6a488035
TO
25 *
26 * @var array
6a488035 27 */
683bf891 28 public static $_links = NULL;
6a488035
TO
29
30 /**
fe482240 31 * Get BAO Name.
6a488035 32 *
a6c01b45
CW
33 * @return string
34 * Classname of BAO.
6a488035 35 */
00be9182 36 public function getBAOName() {
6a488035
TO
37 return 'CRM_Contribute_BAO_Premium';
38 }
39
40 /**
fe482240 41 * Get action Links.
6a488035 42 *
a6c01b45
CW
43 * @return array
44 * (reference) of action links
6a488035 45 */
00be9182 46 public function &links() {
6a488035
TO
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
be2fb01f
CW
51 self::$_links = [
52 CRM_Core_Action::UPDATE => [
6a488035
TO
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'),
be2fb01f
CW
57 ],
58 CRM_Core_Action::PREVIEW => [
6a488035
TO
59 'name' => ts('Preview'),
60 'url' => 'civicrm/admin/contribute/addProductToPage',
61 'qs' => 'action=preview&id=%%id%%&pid=%%pid%%',
62 'title' => ts('Preview Premium'),
be2fb01f
CW
63 ],
64 CRM_Core_Action::DELETE => [
6a488035
TO
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'),
be2fb01f
CW
70 ],
71 ];
6a488035
TO
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.
6a488035 82 */
00be9182 83 public function run() {
6a488035
TO
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 /**
347e061b 109 * Browse function.
6a488035 110 */
00be9182 111 public function browse() {
6a488035 112 // get all custom groups sorted by weight
be2fb01f 113 $premiums = [];
6a488035
TO
114 $pageID = CRM_Utils_Request::retrieve('id', 'Positive',
115 $this, FALSE, 0
116 );
da6b7c62
MW
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;
6a488035
TO
122 $this->assign('products', FALSE);
123 $this->assign('id', $pageID);
124 if (!$premiumID) {
125 return;
126 }
127
da6b7c62
MW
128 $premiumsProductDao = new CRM_Contribute_DAO_PremiumsProduct();
129 $premiumsProductDao->premiums_id = $premiumID;
130 $premiumsProductDao->orderBy('weight');
131 $premiumsProductDao->find();
6a488035 132
da6b7c62 133 while ($premiumsProductDao->fetch()) {
6a488035 134 $productDAO = new CRM_Contribute_DAO_Product();
da6b7c62 135 $productDAO->id = $premiumsProductDao->product_id;
6a488035
TO
136 $productDAO->is_active = 1;
137
138 if ($productDAO->find(TRUE)) {
be2fb01f 139 $premiums[$productDAO->id] = [];
da6b7c62 140 $premiums[$productDAO->id]['weight'] = $premiumsProductDao->weight;
6a488035
TO
141 CRM_Core_DAO::storeValues($productDAO, $premiums[$productDAO->id]);
142
143 $action = array_sum(array_keys($this->links()));
144
da6b7c62 145 $premiums[$premiumsProductDao->product_id]['action'] = CRM_Core_Action::formLink(self::links(), $action,
be2fb01f 146 ['id' => $pageID, 'pid' => $premiumsProductDao->id],
87dab4a4
AH
147 ts('more'),
148 FALSE,
149 'premium.contributionpage.row',
150 'Premium',
da6b7c62 151 $premiumsProductDao->id
6a488035 152 );
da6b7c62
MW
153 // Financial Type
154 if (!empty($premiumsProductDao->financial_type_id)) {
906c7498 155 $premiums[$productDAO->id]['financial_type'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Product', 'financial_type_id', $premiumsProductDao->financial_type_id);
6a488035
TO
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 /**
fe482240 177 * Get name of edit form.
6a488035 178 *
a6c01b45
CW
179 * @return string
180 * Classname of edit form.
6a488035 181 */
00be9182 182 public function editForm() {
6a488035
TO
183 return 'CRM_Contribute_Form_ContributionPage_Premium';
184 }
185
186 /**
fe482240 187 * Get edit form name.
6a488035 188 *
a6c01b45
CW
189 * @return string
190 * name of this page.
6a488035 191 */
00be9182 192 public function editName() {
6a488035
TO
193 return 'Configure Premiums';
194 }
195
196 /**
197 * Get user context.
198 *
dd244018
EM
199 * @param null $mode
200 *
a6c01b45
CW
201 * @return string
202 * user context.
6a488035 203 */
00be9182 204 public function userContext($mode = NULL) {
6a488035
TO
205 return CRM_Utils_System::currentPath();
206 }
96025800 207
6a488035 208}