CRM-19948: Store the logged in contact ID as the file uploader
[civicrm-core.git] / CRM / Core / BAO / File.php
CommitLineData
6a488035 1<?php
0c56e4c8
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
0c56e4c8 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
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) {
b93376a8 100 CRM_Core_Error::statusBounce(ts('Mime Type is now a required parameter for file upload'));
0c56e4c8
TO
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)) {
b93376a8 119 CRM_Core_Error::statusBounce(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;
77ca21d6 148 $fileDAO->upload_date = date('YmdHis');
0c56e4c8
TO
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'])) {
1273d77c 168 CRM_Core_Form_Tag::postProcess($fileParams['attachment_taglist'], $entityFileDAO->id, 'civicrm_file');
0c56e4c8
TO
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 /**
ad37ac8e 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
0c56e4c8
TO
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)) {
ecdae719 206 CRM_Core_Error::fatal(sprintf('No record found for given file ID - %d and entity ID - %d', $fileID, $entityID));
0c56e4c8
TO
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
0527cd81 220 *
353ffa53
TO
221 * public function delete($useWhere = FALSE) {
222 * list($fileID, $entityID, $fieldID) = func_get_args();
223 *
224 * self::deleteFileReferences($fileID, $entityID, $fieldID);
225 * } */
0c56e4c8
TO
226
227 /**
ad37ac8e 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 *
7b8bc7a1
SB
235 * @return bool
236 * Was file deleted?
0c56e4c8 237 */
00be9182 238 public static function deleteEntityFile($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
7b8bc7a1 239 $isDeleted = FALSE;
0c56e4c8 240 if (empty($entityTable) || empty($entityID)) {
7b8bc7a1 241 return $isDeleted;
0c56e4c8
TO
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);
7b8bc7a1 260 $isDeleted = TRUE;
0c56e4c8
TO
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',
21dfd5f5 270 'entity_id' => $fId,
0c56e4c8
TO
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 }
7b8bc7a1 286 $isDeleted = TRUE;
0c56e4c8 287 }
7b8bc7a1 288 return $isDeleted;
0c56e4c8
TO
289 }
290
291 /**
ad37ac8e 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
0c56e4c8 299 */
00be9182 300 public static function getEntityFile($entityTable, $entityID, $addDeleteArgs = FALSE) {
0c56e4c8
TO
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;
06faabfa 319 $result['url'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$dao->cfID}&eid={$dao->entity_id}");
0c56e4c8
TO
320 $result['href'] = "<a href=\"{$result['url']}\">{$result['cleanName']}</a>";
321 $result['tag'] = CRM_Core_BAO_EntityTag::getTag($dao->cfID, 'civicrm_file');
30a0455f 322 $result['icon'] = CRM_Utils_File::getIconFromMimeType($dao->mime_type);
0c56e4c8 323 if ($addDeleteArgs) {
06faabfa 324 $result['deleteURLArgs'] = self::deleteURLArgs($dao->entity_table, $dao->entity_id, $dao->cfID);
0c56e4c8
TO
325 }
326 $results[$dao->cfID] = $result;
327 }
328
329 //fix tag names
330 $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
331
332 foreach ($results as &$values) {
333 if (!empty($values['tag'])) {
334 $tagNames = array();
335 foreach ($values['tag'] as $tid) {
336 $tagNames[] = $tags[$tid];
337 }
338 $values['tag'] = implode(', ', $tagNames);
339 }
340 else {
341 $values['tag'] = '';
342 }
343 }
344
345 $dao->free();
346 return $results;
347 }
348
349 /**
6a0b768e
TO
350 * @param string $entityTable
351 * Table-name or "*" (to reference files directly by file-id).
06faabfa 352 * @param int $entityID
100fef9d
CW
353 * @param int $fileTypeID
354 * @param int $fileID
0c56e4c8
TO
355 *
356 * @return array
357 */
00be9182 358 public static function sql($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
06faabfa
TO
359 if ($entityTable == '*') {
360 // $entityID is the ID of a specific file
361 $sql = "
362SELECT CF.id as cfID,
363 CF.uri as uri,
364 CF.mime_type as mime_type,
365 CF.description as description,
366 CEF.id as cefID,
367 CEF.entity_table as entity_table,
368 CEF.entity_id as entity_id
369FROM civicrm_file AS CF
370LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id )
371WHERE CF.id = %2";
372
0db6c3e1
TO
373 }
374 else {
06faabfa 375 $sql = "
0c56e4c8 376SELECT CF.id as cfID,
6a488035
TO
377 CF.uri as uri,
378 CF.mime_type as mime_type,
379 CF.description as description,
06faabfa
TO
380 CEF.id as cefID,
381 CEF.entity_table as entity_table,
382 CEF.entity_id as entity_id
0c56e4c8
TO
383FROM civicrm_file AS CF
384LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id )
385WHERE CEF.entity_table = %1
386AND CEF.entity_id = %2";
06faabfa 387 }
0c56e4c8
TO
388
389 $params = array(
390 1 => array($entityTable, 'String'),
391 2 => array($entityID, 'Integer'),
392 );
393
394 if ($fileTypeID !== NULL) {
395 $sql .= " AND CF.file_type_id = %3";
396 $params[3] = array($fileTypeID, 'Integer');
397 }
398
399 if ($fileID !== NULL) {
400 $sql .= " AND CF.id = %4";
401 $params[4] = array($fileID, 'Integer');
402 }
403
404 return array($sql, $params);
405 }
406
407 /**
75fd1335
TO
408 * @param CRM_Core_Form $form
409 * @param string $entityTable
100fef9d 410 * @param int $entityID
0c56e4c8
TO
411 * @param null $numAttachments
412 * @param bool $ajaxDelete
413 */
00be9182 414 public static function buildAttachment(&$form, $entityTable, $entityID = NULL, $numAttachments = NULL, $ajaxDelete = FALSE) {
0c56e4c8
TO
415
416 if (!$numAttachments) {
aaffa79f 417 $numAttachments = Civi::settings()->get('max_attachments');
0c56e4c8
TO
418 }
419 // Assign maxAttachments count to template for help message
420 $form->assign('maxAttachments', $numAttachments);
421
422 $config = CRM_Core_Config::singleton();
423 // set default max file size as 2MB
424 $maxFileSize = $config->maxFileSize ? $config->maxFileSize : 2;
425
426 $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID, TRUE);
427 $totalAttachments = 0;
428 if ($currentAttachmentInfo) {
429 $totalAttachments = count($currentAttachmentInfo);
430 $form->add('checkbox', 'is_delete_attachment', ts('Delete All Attachment(s)'));
431 $form->assign('currentAttachmentInfo', $currentAttachmentInfo);
432 }
433 else {
434 $form->assign('currentAttachmentInfo', NULL);
435 }
436
437 if ($totalAttachments) {
438 if ($totalAttachments >= $numAttachments) {
439 $numAttachments = 0;
440 }
441 else {
442 $numAttachments -= $totalAttachments;
443 }
444 }
445
446 $form->assign('numAttachments', $numAttachments);
447
e0f9d6a2 448 CRM_Core_BAO_Tag::getTags('civicrm_file', $tags, NULL,
449 '&nbsp;&nbsp;', TRUE);
0c56e4c8
TO
450
451 // get tagset info
452 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_file');
453
454 // add attachments
455 for ($i = 1; $i <= $numAttachments; $i++) {
d00e9ef0 456 $form->addElement('file', "attachFile_$i", ts('Attach File'), 'size=30 maxlength=221');
0c56e4c8
TO
457 $form->addUploadElement("attachFile_$i");
458 $form->setMaxFileSize($maxFileSize * 1024 * 1024);
459 $form->addRule("attachFile_$i",
460 ts('File size should be less than %1 MByte(s)',
461 array(1 => $maxFileSize)
462 ),
463 'maxfilesize',
464 $maxFileSize * 1024 * 1024
465 );
466 $form->addElement('text', "attachDesc_$i", NULL, array(
467 'size' => 40,
468 'maxlength' => 255,
21dfd5f5 469 'placeholder' => ts('Description'),
0c56e4c8
TO
470 ));
471
472 if (!empty($tags)) {
473 $form->add('select', "tag_$i", ts('Tags'), $tags, FALSE,
474 array(
475 'id' => "tags_$i",
476 'multiple' => 'multiple',
477 'class' => 'huge crm-select2',
21dfd5f5 478 'placeholder' => ts('- none -'),
0c56e4c8
TO
479 )
480 );
481 }
482 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_file', NULL, FALSE, TRUE, "file_taglist_$i");
483 }
484 }
485
486 /**
100fef9d 487 * Return a clean url string and the number of attachment for a
0c56e4c8
TO
488 * given entityTable, entityID
489 *
5a4f6742
CW
490 * @param string $entityTable
491 * The entityTable to which the file is attached.
492 * @param int $entityID
493 * The id of the object in the above entityTable.
494 * @param string $separator
495 * The string separator where to implode the urls.
0c56e4c8 496 *
a6c01b45
CW
497 * @return array
498 * An array with 2 elements. The string and the number of attachments
0c56e4c8 499 */
00be9182 500 public static function attachmentInfo($entityTable, $entityID, $separator = '<br />') {
0c56e4c8
TO
501 if (!$entityID) {
502 return NULL;
503 }
504
505 $currentAttachments = self::getEntityFile($entityTable, $entityID);
506 if (!empty($currentAttachments)) {
507 $currentAttachmentURL = array();
508 foreach ($currentAttachments as $fileID => $attach) {
509 $currentAttachmentURL[] = $attach['href'];
510 }
511 return implode($separator, $currentAttachmentURL);
512 }
513 return NULL;
514 }
515
516 /**
517 * @param $formValues
c490a46a 518 * @param array $params
0c56e4c8 519 * @param $entityTable
100fef9d 520 * @param int $entityID
0c56e4c8 521 */
ae5ffbb7 522 public static function formatAttachment(
0c56e4c8
TO
523 &$formValues,
524 &$params,
525 $entityTable,
526 $entityID = NULL
527 ) {
528
529 // delete current attachments if applicable
530 if ($entityID && !empty($formValues['is_delete_attachment'])) {
531 CRM_Core_BAO_File::deleteEntityFile($entityTable, $entityID);
532 }
533
aaffa79f 534 $numAttachments = Civi::settings()->get('max_attachments');
0c56e4c8 535
0c56e4c8
TO
536 // setup all attachments
537 for ($i = 1; $i <= $numAttachments; $i++) {
538 $attachName = "attachFile_$i";
539 $attachDesc = "attachDesc_$i";
540 $attachTags = "tag_$i";
541 $attachFreeTags = "file_taglist_$i";
542 if (isset($formValues[$attachName]) && !empty($formValues[$attachName])) {
543 // add static tags if selects
544 $tagParams = array();
545 if (!empty($formValues[$attachTags])) {
546 foreach ($formValues[$attachTags] as $tag) {
547 $tagParams[$tag] = 1;
548 }
549 }
550
551 // we dont care if the file is empty or not
552 // CRM-7448
90a73810 553 $extraParams = array(
0c56e4c8 554 'description' => $formValues[$attachDesc],
0c56e4c8 555 'tag' => $tagParams,
21dfd5f5 556 'attachment_taglist' => CRM_Utils_Array::value($attachFreeTags, $formValues, array()),
0c56e4c8
TO
557 );
558
90a73810 559 CRM_Utils_File::formatFile($formValues, $attachName, $extraParams);
cde03305 560
561 // set the formatted attachment attributes to $params, later used by
562 // CRM_Activity_BAO_Activity::sendEmail(...) to send mail with desired attachments
563 if (!empty($formValues[$attachName])) {
564 $params[$attachName] = $formValues[$attachName];
565 }
0c56e4c8
TO
566 }
567 }
568 }
569
570 /**
c490a46a 571 * @param array $params
0c56e4c8 572 * @param $entityTable
100fef9d 573 * @param int $entityID
0c56e4c8 574 */
00be9182 575 public static function processAttachment(&$params, $entityTable, $entityID) {
aaffa79f 576 $numAttachments = Civi::settings()->get('max_attachments');
0c56e4c8
TO
577
578 for ($i = 1; $i <= $numAttachments; $i++) {
579 if (
580 isset($params["attachFile_$i"]) &&
581 is_array($params["attachFile_$i"])
582 ) {
583 self::filePostProcess(
584 $params["attachFile_$i"]['location'],
585 NULL,
586 $entityTable,
587 $entityID,
588 NULL,
589 TRUE,
590 $params["attachFile_$i"],
591 "attachFile_$i",
592 $params["attachFile_$i"]['type']
593 );
594 }
595 }
596 }
597
598 /**
599 * @return array
600 */
00be9182 601 public static function uploadNames() {
aaffa79f 602 $numAttachments = Civi::settings()->get('max_attachments');
0c56e4c8
TO
603
604 $names = array();
605 for ($i = 1; $i <= $numAttachments; $i++) {
606 $names[] = "attachFile_{$i}";
607 }
608 $names[] = 'uploadFile';
609 return $names;
610 }
611
d424ffde 612 /**
c490a46a 613 * copy/attach an existing file to a different entity
0c56e4c8 614 * table and id.
d424ffde 615 *
0c56e4c8 616 * @param $oldEntityTable
100fef9d 617 * @param int $oldEntityId
0c56e4c8 618 * @param $newEntityTable
100fef9d 619 * @param int $newEntityId
0c56e4c8 620 */
00be9182 621 public static function copyEntityFile($oldEntityTable, $oldEntityId, $newEntityTable, $newEntityId) {
6a488035
TO
622 $oldEntityFile = new CRM_Core_DAO_EntityFile();
623 $oldEntityFile->entity_id = $oldEntityId;
624 $oldEntityFile->entity_table = $oldEntityTable;
625 $oldEntityFile->find();
626
627 while ($oldEntityFile->fetch()) {
628 $newEntityFile = new CRM_Core_DAO_EntityFile();
629 $newEntityFile->entity_id = $newEntityId;
630 $newEntityFile->entity_table = $newEntityTable;
631 $newEntityFile->file_id = $oldEntityFile->file_id;
632 $newEntityFile->save();
633 }
634 }
635
0c56e4c8
TO
636 /**
637 * @param $entityTable
100fef9d
CW
638 * @param int $entityID
639 * @param int $fileID
0c56e4c8
TO
640 *
641 * @return string
642 */
00be9182 643 public static function deleteURLArgs($entityTable, $entityID, $fileID) {
6a488035 644 $params['entityTable'] = $entityTable;
0c56e4c8
TO
645 $params['entityID'] = $entityID;
646 $params['fileID'] = $fileID;
6a488035
TO
647
648 $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields);
649 $params['_sgn'] = $signer->sign($params);
650 return CRM_Utils_System::makeQueryString($params);
651 }
652
653 /**
100fef9d 654 * Delete a file attachment from an entity table / entity ID
6a488035 655 *
6a488035 656 */
00be9182 657 public static function deleteAttachment() {
0c56e4c8
TO
658 $params = array();
659 $params['entityTable'] = CRM_Utils_Request::retrieve('entityTable', 'String', CRM_Core_DAO::$_nullObject, TRUE);
660 $params['entityID'] = CRM_Utils_Request::retrieve('entityID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
661 $params['fileID'] = CRM_Utils_Request::retrieve('fileID', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
6a488035 662
0c56e4c8 663 $signature = CRM_Utils_Request::retrieve('_sgn', 'String', CRM_Core_DAO::$_nullObject, TRUE);
6a488035
TO
664
665 $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), self::$_signableFields);
0c56e4c8 666 if (!$signer->validate($signature, $params)) {
6a488035
TO
667 CRM_Core_Error::fatal('Request signature is invalid');
668 }
669
33d245c8 670 self::deleteEntityFile($params['entityTable'], $params['entityID'], NULL, $params['fileID']);
6a488035
TO
671 }
672
6a488035 673
34f51a07 674 /**
100fef9d 675 * Display paper icon for a file attachment -- CRM-13624
34f51a07 676 *
5a4f6742
CW
677 * @param string $entityTable
678 * The entityTable to which the file is attached. eg "civicrm_contact", "civicrm_note", "civicrm_activity".
06faabfa 679 * If you have the ID of a specific row in civicrm_file, use $entityTable='*'
5a4f6742
CW
680 * @param int $entityID
681 * The id of the object in the above entityTable.
1a7ab71e 682 *
72b3a70c
CW
683 * @return array|NULL
684 * list of HTML snippets; one HTML snippet for each attachment. If none found, then NULL
1a7ab71e 685 *
34f51a07 686 */
00be9182 687 public static function paperIconAttachment($entityTable, $entityID) {
0c56e4c8
TO
688 if (empty($entityTable) || !$entityID) {
689 $results = NULL;
690 return $results;
691 }
692 $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID);
693 foreach ($currentAttachmentInfo as $fileKey => $fileValue) {
34f51a07 694 $fileID = $fileValue['fileID'];
0c56e4c8 695 if ($fileID) {
b9773de0
CW
696 $fileType = $fileValue['mime_type'];
697 $url = $fileValue['url'];
698 $title = $fileValue['cleanName'];
34f51a07 699 if ($fileType == 'image/jpeg' ||
0c56e4c8
TO
700 $fileType == 'image/pjpeg' ||
701 $fileType == 'image/gif' ||
702 $fileType == 'image/x-png' ||
703 $fileType == 'image/png'
704 ) {
34f51a07 705 $file_url[$fileID] = "
b9773de0
CW
706 <a href='$url' class='crm-image-popup' title='$title'>
707 <i class='crm-i fa-file-image-o'></i>
34f51a07 708 </a>";
34f51a07 709 }
b9773de0 710 // for non image files
34f51a07 711 else {
b9773de0
CW
712 $file_url[$fileID] = "
713 <a href='$url' title='$title'>
714 <i class='crm-i fa-paperclip'></i>
715 </a>";
34f51a07
N
716 }
717 }
718 }
0c56e4c8
TO
719 if (empty($file_url)) {
720 $results = NULL;
34f51a07
N
721 }
722 else {
0c56e4c8 723 $results = $file_url;
34f51a07
N
724 }
725 return $results;
726 }
6cccc6d4
TO
727
728 /**
729 * Get a reference to the file-search service (if one is available).
730 *
731 * @return CRM_Core_FileSearchInterface|NULL
732 */
00be9182 733 public static function getSearchService() {
6cccc6d4
TO
734 $fileSearches = array();
735 CRM_Utils_Hook::fileSearches($fileSearches);
736
737 // use the first available search
738 foreach ($fileSearches as $fileSearch) {
739 /** @var $fileSearch CRM_Core_FileSearchInterface */
740 return $fileSearch;
741 }
742 return NULL;
743 }
96025800 744
b2aaa85e 745}