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