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