Merge pull request #13613 from colemanw/openCampaignWidget
[civicrm-core.git] / CRM / Note / Form / Note.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 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components generic to note
38 *
39 * It delegates the work to lower level subclasses and integrates the changes
40 * back in. It also uses a lot of functionality with the CRM API's, so any change
41 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
42 *
43 */
44 class CRM_Note_Form_Note extends CRM_Core_Form {
45
46 /**
47 * The table name, used when editing/creating a note
48 *
49 * @var string
50 */
51 protected $_entityTable;
52
53 /**
54 * The table id, used when editing/creating a note
55 *
56 * @var int
57 */
58 protected $_entityId;
59
60 /**
61 * The note id, used when editing the note
62 *
63 * @var int
64 */
65 protected $_id;
66
67 /**
68 * The parent note id, used when adding a comment to a note
69 *
70 * @var int
71 */
72 protected $_parentId;
73
74 public function preProcess() {
75 $this->_entityTable = $this->get('entityTable');
76 $this->_entityId = $this->get('entityId');
77 $this->_id = $this->get('id');
78 $this->_parentId = CRM_Utils_Array::value('parentId', $_GET, 0);
79 if ($this->_parentId) {
80 $this->assign('parentId', $this->_parentId);
81 }
82
83 if ($this->_id && CRM_Core_BAO_Note::getNotePrivacyHidden($this->_id)) {
84 CRM_Core_Error::statusBounce(ts('You do not have access to this note.'));
85 }
86 $this->setPageTitle($this->_parentId ? ts('Comment') : ts('Note'));
87 }
88
89 /**
90 * Set default values for the form. Note that in edit/view mode
91 * the default values are retrieved from the database
92 *
93 *
94 * @return void
95 */
96 public function setDefaultValues() {
97 $defaults = array();
98
99 if ($this->_action & CRM_Core_Action::UPDATE) {
100 if (isset($this->_id)) {
101 $params['id'] = $this->_id;
102 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Note', $params, $defaults);
103 }
104 if ($defaults['entity_table'] == 'civicrm_note') {
105 $defaults['parent_id'] = $defaults['entity_id'];
106 }
107 }
108 elseif ($this->_action & CRM_Core_Action::ADD && $this->_parentId) {
109 $defaults['parent_id'] = $this->_parentId;
110 $defaults['subject'] = 'Re: ' . CRM_Core_BAO_Note::getNoteSubject($this->_parentId);
111 }
112 return $defaults;
113 }
114
115 /**
116 * Explicitly declare the entity api name.
117 */
118 public function getDefaultEntity() {
119 return 'Note';
120 }
121
122 /**
123 * Explicitly declare the form context.
124 */
125 public function getDefaultContext() {
126 return 'create';
127 }
128
129 /**
130 * Build the form object.
131 *
132 * @return void
133 */
134 public function buildQuickForm() {
135 if ($this->_action & CRM_Core_Action::DELETE) {
136 $this->addButtons(array(
137 array(
138 'type' => 'next',
139 'name' => ts('Delete'),
140 'isDefault' => TRUE,
141 ),
142 array(
143 'type' => 'cancel',
144 'name' => ts('Cancel'),
145 ),
146 )
147 );
148 return;
149 }
150
151 $this->addField('subject');
152 $this->addField('note', [], TRUE);
153 $this->addField('privacy');
154 $this->add('hidden', 'parent_id');
155
156 // add attachments part
157 CRM_Core_BAO_File::buildAttachment($this, 'civicrm_note', $this->_id, NULL, TRUE);
158
159 $this->addButtons(array(
160 array(
161 'type' => 'upload',
162 'name' => ts('Save'),
163 'isDefault' => TRUE,
164 ),
165 array(
166 'type' => 'cancel',
167 'name' => ts('Cancel'),
168 ),
169 )
170 );
171 }
172
173 /**
174 *
175 * @return void
176 */
177 public function postProcess() {
178 // store the submitted values in an array
179 $params = $this->controller->exportValues($this->_name);
180
181 $session = CRM_Core_Session::singleton();
182 $params['contact_id'] = $session->get('userID');
183
184 if ($params['parent_id']) {
185 $params['entity_table'] = 'civicrm_note';
186 $params['entity_id'] = $params['parent_id'];
187 }
188 else {
189 $params['entity_table'] = $this->_entityTable;
190 $params['entity_id'] = $this->_entityId;
191 }
192
193 if ($this->_action & CRM_Core_Action::DELETE) {
194 CRM_Core_BAO_Note::del($this->_id);
195 return;
196 }
197
198 $params['id'] = NULL;
199 if ($this->_action & CRM_Core_Action::UPDATE) {
200 $params['id'] = $this->_id;
201 }
202
203 // add attachments as needed
204 CRM_Core_BAO_File::formatAttachment($params, $params, 'civicrm_note', $params['id']);
205
206 $ids = array();
207 $note = CRM_Core_BAO_Note::add($params, $ids);
208
209 CRM_Core_Session::setStatus(ts('Your Note has been saved.'), ts('Saved'), 'success');
210 }
211
212 }