Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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 | ||
00be9182 | 58 | public function preProcess() { |
6a488035 | 59 | $this->_entityTable = $this->get('entityTable'); |
353ffa53 TO |
60 | $this->_entityId = $this->get('entityId'); |
61 | $this->_id = $this->get('id'); | |
62 | $this->_parentId = CRM_Utils_Array::value('parentId', $_GET, 0); | |
6a488035 TO |
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 | } | |
e2046b33 | 70 | $this->setPageTitle($this->_parentId ? ts('Comment') : ts('Note')); |
6a488035 TO |
71 | } |
72 | ||
73 | /** | |
c490a46a | 74 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
75 | * the default values are retrieved from the database |
76 | * | |
6a488035 | 77 | * |
355ba699 | 78 | * @return void |
6a488035 | 79 | */ |
00be9182 | 80 | public function setDefaultValues() { |
be2fb01f | 81 | $defaults = []; |
6a488035 TO |
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 | ||
2b59da4a PN |
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 | ||
6a488035 | 113 | /** |
66f9e52b | 114 | * Build the form object. |
6a488035 | 115 | * |
355ba699 | 116 | * @return void |
6a488035 TO |
117 | */ |
118 | public function buildQuickForm() { | |
119 | if ($this->_action & CRM_Core_Action::DELETE) { | |
be2fb01f CW |
120 | $this->addButtons([ |
121 | [ | |
6a488035 TO |
122 | 'type' => 'next', |
123 | 'name' => ts('Delete'), | |
124 | 'isDefault' => TRUE, | |
be2fb01f CW |
125 | ], |
126 | [ | |
6a488035 TO |
127 | 'type' => 'cancel', |
128 | 'name' => ts('Cancel'), | |
be2fb01f | 129 | ], |
971e129b | 130 | ]); |
6a488035 TO |
131 | return; |
132 | } | |
133 | ||
2b59da4a PN |
134 | $this->addField('subject'); |
135 | $this->addField('note', [], TRUE); | |
136 | $this->addField('privacy'); | |
6a488035 TO |
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 | ||
be2fb01f CW |
142 | $this->addButtons([ |
143 | [ | |
6a488035 TO |
144 | 'type' => 'upload', |
145 | 'name' => ts('Save'), | |
146 | 'isDefault' => TRUE, | |
be2fb01f CW |
147 | ], |
148 | [ | |
6a488035 TO |
149 | 'type' => 'cancel', |
150 | 'name' => ts('Cancel'), | |
be2fb01f | 151 | ], |
971e129b | 152 | ] |
6a488035 TO |
153 | ); |
154 | } | |
155 | ||
156 | /** | |
6a488035 | 157 | * |
355ba699 | 158 | * @return void |
6a488035 TO |
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 | ||
0a3c22d0 | 181 | $params['id'] = NULL; |
6a488035 TO |
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 | ||
be2fb01f | 189 | $ids = []; |
6a488035 TO |
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 | } | |
96025800 | 194 | |
6a488035 | 195 | } |