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