Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | ||
00be9182 | 74 | public function preProcess() { |
6a488035 TO |
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 | } | |
e2046b33 | 86 | $this->setPageTitle($this->_parentId ? ts('Comment') : ts('Note')); |
6a488035 TO |
87 | } |
88 | ||
89 | /** | |
c490a46a | 90 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
91 | * the default values are retrieved from the database |
92 | * | |
93 | * @access public | |
94 | * | |
355ba699 | 95 | * @return void |
6a488035 | 96 | */ |
00be9182 | 97 | public function setDefaultValues() { |
6a488035 TO |
98 | $defaults = array(); |
99 | ||
100 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
101 | if (isset($this->_id)) { | |
102 | $params['id'] = $this->_id; | |
103 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_Note', $params, $defaults); | |
104 | } | |
105 | if ($defaults['entity_table'] == 'civicrm_note') { | |
106 | $defaults['parent_id'] = $defaults['entity_id']; | |
107 | } | |
108 | } | |
109 | elseif ($this->_action & CRM_Core_Action::ADD && $this->_parentId) { | |
110 | $defaults['parent_id'] = $this->_parentId; | |
111 | $defaults['subject'] = 'Re: ' . CRM_Core_BAO_Note::getNoteSubject($this->_parentId); | |
112 | } | |
113 | return $defaults; | |
114 | } | |
115 | ||
116 | /** | |
c490a46a | 117 | * Build the form object |
6a488035 | 118 | * |
355ba699 | 119 | * @return void |
6a488035 TO |
120 | * @access public |
121 | */ | |
122 | public function buildQuickForm() { | |
123 | if ($this->_action & CRM_Core_Action::DELETE) { | |
124 | $this->addButtons(array( | |
125 | array( | |
126 | 'type' => 'next', | |
127 | 'name' => ts('Delete'), | |
128 | 'isDefault' => TRUE, | |
129 | ), | |
130 | array( | |
131 | 'type' => 'cancel', | |
132 | 'name' => ts('Cancel'), | |
133 | ), | |
134 | ) | |
135 | ); | |
136 | return; | |
137 | } | |
138 | ||
139 | $this->add('text', 'subject', ts('Subject:'), array('size' => 20)); | |
140 | $this->add('textarea', 'note', ts('Note:'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note', 'note'), TRUE); | |
141 | $this->add('select', 'privacy', ts('Privacy:'), CRM_Core_OptionGroup::values('note_privacy')); | |
142 | ||
143 | $this->add('hidden', 'parent_id'); | |
144 | ||
145 | // add attachments part | |
146 | CRM_Core_BAO_File::buildAttachment($this, 'civicrm_note', $this->_id, NULL, TRUE); | |
147 | ||
148 | $this->addButtons(array( | |
149 | array( | |
150 | 'type' => 'upload', | |
151 | 'name' => ts('Save'), | |
152 | 'isDefault' => TRUE, | |
153 | ), | |
154 | array( | |
155 | 'type' => 'cancel', | |
156 | 'name' => ts('Cancel'), | |
157 | ), | |
158 | ) | |
159 | ); | |
160 | } | |
161 | ||
162 | /** | |
6a488035 TO |
163 | * @access public |
164 | * | |
355ba699 | 165 | * @return void |
6a488035 TO |
166 | */ |
167 | public function postProcess() { | |
168 | // store the submitted values in an array | |
169 | $params = $this->controller->exportValues($this->_name); | |
170 | ||
171 | $session = CRM_Core_Session::singleton(); | |
172 | $params['contact_id'] = $session->get('userID'); | |
173 | ||
174 | if ($params['parent_id']) { | |
175 | $params['entity_table'] = 'civicrm_note'; | |
176 | $params['entity_id'] = $params['parent_id']; | |
177 | } | |
178 | else { | |
179 | $params['entity_table'] = $this->_entityTable; | |
180 | $params['entity_id'] = $this->_entityId; | |
181 | } | |
182 | ||
183 | if ($this->_action & CRM_Core_Action::DELETE) { | |
184 | CRM_Core_BAO_Note::del($this->_id); | |
185 | return; | |
186 | } | |
187 | ||
188 | $params['id'] = null; | |
189 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
190 | $params['id'] = $this->_id; | |
191 | } | |
192 | ||
193 | // add attachments as needed | |
194 | CRM_Core_BAO_File::formatAttachment($params, $params, 'civicrm_note', $params['id']); | |
195 | ||
196 | $ids = array(); | |
197 | $note = CRM_Core_BAO_Note::add($params, $ids); | |
198 | ||
199 | CRM_Core_Session::setStatus(ts('Your Note has been saved.'), ts('Saved'), 'success'); | |
200 | } | |
201 | } |