Fix api.File.delete to actually delete the file
[civicrm-core.git] / CRM / Core / BAO / File.php
CommitLineData
6a488035 1<?php
0c56e4c8
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 */
39class 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
TO
222 * combination
223 */
00be9182 224 public static function deleteEntityFile($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
0c56e4c8
TO
225 if (empty($entityTable) || empty($entityID)) {
226 return;
227 }
228
229 $config = CRM_Core_Config::singleton();
230
231 list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID, $fileID);
232 $dao = CRM_Core_DAO::executeQuery($sql, $params);
233
234 $cfIDs = array();
235 $cefIDs = array();
236 while ($dao->fetch()) {
237 $cfIDs[$dao->cfID] = $dao->uri;
238 $cefIDs[] = $dao->cefID;
239 }
240
241 if (!empty($cefIDs)) {
242 $cefIDs = implode(',', $cefIDs);
243 $sql = "DELETE FROM civicrm_entity_file where id IN ( $cefIDs )";
244 CRM_Core_DAO::executeQuery($sql);
245 }
246
247 if (!empty($cfIDs)) {
248 // Delete file only if there no any entity using this file.
249 $deleteFiles = array();
250 foreach ($cfIDs as $fId => $fUri) {
251 //delete tags from entity tag table
252 $tagParams = array(
253 'entity_table' => 'civicrm_file',
21dfd5f5 254 'entity_id' => $fId,
0c56e4c8
TO
255 );
256
257 CRM_Core_BAO_EntityTag::del($tagParams);
258
259 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fId, 'id', 'file_id')) {
260 unlink($config->customFileUploadDir . DIRECTORY_SEPARATOR . $fUri);
261 $deleteFiles[$fId] = $fId;
262 }
263 }
264
265 if (!empty($deleteFiles)) {
266 $deleteFiles = implode(',', $deleteFiles);
267 $sql = "DELETE FROM civicrm_file where id IN ( $deleteFiles )";
268 CRM_Core_DAO::executeQuery($sql);
269 }
270 }
271 }
272
273 /**
100fef9d 274 * Get all the files and associated object associated with this
0c56e4c8
TO
275 * combination
276 */
00be9182 277 public static function getEntityFile($entityTable, $entityID, $addDeleteArgs = FALSE) {
0c56e4c8
TO
278 if (empty($entityTable) || !$entityID) {
279 $results = NULL;
280 return $results;
281 }
282
283 $config = CRM_Core_Config::singleton();
284
285 list($sql, $params) = self::sql($entityTable, $entityID, NULL);
286 $dao = CRM_Core_DAO::executeQuery($sql, $params);
287 $results = array();
288 while ($dao->fetch()) {
289 $result['fileID'] = $dao->cfID;
290 $result['entityID'] = $dao->cefID;
291 $result['mime_type'] = $dao->mime_type;
292 $result['fileName'] = $dao->uri;
293 $result['description'] = $dao->description;
294 $result['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri);
295 $result['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri;
06faabfa 296 $result['url'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$dao->cfID}&eid={$dao->entity_id}");
0c56e4c8
TO
297 $result['href'] = "<a href=\"{$result['url']}\">{$result['cleanName']}</a>";
298 $result['tag'] = CRM_Core_BAO_EntityTag::getTag($dao->cfID, 'civicrm_file');
299 if ($addDeleteArgs) {
06faabfa 300 $result['deleteURLArgs'] = self::deleteURLArgs($dao->entity_table, $dao->entity_id, $dao->cfID);
0c56e4c8
TO
301 }
302 $results[$dao->cfID] = $result;
303 }
304
305 //fix tag names
306 $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
307
308 foreach ($results as &$values) {
309 if (!empty($values['tag'])) {
310 $tagNames = array();
311 foreach ($values['tag'] as $tid) {
312 $tagNames[] = $tags[$tid];
313 }
314 $values['tag'] = implode(', ', $tagNames);
315 }
316 else {
317 $values['tag'] = '';
318 }
319 }
320
321 $dao->free();
322 return $results;
323 }
324
325 /**
6a0b768e
TO
326 * @param string $entityTable
327 * Table-name or "*" (to reference files directly by file-id).
06faabfa 328 * @param int $entityID
100fef9d
CW
329 * @param int $fileTypeID
330 * @param int $fileID
0c56e4c8
TO
331 *
332 * @return array
333 */
00be9182 334 public static function sql($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
06faabfa
TO
335 if ($entityTable == '*') {
336 // $entityID is the ID of a specific file
337 $sql = "
338SELECT CF.id as cfID,
339 CF.uri as uri,
340 CF.mime_type as mime_type,
341 CF.description as description,
342 CEF.id as cefID,
343 CEF.entity_table as entity_table,
344 CEF.entity_id as entity_id
345FROM civicrm_file AS CF
346LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id )
347WHERE CF.id = %2";
348
0db6c3e1
TO
349 }
350 else {
06faabfa 351 $sql = "
0c56e4c8 352SELECT CF.id as cfID,
6a488035
TO
353 CF.uri as uri,
354 CF.mime_type as mime_type,
355 CF.description as description,
06faabfa
TO
356 CEF.id as cefID,
357 CEF.entity_table as entity_table,
358 CEF.entity_id as entity_id
0c56e4c8
TO
359FROM civicrm_file AS CF
360LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id )
361WHERE CEF.entity_table = %1
362AND CEF.entity_id = %2";
06faabfa 363 }
0c56e4c8
TO
364
365 $params = array(
366 1 => array($entityTable, 'String'),
367 2 => array($entityID, 'Integer'),
368 );
369
370 if ($fileTypeID !== NULL) {
371 $sql .= " AND CF.file_type_id = %3";
372 $params[3] = array($fileTypeID, 'Integer');
373 }
374
375 if ($fileID !== NULL) {
376 $sql .= " AND CF.id = %4";
377 $params[4] = array($fileID, 'Integer');
378 }
379
380 return array($sql, $params);
381 }
382
383 /**
75fd1335
TO
384 * @param CRM_Core_Form $form
385 * @param string $entityTable
100fef9d 386 * @param int $entityID
0c56e4c8
TO
387 * @param null $numAttachments
388 * @param bool $ajaxDelete
389 */
00be9182 390 public static function buildAttachment(&$form, $entityTable, $entityID = NULL, $numAttachments = NULL, $ajaxDelete = FALSE) {
0c56e4c8
TO
391
392 if (!$numAttachments) {
393 $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments');
394 }
395 // Assign maxAttachments count to template for help message
396 $form->assign('maxAttachments', $numAttachments);
397
398 $config = CRM_Core_Config::singleton();
399 // set default max file size as 2MB
400 $maxFileSize = $config->maxFileSize ? $config->maxFileSize : 2;
401
402 $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID, TRUE);
403 $totalAttachments = 0;
404 if ($currentAttachmentInfo) {
405 $totalAttachments = count($currentAttachmentInfo);
406 $form->add('checkbox', 'is_delete_attachment', ts('Delete All Attachment(s)'));
407 $form->assign('currentAttachmentInfo', $currentAttachmentInfo);
408 }
409 else {
410 $form->assign('currentAttachmentInfo', NULL);
411 }
412
413 if ($totalAttachments) {
414 if ($totalAttachments >= $numAttachments) {
415 $numAttachments = 0;
416 }
417 else {
418 $numAttachments -= $totalAttachments;
419 }
420 }
421
422 $form->assign('numAttachments', $numAttachments);
423
e0f9d6a2 424 CRM_Core_BAO_Tag::getTags('civicrm_file', $tags, NULL,
425 '&nbsp;&nbsp;', TRUE);
0c56e4c8
TO
426
427 // get tagset info
428 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_file');
429
430 // add attachments
431 for ($i = 1; $i <= $numAttachments; $i++) {
432 $form->addElement('file', "attachFile_$i", ts('Attach File'), 'size=30 maxlength=60');
433 $form->addUploadElement("attachFile_$i");
434 $form->setMaxFileSize($maxFileSize * 1024 * 1024);
435 $form->addRule("attachFile_$i",
436 ts('File size should be less than %1 MByte(s)',
437 array(1 => $maxFileSize)
438 ),
439 'maxfilesize',
440 $maxFileSize * 1024 * 1024
441 );
442 $form->addElement('text', "attachDesc_$i", NULL, array(
443 'size' => 40,
444 'maxlength' => 255,
21dfd5f5 445 'placeholder' => ts('Description'),
0c56e4c8
TO
446 ));
447
448 if (!empty($tags)) {
449 $form->add('select', "tag_$i", ts('Tags'), $tags, FALSE,
450 array(
451 'id' => "tags_$i",
452 'multiple' => 'multiple',
453 'class' => 'huge crm-select2',
21dfd5f5 454 'placeholder' => ts('- none -'),
0c56e4c8
TO
455 )
456 );
457 }
458 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_file', NULL, FALSE, TRUE, "file_taglist_$i");
459 }
460 }
461
462 /**
100fef9d 463 * Return a clean url string and the number of attachment for a
0c56e4c8
TO
464 * given entityTable, entityID
465 *
5a4f6742
CW
466 * @param string $entityTable
467 * The entityTable to which the file is attached.
468 * @param int $entityID
469 * The id of the object in the above entityTable.
470 * @param string $separator
471 * The string separator where to implode the urls.
0c56e4c8 472 *
a6c01b45
CW
473 * @return array
474 * An array with 2 elements. The string and the number of attachments
0c56e4c8 475 */
00be9182 476 public static function attachmentInfo($entityTable, $entityID, $separator = '<br />') {
0c56e4c8
TO
477 if (!$entityID) {
478 return NULL;
479 }
480
481 $currentAttachments = self::getEntityFile($entityTable, $entityID);
482 if (!empty($currentAttachments)) {
483 $currentAttachmentURL = array();
484 foreach ($currentAttachments as $fileID => $attach) {
485 $currentAttachmentURL[] = $attach['href'];
486 }
487 return implode($separator, $currentAttachmentURL);
488 }
489 return NULL;
490 }
491
492 /**
493 * @param $formValues
c490a46a 494 * @param array $params
0c56e4c8 495 * @param $entityTable
100fef9d 496 * @param int $entityID
0c56e4c8 497 */
ae5ffbb7 498 public static function formatAttachment(
0c56e4c8
TO
499 &$formValues,
500 &$params,
501 $entityTable,
502 $entityID = NULL
503 ) {
504
505 // delete current attachments if applicable
506 if ($entityID && !empty($formValues['is_delete_attachment'])) {
507 CRM_Core_BAO_File::deleteEntityFile($entityTable, $entityID);
508 }
509
510 $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments');
511
512 $now = date('Ymdhis');
513
514 // setup all attachments
515 for ($i = 1; $i <= $numAttachments; $i++) {
516 $attachName = "attachFile_$i";
517 $attachDesc = "attachDesc_$i";
518 $attachTags = "tag_$i";
519 $attachFreeTags = "file_taglist_$i";
520 if (isset($formValues[$attachName]) && !empty($formValues[$attachName])) {
521 // add static tags if selects
522 $tagParams = array();
523 if (!empty($formValues[$attachTags])) {
524 foreach ($formValues[$attachTags] as $tag) {
525 $tagParams[$tag] = 1;
526 }
527 }
528
529 // we dont care if the file is empty or not
530 // CRM-7448
531 $fileParams = array(
532 'uri' => $formValues[$attachName]['name'],
533 'type' => $formValues[$attachName]['type'],
534 'location' => $formValues[$attachName]['name'],
535 'description' => $formValues[$attachDesc],
536 'upload_date' => $now,
537 'tag' => $tagParams,
21dfd5f5 538 'attachment_taglist' => CRM_Utils_Array::value($attachFreeTags, $formValues, array()),
0c56e4c8
TO
539 );
540
541 $params[$attachName] = $fileParams;
542 }
543 }
544 }
545
546 /**
c490a46a 547 * @param array $params
0c56e4c8 548 * @param $entityTable
100fef9d 549 * @param int $entityID
0c56e4c8 550 */
00be9182 551 public static function processAttachment(&$params, $entityTable, $entityID) {
0c56e4c8
TO
552 $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments');
553
554 for ($i = 1; $i <= $numAttachments; $i++) {
555 if (
556 isset($params["attachFile_$i"]) &&
557 is_array($params["attachFile_$i"])
558 ) {
559 self::filePostProcess(
560 $params["attachFile_$i"]['location'],
561 NULL,
562 $entityTable,
563 $entityID,
564 NULL,
565 TRUE,
566 $params["attachFile_$i"],
567 "attachFile_$i",
568 $params["attachFile_$i"]['type']
569 );
570 }
571 }
572 }
573
574 /**
575 * @return array
576 */
00be9182 577 public static function uploadNames() {
0c56e4c8
TO
578 $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments');
579
580 $names = array();
581 for ($i = 1; $i <= $numAttachments; $i++) {
582 $names[] = "attachFile_{$i}";
583 }
584 $names[] = 'uploadFile';
585 return $names;
586 }
587
d424ffde 588 /**
c490a46a 589 * copy/attach an existing file to a different entity
0c56e4c8 590 * table and id.
d424ffde 591 *
0c56e4c8 592 * @param $oldEntityTable
100fef9d 593 * @param int $oldEntityId
0c56e4c8 594 * @param $newEntityTable
100fef9d 595 * @param int $newEntityId
0c56e4c8 596 */
00be9182 597 public static function copyEntityFile($oldEntityTable, $oldEntityId, $newEntityTable, $newEntityId) {
6a488035
TO
598 $oldEntityFile = new CRM_Core_DAO_EntityFile();
599 $oldEntityFile->entity_id = $oldEntityId;
600 $oldEntityFile->entity_table = $oldEntityTable;
601 $oldEntityFile->find();
602
603 while ($oldEntityFile->fetch()) {
604 $newEntityFile = new CRM_Core_DAO_EntityFile();
605 $newEntityFile->entity_id = $newEntityId;
606 $newEntityFile->entity_table = $newEntityTable;
607 $newEntityFile->file_id = $oldEntityFile->file_id;
608 $newEntityFile->save();
609 }
610 }
611
0c56e4c8
TO
612 /**
613 * @param $entityTable
100fef9d
CW
614 * @param int $entityID
615 * @param int $fileID
0c56e4c8
TO
616 *
617 * @return string
618 */
00be9182 619 public static function deleteURLArgs($entityTable, $entityID, $fileID) {
6a488035 620 $params['entityTable'] = $entityTable;
0c56e4c8
TO
621 $params['entityID'] = $entityID;
622 $params['fileID'] = $fileID;
6a488035
TO
623
624 $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields);
625 $params['_sgn'] = $signer->sign($params);
626 return CRM_Utils_System::makeQueryString($params);
627 }
628
629 /**
100fef9d 630 * Delete a file attachment from an entity table / entity ID
6a488035 631 *
6a488035 632 */
00be9182 633 public static function deleteAttachment() {
0c56e4c8
TO
634 $params = array();
635 $params['entityTable'] = CRM_Utils_Request::retrieve('entityTable', 'String', CRM_Core_DAO::$_nullObject, TRUE);
636 $params['entityID'] = CRM_Utils_Request::retrieve('entityID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
637 $params['fileID'] = CRM_Utils_Request::retrieve('fileID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
6a488035 638
0c56e4c8 639 $signature = CRM_Utils_Request::retrieve('_sgn', 'String', CRM_Core_DAO::$_nullObject, TRUE);
6a488035
TO
640
641 $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields);
0c56e4c8 642 if (!$signer->validate($signature, $params)) {
6a488035
TO
643 CRM_Core_Error::fatal('Request signature is invalid');
644 }
645
646 CRM_Core_BAO_File::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']);
647 }
648
6a488035 649
34f51a07 650 /**
100fef9d 651 * Display paper icon for a file attachment -- CRM-13624
34f51a07 652 *
5a4f6742
CW
653 * @param string $entityTable
654 * The entityTable to which the file is attached. eg "civicrm_contact", "civicrm_note", "civicrm_activity".
06faabfa 655 * If you have the ID of a specific row in civicrm_file, use $entityTable='*'
5a4f6742
CW
656 * @param int $entityID
657 * The id of the object in the above entityTable.
1a7ab71e 658 *
72b3a70c
CW
659 * @return array|NULL
660 * list of HTML snippets; one HTML snippet for each attachment. If none found, then NULL
1a7ab71e 661 *
34f51a07 662 */
00be9182 663 public static function paperIconAttachment($entityTable, $entityID) {
0c56e4c8
TO
664 if (empty($entityTable) || !$entityID) {
665 $results = NULL;
666 return $results;
667 }
668 $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID);
669 foreach ($currentAttachmentInfo as $fileKey => $fileValue) {
34f51a07
N
670 $fileID = $fileValue['fileID'];
671 $fileType = $fileValue['mime_type'];
0c56e4c8 672 if ($fileID) {
34f51a07 673 if ($fileType == 'image/jpeg' ||
0c56e4c8
TO
674 $fileType == 'image/pjpeg' ||
675 $fileType == 'image/gif' ||
676 $fileType == 'image/x-png' ||
677 $fileType == 'image/png'
678 ) {
34f51a07
N
679 $url = $fileValue['url'];
680 $alt = $fileValue['cleanName'];
681 $file_url[$fileID] = "
682 <a href=\"$url\" class='crm-image-popup'>
683 <div class='icon paper-icon' title=\"$alt\" alt=\"$alt\"></div>
684 </a>";
685 // for non image files
686 }
687 else {
688 $url = $fileValue['url'];
689 $alt = $fileValue['cleanName'];
690 $file_url[$fileID] = "<a href=\"$url\"><div class='icon paper-icon' title=\"$alt\" alt=\"$alt\"></div></a>";
691 }
692 }
693 }
0c56e4c8
TO
694 if (empty($file_url)) {
695 $results = NULL;
34f51a07
N
696 }
697 else {
0c56e4c8 698 $results = $file_url;
34f51a07
N
699 }
700 return $results;
701 }
6cccc6d4
TO
702
703 /**
704 * Get a reference to the file-search service (if one is available).
705 *
706 * @return CRM_Core_FileSearchInterface|NULL
707 */
00be9182 708 public static function getSearchService() {
6cccc6d4
TO
709 $fileSearches = array();
710 CRM_Utils_Hook::fileSearches($fileSearches);
711
712 // use the first available search
713 foreach ($fileSearches as $fileSearch) {
714 /** @var $fileSearch CRM_Core_FileSearchInterface */
715 return $fileSearch;
716 }
717 return NULL;
718 }
96025800 719
b2aaa85e 720}