Commit | Line | Data |
---|---|---|
4825de4a CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
4825de4a | 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 | | |
4825de4a CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
4825de4a CW |
16 | */ |
17 | class CRM_Mailing_BAO_MailingComponent extends CRM_Mailing_DAO_MailingComponent { | |
18 | ||
19 | /** | |
4f940304 | 20 | * Retrieve DB object and copy to defaults array. |
4825de4a CW |
21 | * |
22 | * @param array $params | |
4f940304 | 23 | * Array of criteria values. |
4825de4a | 24 | * @param array $defaults |
4f940304 CW |
25 | * Array to be populated with found values. |
26 | * | |
27 | * @return self|null | |
28 | * The DAO object, if found. | |
4825de4a | 29 | * |
4f940304 | 30 | * @deprecated |
4825de4a | 31 | */ |
4f940304 CW |
32 | public static function retrieve($params, &$defaults) { |
33 | return self::commonRetrieve(self::class, $params, $defaults); | |
4825de4a CW |
34 | } |
35 | ||
36 | /** | |
37 | * Update the is_active flag in the db. | |
38 | * | |
39 | * @param int $id | |
40 | * Id of the database record. | |
41 | * @param bool $is_active | |
42 | * Value we want to set the is_active field. | |
43 | * | |
44 | * @return bool | |
45 | * true if we found and updated the object, else false | |
46 | */ | |
47 | public static function setIsActive($id, $is_active) { | |
48 | return CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_MailingComponent', $id, 'is_active', $is_active); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Create and Update mailing component. | |
53 | * | |
54 | * @param array $params | |
55 | * (reference ) an assoc array of name/value pairs. | |
56 | * @param array $ids | |
57 | * (deprecated) the array that holds all the db ids. | |
58 | * | |
59 | * @return CRM_Mailing_BAO_MailingComponent | |
60 | */ | |
be2fb01f | 61 | public static function add(&$params, $ids = []) { |
8df1a020 | 62 | $id = $params['id'] ?? $ids['id'] ?? NULL; |
4825de4a CW |
63 | $component = new CRM_Mailing_BAO_MailingComponent(); |
64 | if ($id) { | |
65 | $component->id = $id; | |
66 | $component->find(TRUE); | |
67 | } | |
68 | ||
69 | $component->copyValues($params); | |
70 | if (empty($id) && empty($params['body_text'])) { | |
71 | $component->body_text = CRM_Utils_String::htmlToText(CRM_Utils_Array::value('body_html', $params)); | |
72 | } | |
73 | ||
74 | if ($component->is_default) { | |
75 | if (!empty($id)) { | |
76 | $sql = 'UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type = %1 AND id <> %2'; | |
be2fb01f CW |
77 | $sqlParams = [ |
78 | 1 => [$component->component_type, 'String'], | |
79 | 2 => [$id, 'Positive'], | |
80 | ]; | |
4825de4a CW |
81 | } |
82 | else { | |
83 | $sql = 'UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type = %1'; | |
be2fb01f CW |
84 | $sqlParams = [ |
85 | 1 => [$component->component_type, 'String'], | |
86 | ]; | |
4825de4a CW |
87 | } |
88 | CRM_Core_DAO::executeQuery($sql, $sqlParams); | |
89 | } | |
90 | ||
91 | $component->save(); | |
92 | return $component; | |
93 | } | |
94 | ||
95 | } |