Merge pull request #16884 from civicrm/5.24
[civicrm-core.git] / CRM / Contribute / BAO / Product.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 class CRM_Contribute_BAO_Product extends CRM_Contribute_DAO_Product {
18
19 /**
20 * Static holder for the default LT.
21 * @var int
22 */
23 public static $_defaultContributionType = NULL;
24
25 /**
26 * Class constructor.
27 */
28 public function __construct() {
29 parent::__construct();
30 }
31
32 /**
33 * Fetch object based on array of properties.
34 *
35 * @param array $params
36 * (reference ) an assoc array of name/value pairs.
37 * @param array $defaults
38 * (reference ) an assoc array to hold the flattened values.
39 *
40 * @return CRM_Contribute_BAO_Product
41 */
42 public static function retrieve(&$params, &$defaults) {
43 $premium = new CRM_Contribute_DAO_Product();
44 $premium->copyValues($params);
45 if ($premium->find(TRUE)) {
46 $premium->product_name = $premium->name;
47 CRM_Core_DAO::storeValues($premium, $defaults);
48 return $premium;
49 }
50 return NULL;
51 }
52
53 /**
54 * Update the is_active flag in the db.
55 *
56 * @param int $id
57 * Id of the database record.
58 * @param bool $is_active
59 * Value we want to set the is_active field.
60 *
61 * @return bool
62 */
63 public static function setIsActive($id, $is_active) {
64 if (!$is_active) {
65 $dao = new CRM_Contribute_DAO_PremiumsProduct();
66 $dao->product_id = $id;
67 $dao->delete();
68 }
69 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Product', $id, 'is_active', $is_active);
70 }
71
72 /**
73 * Add a premium product to the database, and return it.
74 *
75 * @param array $params
76 * Update parameters.
77 *
78 * @return CRM_Contribute_DAO_Product
79 */
80 public static function create($params) {
81 $id = $params['id'] ?? NULL;
82 if (empty($id)) {
83 $defaultParams = [
84 'id' => $id,
85 'image' => '',
86 'thumbnail' => '',
87 'is_active' => 0,
88 'is_deductible' => FALSE,
89 'currency' => CRM_Core_Config::singleton()->defaultCurrency,
90 ];
91 $params = array_merge($defaultParams, $params);
92 }
93
94 // Modify the submitted values for 'image' and 'thumbnail' so that we use
95 // local URLs for these images when possible.
96 if (isset($params['image'])) {
97 $params['image'] = CRM_Utils_String::simplifyURL($params['image'], TRUE);
98 }
99 if (isset($params['thumbnail'])) {
100 $params['thumbnail'] = CRM_Utils_String::simplifyURL($params['thumbnail'], TRUE);
101 }
102
103 // Save and return
104 $premium = new CRM_Contribute_DAO_Product();
105 $premium->copyValues($params);
106 $premium->save();
107 return $premium;
108 }
109
110 /**
111 * Delete premium Types.
112 *
113 * @param int $productID
114 *
115 * @throws \CRM_Core_Exception
116 */
117 public static function del($productID) {
118 //check dependencies
119 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
120 $premiumsProduct->product_id = $productID;
121 if ($premiumsProduct->find(TRUE)) {
122 throw new CRM_Core_Exception('Cannot delete a Premium that is linked to a Contribution page');
123 }
124 // delete product
125 $premium = new CRM_Contribute_DAO_Product();
126 $premium->id = $productID;
127 $premium->delete();
128 }
129
130 }