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