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