Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0c56e4c8 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
0c56e4c8 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
0c56e4c8 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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
0c56e4c8 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
0c56e4c8 TO |
32 | * $Id$ |
33 | * | |
6a488035 TO |
34 | */ |
35 | ||
0c56e4c8 TO |
36 | /** |
37 | * BAO object for crm_log table | |
38 | */ | |
39 | class CRM_Core_BAO_File extends CRM_Core_DAO_File { | |
40 | ||
41 | static $_signableFields = array('entityTable', 'entityID', 'fileID'); | |
42 | ||
43 | /** | |
100fef9d CW |
44 | * @param int $fileID |
45 | * @param int $entityID | |
0c56e4c8 TO |
46 | * @param null $entityTable |
47 | * | |
48 | * @return array | |
49 | */ | |
00be9182 | 50 | public static function path($fileID, $entityID, $entityTable = NULL) { |
0c56e4c8 TO |
51 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); |
52 | if ($entityTable) { | |
53 | $entityFileDAO->entity_table = $entityTable; | |
54 | } | |
55 | $entityFileDAO->entity_id = $entityID; | |
56 | $entityFileDAO->file_id = $fileID; | |
57 | ||
58 | if ($entityFileDAO->find(TRUE)) { | |
59 | $fileDAO = new CRM_Core_DAO_File(); | |
60 | $fileDAO->id = $fileID; | |
61 | if ($fileDAO->find(TRUE)) { | |
62 | $config = CRM_Core_Config::singleton(); | |
63 | $path = $config->customFileUploadDir . $fileDAO->uri; | |
64 | ||
65 | if (file_exists($path) && is_readable($path)) { | |
66 | return array($path, $fileDAO->mime_type); | |
67 | } | |
68 | } | |
69 | } | |
70 | ||
71 | return array(NULL, NULL); | |
72 | } | |
73 | ||
74 | ||
75 | /** | |
76 | * @param $data | |
100fef9d | 77 | * @param int $fileTypeID |
0c56e4c8 | 78 | * @param $entityTable |
100fef9d | 79 | * @param int $entityID |
0c56e4c8 TO |
80 | * @param $entitySubtype |
81 | * @param bool $overwrite | |
75fd1335 | 82 | * @param null|array $fileParams |
0c56e4c8 TO |
83 | * @param string $uploadName |
84 | * @param null $mimeType | |
85 | * | |
86 | * @throws Exception | |
87 | */ | |
ae5ffbb7 | 88 | public static function filePostProcess( |
0c56e4c8 TO |
89 | $data, |
90 | $fileTypeID, | |
91 | $entityTable, | |
92 | $entityID, | |
93 | $entitySubtype, | |
94 | $overwrite = TRUE, | |
95 | $fileParams = NULL, | |
96 | $uploadName = 'uploadFile', | |
97 | $mimeType = NULL | |
98 | ) { | |
99 | if (!$mimeType) { | |
100 | CRM_Core_Error::fatal(ts('Mime Type is now a required parameter')); | |
101 | } | |
102 | ||
103 | $config = CRM_Core_Config::singleton(); | |
104 | ||
105 | $path = explode('/', $data); | |
106 | $filename = $path[count($path) - 1]; | |
107 | ||
108 | // rename this file to go into the secure directory | |
109 | if ($entitySubtype) { | |
110 | $directoryName = $config->customFileUploadDir . $entitySubtype . DIRECTORY_SEPARATOR . $entityID; | |
111 | } | |
112 | else { | |
113 | $directoryName = $config->customFileUploadDir; | |
114 | } | |
115 | ||
116 | CRM_Utils_File::createDir($directoryName); | |
117 | ||
118 | if (!rename($data, $directoryName . DIRECTORY_SEPARATOR . $filename)) { | |
119 | CRM_Core_Error::fatal(ts('Could not move custom file to custom upload directory')); | |
0c56e4c8 TO |
120 | } |
121 | ||
122 | // to get id's | |
123 | if ($overwrite && $fileTypeID) { | |
124 | list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID); | |
125 | } | |
126 | else { | |
127 | list($sql, $params) = self::sql($entityTable, $entityID, 0); | |
128 | } | |
129 | ||
130 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
131 | $dao->fetch(); | |
132 | ||
133 | $fileDAO = new CRM_Core_DAO_File(); | |
134 | $op = 'create'; | |
135 | if (isset($dao->cfID) && $dao->cfID) { | |
136 | $op = 'edit'; | |
137 | $fileDAO->id = $dao->cfID; | |
138 | unlink($directoryName . DIRECTORY_SEPARATOR . $dao->uri); | |
139 | } | |
140 | ||
141 | if (!empty($fileParams)) { | |
142 | $fileDAO->copyValues($fileParams); | |
143 | } | |
144 | ||
145 | $fileDAO->uri = $filename; | |
146 | $fileDAO->mime_type = $mimeType; | |
147 | $fileDAO->file_type_id = $fileTypeID; | |
148 | $fileDAO->upload_date = date('Ymdhis'); | |
149 | $fileDAO->save(); | |
150 | ||
151 | // need to add/update civicrm_entity_file | |
152 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); | |
153 | if (isset($dao->cefID) && $dao->cefID) { | |
154 | $entityFileDAO->id = $dao->cefID; | |
155 | } | |
156 | $entityFileDAO->entity_table = $entityTable; | |
157 | $entityFileDAO->entity_id = $entityID; | |
158 | $entityFileDAO->file_id = $fileDAO->id; | |
159 | $entityFileDAO->save(); | |
160 | ||
161 | //save static tags | |
162 | if (!empty($fileParams['tag'])) { | |
163 | CRM_Core_BAO_EntityTag::create($fileParams['tag'], 'civicrm_file', $entityFileDAO->id); | |
164 | } | |
165 | ||
166 | //save free tags | |
167 | if (isset($fileParams['attachment_taglist']) && !empty($fileParams['attachment_taglist'])) { | |
168 | CRM_Core_Form_Tag::postProcess($fileParams['attachment_taglist'], $entityFileDAO->id, 'civicrm_file', CRM_Core_DAO::$_nullObject); | |
169 | } | |
170 | ||
171 | // lets call the post hook here so attachments code can do the right stuff | |
172 | CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO); | |
173 | } | |
174 | ||
175 | /** | |
176 | * A static function wrapper that deletes the various objects that are | |
177 | * connected to a file object (i.e. file, entityFile and customValue | |
178 | */ | |
179 | public static function deleteFileReferences($fileID, $entityID, $fieldID) { | |
180 | $fileDAO = new CRM_Core_DAO_File(); | |
181 | $fileDAO->id = $fileID; | |
182 | if (!$fileDAO->find(TRUE)) { | |
183 | CRM_Core_Error::fatal(); | |
184 | } | |
185 | ||
186 | // lets call a pre hook before the delete, so attachments hooks can get the info before things | |
187 | // disappear | |
188 | CRM_Utils_Hook::pre('delete', 'File', $fileID, $fileDAO); | |
189 | ||
190 | // get the table and column name | |
191 | list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); | |
192 | ||
193 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); | |
194 | $entityFileDAO->file_id = $fileID; | |
195 | $entityFileDAO->entity_id = $entityID; | |
196 | $entityFileDAO->entity_table = $tableName; | |
197 | ||
198 | if (!$entityFileDAO->find(TRUE)) { | |
199 | CRM_Core_Error::fatal(); | |
200 | } | |
201 | ||
202 | $entityFileDAO->delete(); | |
203 | $fileDAO->delete(); | |
204 | ||
205 | // also set the value to null of the table and column | |
206 | $query = "UPDATE $tableName SET $columnName = null WHERE $columnName = %1"; | |
207 | $params = array(1 => array($fileID, 'Integer')); | |
208 | CRM_Core_DAO::executeQuery($query, $params); | |
209 | } | |
210 | ||
211 | /** | |
212 | * The $useWhere is used so that the signature matches the parent class | |
0527cd81 | 213 | * |
353ffa53 TO |
214 | * public function delete($useWhere = FALSE) { |
215 | * list($fileID, $entityID, $fieldID) = func_get_args(); | |
216 | * | |
217 | * self::deleteFileReferences($fileID, $entityID, $fieldID); | |
218 | * } */ | |
0c56e4c8 TO |
219 | |
220 | /** | |
100fef9d | 221 | * Delete all the files and associated object associated with this |
0c56e4c8 | 222 | * combination |
7b8bc7a1 SB |
223 | * @return bool |
224 | * Was file deleted? | |
0c56e4c8 | 225 | */ |
00be9182 | 226 | public static function deleteEntityFile($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) { |
7b8bc7a1 | 227 | $isDeleted = FALSE; |
0c56e4c8 | 228 | if (empty($entityTable) || empty($entityID)) { |
7b8bc7a1 | 229 | return $isDeleted; |
0c56e4c8 TO |
230 | } |
231 | ||
232 | $config = CRM_Core_Config::singleton(); | |
233 | ||
234 | list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID, $fileID); | |
235 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
236 | ||
237 | $cfIDs = array(); | |
238 | $cefIDs = array(); | |
239 | while ($dao->fetch()) { | |
240 | $cfIDs[$dao->cfID] = $dao->uri; | |
241 | $cefIDs[] = $dao->cefID; | |
242 | } | |
243 | ||
244 | if (!empty($cefIDs)) { | |
245 | $cefIDs = implode(',', $cefIDs); | |
246 | $sql = "DELETE FROM civicrm_entity_file where id IN ( $cefIDs )"; | |
247 | CRM_Core_DAO::executeQuery($sql); | |
7b8bc7a1 | 248 | $isDeleted = TRUE; |
0c56e4c8 TO |
249 | } |
250 | ||
251 | if (!empty($cfIDs)) { | |
252 | // Delete file only if there no any entity using this file. | |
253 | $deleteFiles = array(); | |
254 | foreach ($cfIDs as $fId => $fUri) { | |
255 | //delete tags from entity tag table | |
256 | $tagParams = array( | |
257 | 'entity_table' => 'civicrm_file', | |
21dfd5f5 | 258 | 'entity_id' => $fId, |
0c56e4c8 TO |
259 | ); |
260 | ||
261 | CRM_Core_BAO_EntityTag::del($tagParams); | |
262 | ||
263 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fId, 'id', 'file_id')) { | |
264 | unlink($config->customFileUploadDir . DIRECTORY_SEPARATOR . $fUri); | |
265 | $deleteFiles[$fId] = $fId; | |
266 | } | |
267 | } | |
268 | ||
269 | if (!empty($deleteFiles)) { | |
270 | $deleteFiles = implode(',', $deleteFiles); | |
271 | $sql = "DELETE FROM civicrm_file where id IN ( $deleteFiles )"; | |
272 | CRM_Core_DAO::executeQuery($sql); | |
273 | } | |
7b8bc7a1 | 274 | $isDeleted = TRUE; |
0c56e4c8 | 275 | } |
7b8bc7a1 | 276 | return $isDeleted; |
0c56e4c8 TO |
277 | } |
278 | ||
279 | /** | |
100fef9d | 280 | * Get all the files and associated object associated with this |
0c56e4c8 TO |
281 | * combination |
282 | */ | |
00be9182 | 283 | public static function getEntityFile($entityTable, $entityID, $addDeleteArgs = FALSE) { |
0c56e4c8 TO |
284 | if (empty($entityTable) || !$entityID) { |
285 | $results = NULL; | |
286 | return $results; | |
287 | } | |
288 | ||
289 | $config = CRM_Core_Config::singleton(); | |
290 | ||
291 | list($sql, $params) = self::sql($entityTable, $entityID, NULL); | |
292 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
293 | $results = array(); | |
294 | while ($dao->fetch()) { | |
295 | $result['fileID'] = $dao->cfID; | |
296 | $result['entityID'] = $dao->cefID; | |
297 | $result['mime_type'] = $dao->mime_type; | |
298 | $result['fileName'] = $dao->uri; | |
299 | $result['description'] = $dao->description; | |
300 | $result['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri); | |
301 | $result['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri; | |
06faabfa | 302 | $result['url'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$dao->cfID}&eid={$dao->entity_id}"); |
0c56e4c8 TO |
303 | $result['href'] = "<a href=\"{$result['url']}\">{$result['cleanName']}</a>"; |
304 | $result['tag'] = CRM_Core_BAO_EntityTag::getTag($dao->cfID, 'civicrm_file'); | |
305 | if ($addDeleteArgs) { | |
06faabfa | 306 | $result['deleteURLArgs'] = self::deleteURLArgs($dao->entity_table, $dao->entity_id, $dao->cfID); |
0c56e4c8 TO |
307 | } |
308 | $results[$dao->cfID] = $result; | |
309 | } | |
310 | ||
311 | //fix tag names | |
312 | $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); | |
313 | ||
314 | foreach ($results as &$values) { | |
315 | if (!empty($values['tag'])) { | |
316 | $tagNames = array(); | |
317 | foreach ($values['tag'] as $tid) { | |
318 | $tagNames[] = $tags[$tid]; | |
319 | } | |
320 | $values['tag'] = implode(', ', $tagNames); | |
321 | } | |
322 | else { | |
323 | $values['tag'] = ''; | |
324 | } | |
325 | } | |
326 | ||
327 | $dao->free(); | |
328 | return $results; | |
329 | } | |
330 | ||
331 | /** | |
6a0b768e TO |
332 | * @param string $entityTable |
333 | * Table-name or "*" (to reference files directly by file-id). | |
06faabfa | 334 | * @param int $entityID |
100fef9d CW |
335 | * @param int $fileTypeID |
336 | * @param int $fileID | |
0c56e4c8 TO |
337 | * |
338 | * @return array | |
339 | */ | |
00be9182 | 340 | public static function sql($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) { |
06faabfa TO |
341 | if ($entityTable == '*') { |
342 | // $entityID is the ID of a specific file | |
343 | $sql = " | |
344 | SELECT CF.id as cfID, | |
345 | CF.uri as uri, | |
346 | CF.mime_type as mime_type, | |
347 | CF.description as description, | |
348 | CEF.id as cefID, | |
349 | CEF.entity_table as entity_table, | |
350 | CEF.entity_id as entity_id | |
351 | FROM civicrm_file AS CF | |
352 | LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id ) | |
353 | WHERE CF.id = %2"; | |
354 | ||
0db6c3e1 TO |
355 | } |
356 | else { | |
06faabfa | 357 | $sql = " |
0c56e4c8 | 358 | SELECT CF.id as cfID, |
6a488035 TO |
359 | CF.uri as uri, |
360 | CF.mime_type as mime_type, | |
361 | CF.description as description, | |
06faabfa TO |
362 | CEF.id as cefID, |
363 | CEF.entity_table as entity_table, | |
364 | CEF.entity_id as entity_id | |
0c56e4c8 TO |
365 | FROM civicrm_file AS CF |
366 | LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id ) | |
367 | WHERE CEF.entity_table = %1 | |
368 | AND CEF.entity_id = %2"; | |
06faabfa | 369 | } |
0c56e4c8 TO |
370 | |
371 | $params = array( | |
372 | 1 => array($entityTable, 'String'), | |
373 | 2 => array($entityID, 'Integer'), | |
374 | ); | |
375 | ||
376 | if ($fileTypeID !== NULL) { | |
377 | $sql .= " AND CF.file_type_id = %3"; | |
378 | $params[3] = array($fileTypeID, 'Integer'); | |
379 | } | |
380 | ||
381 | if ($fileID !== NULL) { | |
382 | $sql .= " AND CF.id = %4"; | |
383 | $params[4] = array($fileID, 'Integer'); | |
384 | } | |
385 | ||
386 | return array($sql, $params); | |
387 | } | |
388 | ||
389 | /** | |
75fd1335 TO |
390 | * @param CRM_Core_Form $form |
391 | * @param string $entityTable | |
100fef9d | 392 | * @param int $entityID |
0c56e4c8 TO |
393 | * @param null $numAttachments |
394 | * @param bool $ajaxDelete | |
395 | */ | |
00be9182 | 396 | public static function buildAttachment(&$form, $entityTable, $entityID = NULL, $numAttachments = NULL, $ajaxDelete = FALSE) { |
0c56e4c8 TO |
397 | |
398 | if (!$numAttachments) { | |
399 | $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); | |
400 | } | |
401 | // Assign maxAttachments count to template for help message | |
402 | $form->assign('maxAttachments', $numAttachments); | |
403 | ||
404 | $config = CRM_Core_Config::singleton(); | |
405 | // set default max file size as 2MB | |
406 | $maxFileSize = $config->maxFileSize ? $config->maxFileSize : 2; | |
407 | ||
408 | $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID, TRUE); | |
409 | $totalAttachments = 0; | |
410 | if ($currentAttachmentInfo) { | |
411 | $totalAttachments = count($currentAttachmentInfo); | |
412 | $form->add('checkbox', 'is_delete_attachment', ts('Delete All Attachment(s)')); | |
413 | $form->assign('currentAttachmentInfo', $currentAttachmentInfo); | |
414 | } | |
415 | else { | |
416 | $form->assign('currentAttachmentInfo', NULL); | |
417 | } | |
418 | ||
419 | if ($totalAttachments) { | |
420 | if ($totalAttachments >= $numAttachments) { | |
421 | $numAttachments = 0; | |
422 | } | |
423 | else { | |
424 | $numAttachments -= $totalAttachments; | |
425 | } | |
426 | } | |
427 | ||
428 | $form->assign('numAttachments', $numAttachments); | |
429 | ||
e0f9d6a2 | 430 | CRM_Core_BAO_Tag::getTags('civicrm_file', $tags, NULL, |
431 | ' ', TRUE); | |
0c56e4c8 TO |
432 | |
433 | // get tagset info | |
434 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_file'); | |
435 | ||
436 | // add attachments | |
437 | for ($i = 1; $i <= $numAttachments; $i++) { | |
438 | $form->addElement('file', "attachFile_$i", ts('Attach File'), 'size=30 maxlength=60'); | |
439 | $form->addUploadElement("attachFile_$i"); | |
440 | $form->setMaxFileSize($maxFileSize * 1024 * 1024); | |
441 | $form->addRule("attachFile_$i", | |
442 | ts('File size should be less than %1 MByte(s)', | |
443 | array(1 => $maxFileSize) | |
444 | ), | |
445 | 'maxfilesize', | |
446 | $maxFileSize * 1024 * 1024 | |
447 | ); | |
448 | $form->addElement('text', "attachDesc_$i", NULL, array( | |
449 | 'size' => 40, | |
450 | 'maxlength' => 255, | |
21dfd5f5 | 451 | 'placeholder' => ts('Description'), |
0c56e4c8 TO |
452 | )); |
453 | ||
454 | if (!empty($tags)) { | |
455 | $form->add('select', "tag_$i", ts('Tags'), $tags, FALSE, | |
456 | array( | |
457 | 'id' => "tags_$i", | |
458 | 'multiple' => 'multiple', | |
459 | 'class' => 'huge crm-select2', | |
21dfd5f5 | 460 | 'placeholder' => ts('- none -'), |
0c56e4c8 TO |
461 | ) |
462 | ); | |
463 | } | |
464 | CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_file', NULL, FALSE, TRUE, "file_taglist_$i"); | |
465 | } | |
466 | } | |
467 | ||
468 | /** | |
100fef9d | 469 | * Return a clean url string and the number of attachment for a |
0c56e4c8 TO |
470 | * given entityTable, entityID |
471 | * | |
5a4f6742 CW |
472 | * @param string $entityTable |
473 | * The entityTable to which the file is attached. | |
474 | * @param int $entityID | |
475 | * The id of the object in the above entityTable. | |
476 | * @param string $separator | |
477 | * The string separator where to implode the urls. | |
0c56e4c8 | 478 | * |
a6c01b45 CW |
479 | * @return array |
480 | * An array with 2 elements. The string and the number of attachments | |
0c56e4c8 | 481 | */ |
00be9182 | 482 | public static function attachmentInfo($entityTable, $entityID, $separator = '<br />') { |
0c56e4c8 TO |
483 | if (!$entityID) { |
484 | return NULL; | |
485 | } | |
486 | ||
487 | $currentAttachments = self::getEntityFile($entityTable, $entityID); | |
488 | if (!empty($currentAttachments)) { | |
489 | $currentAttachmentURL = array(); | |
490 | foreach ($currentAttachments as $fileID => $attach) { | |
491 | $currentAttachmentURL[] = $attach['href']; | |
492 | } | |
493 | return implode($separator, $currentAttachmentURL); | |
494 | } | |
495 | return NULL; | |
496 | } | |
497 | ||
498 | /** | |
499 | * @param $formValues | |
c490a46a | 500 | * @param array $params |
0c56e4c8 | 501 | * @param $entityTable |
100fef9d | 502 | * @param int $entityID |
0c56e4c8 | 503 | */ |
ae5ffbb7 | 504 | public static function formatAttachment( |
0c56e4c8 TO |
505 | &$formValues, |
506 | &$params, | |
507 | $entityTable, | |
508 | $entityID = NULL | |
509 | ) { | |
510 | ||
511 | // delete current attachments if applicable | |
512 | if ($entityID && !empty($formValues['is_delete_attachment'])) { | |
513 | CRM_Core_BAO_File::deleteEntityFile($entityTable, $entityID); | |
514 | } | |
515 | ||
516 | $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); | |
517 | ||
518 | $now = date('Ymdhis'); | |
519 | ||
520 | // setup all attachments | |
521 | for ($i = 1; $i <= $numAttachments; $i++) { | |
522 | $attachName = "attachFile_$i"; | |
523 | $attachDesc = "attachDesc_$i"; | |
524 | $attachTags = "tag_$i"; | |
525 | $attachFreeTags = "file_taglist_$i"; | |
526 | if (isset($formValues[$attachName]) && !empty($formValues[$attachName])) { | |
527 | // add static tags if selects | |
528 | $tagParams = array(); | |
529 | if (!empty($formValues[$attachTags])) { | |
530 | foreach ($formValues[$attachTags] as $tag) { | |
531 | $tagParams[$tag] = 1; | |
532 | } | |
533 | } | |
534 | ||
535 | // we dont care if the file is empty or not | |
536 | // CRM-7448 | |
537 | $fileParams = array( | |
538 | 'uri' => $formValues[$attachName]['name'], | |
539 | 'type' => $formValues[$attachName]['type'], | |
540 | 'location' => $formValues[$attachName]['name'], | |
541 | 'description' => $formValues[$attachDesc], | |
542 | 'upload_date' => $now, | |
543 | 'tag' => $tagParams, | |
21dfd5f5 | 544 | 'attachment_taglist' => CRM_Utils_Array::value($attachFreeTags, $formValues, array()), |
0c56e4c8 TO |
545 | ); |
546 | ||
547 | $params[$attachName] = $fileParams; | |
548 | } | |
549 | } | |
550 | } | |
551 | ||
552 | /** | |
c490a46a | 553 | * @param array $params |
0c56e4c8 | 554 | * @param $entityTable |
100fef9d | 555 | * @param int $entityID |
0c56e4c8 | 556 | */ |
00be9182 | 557 | public static function processAttachment(&$params, $entityTable, $entityID) { |
0c56e4c8 TO |
558 | $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); |
559 | ||
560 | for ($i = 1; $i <= $numAttachments; $i++) { | |
561 | if ( | |
562 | isset($params["attachFile_$i"]) && | |
563 | is_array($params["attachFile_$i"]) | |
564 | ) { | |
565 | self::filePostProcess( | |
566 | $params["attachFile_$i"]['location'], | |
567 | NULL, | |
568 | $entityTable, | |
569 | $entityID, | |
570 | NULL, | |
571 | TRUE, | |
572 | $params["attachFile_$i"], | |
573 | "attachFile_$i", | |
574 | $params["attachFile_$i"]['type'] | |
575 | ); | |
576 | } | |
577 | } | |
578 | } | |
579 | ||
580 | /** | |
581 | * @return array | |
582 | */ | |
00be9182 | 583 | public static function uploadNames() { |
0c56e4c8 TO |
584 | $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); |
585 | ||
586 | $names = array(); | |
587 | for ($i = 1; $i <= $numAttachments; $i++) { | |
588 | $names[] = "attachFile_{$i}"; | |
589 | } | |
590 | $names[] = 'uploadFile'; | |
591 | return $names; | |
592 | } | |
593 | ||
d424ffde | 594 | /** |
c490a46a | 595 | * copy/attach an existing file to a different entity |
0c56e4c8 | 596 | * table and id. |
d424ffde | 597 | * |
0c56e4c8 | 598 | * @param $oldEntityTable |
100fef9d | 599 | * @param int $oldEntityId |
0c56e4c8 | 600 | * @param $newEntityTable |
100fef9d | 601 | * @param int $newEntityId |
0c56e4c8 | 602 | */ |
00be9182 | 603 | public static function copyEntityFile($oldEntityTable, $oldEntityId, $newEntityTable, $newEntityId) { |
6a488035 TO |
604 | $oldEntityFile = new CRM_Core_DAO_EntityFile(); |
605 | $oldEntityFile->entity_id = $oldEntityId; | |
606 | $oldEntityFile->entity_table = $oldEntityTable; | |
607 | $oldEntityFile->find(); | |
608 | ||
609 | while ($oldEntityFile->fetch()) { | |
610 | $newEntityFile = new CRM_Core_DAO_EntityFile(); | |
611 | $newEntityFile->entity_id = $newEntityId; | |
612 | $newEntityFile->entity_table = $newEntityTable; | |
613 | $newEntityFile->file_id = $oldEntityFile->file_id; | |
614 | $newEntityFile->save(); | |
615 | } | |
616 | } | |
617 | ||
0c56e4c8 TO |
618 | /** |
619 | * @param $entityTable | |
100fef9d CW |
620 | * @param int $entityID |
621 | * @param int $fileID | |
0c56e4c8 TO |
622 | * |
623 | * @return string | |
624 | */ | |
00be9182 | 625 | public static function deleteURLArgs($entityTable, $entityID, $fileID) { |
6a488035 | 626 | $params['entityTable'] = $entityTable; |
0c56e4c8 TO |
627 | $params['entityID'] = $entityID; |
628 | $params['fileID'] = $fileID; | |
6a488035 TO |
629 | |
630 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields); | |
631 | $params['_sgn'] = $signer->sign($params); | |
632 | return CRM_Utils_System::makeQueryString($params); | |
633 | } | |
634 | ||
635 | /** | |
100fef9d | 636 | * Delete a file attachment from an entity table / entity ID |
6a488035 | 637 | * |
6a488035 | 638 | */ |
00be9182 | 639 | public static function deleteAttachment() { |
0c56e4c8 TO |
640 | $params = array(); |
641 | $params['entityTable'] = CRM_Utils_Request::retrieve('entityTable', 'String', CRM_Core_DAO::$_nullObject, TRUE); | |
642 | $params['entityID'] = CRM_Utils_Request::retrieve('entityID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); | |
643 | $params['fileID'] = CRM_Utils_Request::retrieve('fileID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); | |
6a488035 | 644 | |
0c56e4c8 | 645 | $signature = CRM_Utils_Request::retrieve('_sgn', 'String', CRM_Core_DAO::$_nullObject, TRUE); |
6a488035 TO |
646 | |
647 | $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields); | |
0c56e4c8 | 648 | if (!$signer->validate($signature, $params)) { |
6a488035 TO |
649 | CRM_Core_Error::fatal('Request signature is invalid'); |
650 | } | |
651 | ||
652 | CRM_Core_BAO_File::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']); | |
653 | } | |
654 | ||
6a488035 | 655 | |
34f51a07 | 656 | /** |
100fef9d | 657 | * Display paper icon for a file attachment -- CRM-13624 |
34f51a07 | 658 | * |
5a4f6742 CW |
659 | * @param string $entityTable |
660 | * The entityTable to which the file is attached. eg "civicrm_contact", "civicrm_note", "civicrm_activity". | |
06faabfa | 661 | * If you have the ID of a specific row in civicrm_file, use $entityTable='*' |
5a4f6742 CW |
662 | * @param int $entityID |
663 | * The id of the object in the above entityTable. | |
1a7ab71e | 664 | * |
72b3a70c CW |
665 | * @return array|NULL |
666 | * list of HTML snippets; one HTML snippet for each attachment. If none found, then NULL | |
1a7ab71e | 667 | * |
34f51a07 | 668 | */ |
00be9182 | 669 | public static function paperIconAttachment($entityTable, $entityID) { |
0c56e4c8 TO |
670 | if (empty($entityTable) || !$entityID) { |
671 | $results = NULL; | |
672 | return $results; | |
673 | } | |
674 | $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID); | |
675 | foreach ($currentAttachmentInfo as $fileKey => $fileValue) { | |
34f51a07 N |
676 | $fileID = $fileValue['fileID']; |
677 | $fileType = $fileValue['mime_type']; | |
0c56e4c8 | 678 | if ($fileID) { |
34f51a07 | 679 | if ($fileType == 'image/jpeg' || |
0c56e4c8 TO |
680 | $fileType == 'image/pjpeg' || |
681 | $fileType == 'image/gif' || | |
682 | $fileType == 'image/x-png' || | |
683 | $fileType == 'image/png' | |
684 | ) { | |
34f51a07 N |
685 | $url = $fileValue['url']; |
686 | $alt = $fileValue['cleanName']; | |
687 | $file_url[$fileID] = " | |
688 | <a href=\"$url\" class='crm-image-popup'> | |
689 | <div class='icon paper-icon' title=\"$alt\" alt=\"$alt\"></div> | |
690 | </a>"; | |
691 | // for non image files | |
692 | } | |
693 | else { | |
694 | $url = $fileValue['url']; | |
695 | $alt = $fileValue['cleanName']; | |
696 | $file_url[$fileID] = "<a href=\"$url\"><div class='icon paper-icon' title=\"$alt\" alt=\"$alt\"></div></a>"; | |
697 | } | |
698 | } | |
699 | } | |
0c56e4c8 TO |
700 | if (empty($file_url)) { |
701 | $results = NULL; | |
34f51a07 N |
702 | } |
703 | else { | |
0c56e4c8 | 704 | $results = $file_url; |
34f51a07 N |
705 | } |
706 | return $results; | |
707 | } | |
6cccc6d4 TO |
708 | |
709 | /** | |
710 | * Get a reference to the file-search service (if one is available). | |
711 | * | |
712 | * @return CRM_Core_FileSearchInterface|NULL | |
713 | */ | |
00be9182 | 714 | public static function getSearchService() { |
6cccc6d4 TO |
715 | $fileSearches = array(); |
716 | CRM_Utils_Hook::fileSearches($fileSearches); | |
717 | ||
718 | // use the first available search | |
719 | foreach ($fileSearches as $fileSearch) { | |
720 | /** @var $fileSearch CRM_Core_FileSearchInterface */ | |
721 | return $fileSearch; | |
722 | } | |
723 | return NULL; | |
724 | } | |
96025800 | 725 | |
b2aaa85e | 726 | } |