Merge pull request #21883 from mariav0/patch-21
[civicrm-core.git] / CRM / Contribute / BAO / Product.php
CommitLineData
37828d4f
MW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
37828d4f 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 |
37828d4f
MW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
37828d4f
MW
16 */
17class CRM_Contribute_BAO_Product extends CRM_Contribute_DAO_Product {
18
19 /**
20 * Static holder for the default LT.
1330f57a 21 * @var int
37828d4f 22 */
1330f57a 23 public static $_defaultContributionType = NULL;
37828d4f
MW
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
32f27499 76 * Update parameters.
37828d4f
MW
77 *
78 * @return CRM_Contribute_DAO_Product
79 */
32f27499 80 public static function create($params) {
9c1bc317 81 $id = $params['id'] ?? NULL;
7e49aa93 82 $op = !empty($id) ? 'edit' : 'create';
048fa8d6
MW
83 if (empty($id)) {
84 $defaultParams = [
85 'id' => $id,
86 'image' => '',
87 'thumbnail' => '',
88 'is_active' => 0,
89 'is_deductible' => FALSE,
90 'currency' => CRM_Core_Config::singleton()->defaultCurrency,
91 ];
92 $params = array_merge($defaultParams, $params);
93 }
7e49aa93 94 CRM_Utils_Hook::pre($op, 'Product', $id, $params);
37828d4f
MW
95 // Modify the submitted values for 'image' and 'thumbnail' so that we use
96 // local URLs for these images when possible.
048fa8d6
MW
97 if (isset($params['image'])) {
98 $params['image'] = CRM_Utils_String::simplifyURL($params['image'], TRUE);
99 }
100 if (isset($params['thumbnail'])) {
101 $params['thumbnail'] = CRM_Utils_String::simplifyURL($params['thumbnail'], TRUE);
102 }
37828d4f
MW
103
104 // Save and return
105 $premium = new CRM_Contribute_DAO_Product();
106 $premium->copyValues($params);
107 $premium->save();
7e49aa93 108 CRM_Utils_Hook::post($op, 'Product', $id, $premium);
37828d4f
MW
109 return $premium;
110 }
111
112 /**
113 * Delete premium Types.
114 *
115 * @param int $productID
1d4cebf3 116 * @deprecated
37828d4f
MW
117 * @throws \CRM_Core_Exception
118 */
119 public static function del($productID) {
1d4cebf3 120 static::deleteRecord(['id' => $productID]);
37828d4f
MW
121 }
122
123}