Merge pull request #18063 from civicrm/5.28
[civicrm-core.git] / CRM / Friend / Form / Event.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
18 /**
19 * This class generates form components for Tell A Friend
20 *
21 */
22 class CRM_Friend_Form_Event extends CRM_Event_Form_ManageEvent {
23
24 /**
25 * Tell a friend id in db.
26 *
27 * @var int
28 */
29 public $_friendId;
30
31 public function preProcess() {
32 parent::preProcess();
33 $this->setSelectedChild('friend');
34 }
35
36 /**
37 * Set default values for the form.
38 *
39 *
40 * @return void
41 */
42 public function setDefaultValues() {
43 $defaults = [];
44
45 if (isset($this->_id)) {
46 $defaults['entity_table'] = 'civicrm_event';
47 $defaults['entity_id'] = $this->_id;
48 CRM_Friend_BAO_Friend::getValues($defaults);
49 if (!empty($defaults['id'])) {
50 $defaults['tf_id'] = $defaults['id'] ?? NULL;
51 $this->_friendId = $defaults['tf_id'];
52 // lets unset the 'id' since it conflicts with eventID (or contribID)
53 // CRM-12667
54 unset($defaults['id']);
55 }
56 $defaults['tf_title'] = $defaults['title'] ?? NULL;
57 $defaults['tf_is_active'] = $defaults['is_active'] ?? NULL;
58 $defaults['tf_thankyou_title'] = $defaults['thankyou_title'] ?? NULL;
59 $defaults['tf_thankyou_text'] = $defaults['thankyou_text'] ?? NULL;
60 }
61
62 if (!$this->_friendId) {
63 $defaults['intro'] = ts('Help us spread the word about this event. Use the space below to personalize your email message - let your friends know why you\'re attending. Then fill in the name(s) and email address(es) and click \'Send Your Message\'.');
64 $defaults['suggested_message'] = ts('Thought you might be interested in checking out this event. I\'m planning on attending.');
65 $defaults['tf_thankyou_text'] = ts('Thanks for spreading the word about this event to your friends.');
66 $defaults['tf_title'] = ts('Tell a Friend');
67 $defaults['tf_thankyou_title'] = ts('Thanks for Spreading the Word');
68 }
69
70 return $defaults;
71 }
72
73 /**
74 * Build the form object.
75 *
76 * @return void
77 */
78 public function buildQuickForm() {
79 if (isset($this->_id)) {
80 $defaults['entity_table'] = 'civicrm_event';
81 $defaults['entity_id'] = $this->_id;
82 CRM_Friend_BAO_Friend::getValues($defaults);
83 $this->_friendId = $defaults['id'] ?? NULL;
84 }
85
86 CRM_Friend_BAO_Friend::buildFriendForm($this);
87 parent::buildQuickForm();
88 }
89
90 /**
91 * Process the form submission.
92 *
93 *
94 * @return void
95 */
96 public function postProcess() {
97 // get the submitted form values.
98 $formValues = $this->controller->exportValues($this->_name);
99
100 // let's unset event id
101 unset($formValues['id']);
102
103 $formValues['entity_table'] = 'civicrm_event';
104 $formValues['entity_id'] = $this->_id;
105 $formValues['title'] = $formValues['tf_title'];
106 $formValues['is_active'] = CRM_Utils_Array::value('tf_is_active', $formValues, FALSE);
107 $formValues['thankyou_title'] = $formValues['tf_thankyou_title'] ?? NULL;
108 $formValues['thankyou_text'] = $formValues['tf_thankyou_text'] ?? NULL;
109
110 if (($this->_action & CRM_Core_Action::UPDATE) && $this->_friendId) {
111 $formValues['id'] = $this->_friendId;
112 }
113
114 CRM_Friend_BAO_Friend::addTellAFriend($formValues);
115
116 // Update tab "disabled" css class
117 $this->ajaxResponse['tabValid'] = !empty($formValues['tf_is_active']);
118
119 parent::endPostProcess();
120 }
121
122 /**
123 * Return a descriptive name for the page, used in wizard header
124 *
125 * @return string
126 */
127 public function getTitle() {
128 return ts('Tell a Friend');
129 }
130
131 }