- /**
- *
- * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
- * $Id$
- *
- */
-
- /**
- * BAO object for crm_log table
- */
- class CRM_Core_BAO_File extends CRM_Core_DAO_File {
-
- static $_signableFields = array('entityTable', 'entityID', 'fileID');
-
- static function path($fileID, $entityID, $entityTable = NULL) {
- $entityFileDAO = new CRM_Core_DAO_EntityFile();
- if ($entityTable) {
- $entityFileDAO->entity_table = $entityTable;
- }
- $entityFileDAO->entity_id = $entityID;
- $entityFileDAO->file_id = $fileID;
-
- if ($entityFileDAO->find(TRUE)) {
- $fileDAO = new CRM_Core_DAO_File();
- $fileDAO->id = $fileID;
- if ($fileDAO->find(TRUE)) {
- $config = CRM_Core_Config::singleton();
- $path = $config->customFileUploadDir . $fileDAO->uri;
-
- if (file_exists($path) && is_readable($path)) {
- return array($path, $fileDAO->mime_type);
- }
- }
- }
-
- return array(NULL, NULL);
- }
-
-
- static function filePostProcess(
- $data,
- $fileTypeID,
- $entityTable,
- $entityID,
- $entitySubtype,
- $overwrite = TRUE,
- $fileParams = NULL,
- $uploadName = 'uploadFile',
- $mimeType = null
- ) {
- if (!$mimeType) {
- CRM_Core_Error::fatal(ts('Mime Type is now a required parameter'));
- }
-
- $config = CRM_Core_Config::singleton();
-
- $path = explode('/', $data);
- $filename = $path[count($path) - 1];
-
- // rename this file to go into the secure directory
- if ($entitySubtype) {
- $directoryName = $config->customFileUploadDir . $entitySubtype . DIRECTORY_SEPARATOR . $entityID;
- }
- else {
- $directoryName = $config->customFileUploadDir;
- }
-
- CRM_Utils_File::createDir($directoryName);
-
- if (!rename($data, $directoryName . DIRECTORY_SEPARATOR . $filename)) {
- CRM_Core_Error::fatal(ts('Could not move custom file to custom upload directory'));
- break;
- }
-
- // to get id's
- if ($overwrite && $fileTypeID) {
- list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID);
- }
- else {
- list($sql, $params) = self::sql($entityTable, $entityID, 0);
- }
-
- $dao = CRM_Core_DAO::executeQuery($sql, $params);
- $dao->fetch();
-
- $fileDAO = new CRM_Core_DAO_File();
- $op = 'create';
- if (isset($dao->cfID) && $dao->cfID) {
- $op = 'edit';
- $fileDAO->id = $dao->cfID;
- unlink($directoryName . DIRECTORY_SEPARATOR . $dao->uri);
- }
-
- if (!empty($fileParams)) {
- $fileDAO->copyValues($fileParams);
- }
-
- $fileDAO->uri = $filename;
- $fileDAO->mime_type = $mimeType;
- $fileDAO->file_type_id = $fileTypeID;
- $fileDAO->upload_date = date('Ymdhis');
- $fileDAO->save();
-
- // need to add/update civicrm_entity_file
- $entityFileDAO = new CRM_Core_DAO_EntityFile();
- if (isset($dao->cefID) && $dao->cefID) {
- $entityFileDAO->id = $dao->cefID;
- }
- $entityFileDAO->entity_table = $entityTable;
- $entityFileDAO->entity_id = $entityID;
- $entityFileDAO->file_id = $fileDAO->id;
- $entityFileDAO->save();
-
- //save static tags
- if (!empty($fileParams['tag'])) {
- CRM_Core_BAO_EntityTag::create($fileParams['tag'], 'civicrm_file', $entityFileDAO->id);
- }
-
- //save free tags
- if (isset($fileParams['attachment_taglist']) && !empty($fileParams['attachment_taglist'])) {
- CRM_Core_Form_Tag::postProcess($fileParams['attachment_taglist'], $entityFileDAO->id, 'civicrm_file', CRM_Core_DAO::$_nullObject);
- }
-
- // lets call the post hook here so attachments code can do the right stuff
- CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO);
- }
-
- /**
- * A static function wrapper that deletes the various objects that are
- * connected to a file object (i.e. file, entityFile and customValue
- */
- public static function deleteFileReferences($fileID, $entityID, $fieldID) {
- $fileDAO = new CRM_Core_DAO_File();
- $fileDAO->id = $fileID;
- if (!$fileDAO->find(TRUE)) {
- CRM_Core_Error::fatal();
- }
-
- // lets call a pre hook before the delete, so attachments hooks can get the info before things
- // disappear
- CRM_Utils_Hook::pre('delete', 'File', $fileID, $fileDAO);
-
- // get the table and column name
- list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID);
-
- $entityFileDAO = new CRM_Core_DAO_EntityFile();
- $entityFileDAO->file_id = $fileID;
- $entityFileDAO->entity_id = $entityID;
- $entityFileDAO->entity_table = $tableName;
-
- if (!$entityFileDAO->find(TRUE)) {
- CRM_Core_Error::fatal();
- }
-
- $entityFileDAO->delete();
- $fileDAO->delete();
-
- // also set the value to null of the table and column
- $query = "UPDATE $tableName SET $columnName = null WHERE $columnName = %1";
- $params = array(1 => array($fileID, 'Integer'));
- CRM_Core_DAO::executeQuery($query, $params);
- }
-
- /**
- * The $useWhere is used so that the signature matches the parent class
- */
- public function delete($useWhere = false) {
- list($fileID, $entityID, $fieldID) = func_get_args();
-
- self::deleteFileReferences($fileID, $entityID, $fieldID);
- }
-
- /**
- * delete all the files and associated object associated with this
- * combination
- */
- static function deleteEntityFile($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
- if (empty($entityTable) || empty($entityID)) {
- return;
- }
-
- $config = CRM_Core_Config::singleton();
-
- list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID, $fileID);
- $dao = CRM_Core_DAO::executeQuery($sql, $params);
-
- $cfIDs = array();
- $cefIDs = array();
- while ($dao->fetch()) {
- $cfIDs[$dao->cfID] = $dao->uri;
- $cefIDs[] = $dao->cefID;
- }
-
- if (!empty($cefIDs)) {
- $cefIDs = implode(',', $cefIDs);
- $sql = "DELETE FROM civicrm_entity_file where id IN ( $cefIDs )";
- CRM_Core_DAO::executeQuery($sql);
- }
-
- if (!empty($cfIDs)) {
- // Delete file only if there no any entity using this file.
- $deleteFiles = array();
- foreach ($cfIDs as $fId => $fUri) {
- //delete tags from entity tag table
- $tagParams = array(
- 'entity_table' => 'civicrm_file',
- 'entity_id' => $fId
- );
-
- CRM_Core_BAO_EntityTag::del($tagParams);
-
- if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fId, 'id', 'file_id')) {
- unlink($config->customFileUploadDir . DIRECTORY_SEPARATOR . $fUri);
- $deleteFiles[$fId] = $fId;
- }
- }
-
- if (!empty($deleteFiles)) {
- $deleteFiles = implode(',', $deleteFiles);
- $sql = "DELETE FROM civicrm_file where id IN ( $deleteFiles )";
- CRM_Core_DAO::executeQuery($sql);
- }
- }
- }
-
- /**
- * get all the files and associated object associated with this
- * combination
- */
- static function getEntityFile($entityTable, $entityID, $addDeleteArgs = false) {
- if (empty($entityTable) || !$entityID) {
- $results = NULL;
- return $results;
- }
-
- $config = CRM_Core_Config::singleton();
-
- list($sql, $params) = self::sql($entityTable, $entityID, NULL);
- $dao = CRM_Core_DAO::executeQuery($sql, $params);
- $results = array();
- while ($dao->fetch()) {
- $result['fileID'] = $dao->cfID;
- $result['entityID'] = $dao->cefID;
- $result['mime_type'] = $dao->mime_type;
- $result['fileName'] = $dao->uri;
- $result['description'] = $dao->description;
- $result['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri);
- $result['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri;
- $result['url'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$dao->cfID}&eid={$entityID}");
- $result['href'] = "<a href=\"{$result['url']}\">{$result['cleanName']}</a>";
- $result['tag'] = CRM_Core_BAO_EntityTag::getTag($dao->cfID, 'civicrm_file');
- if ($addDeleteArgs) {
- $result['deleteURLArgs'] = self::deleteURLArgs($entityTable, $entityID, $dao->cfID);
- }
- $results[$dao->cfID] = $result;
- }
-
- //fix tag names
- $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
-
- foreach($results as &$values) {
- if (!empty($values['tag'])) {
- $tagNames = array();
- foreach( $values['tag'] as $tid ) {
- $tagNames[] = $tags[$tid];
- }
- $values['tag'] = implode(', ', $tagNames);
- } else {
- $values['tag'] = '';
- }
- }
-
- $dao->free();
- return $results;
- }
-
- static function sql($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
- $sql = "
- SELECT CF.id as cfID,
+/**
+ * BAO object for crm_log table
+ */
+class CRM_Core_BAO_File extends CRM_Core_DAO_File {
+
+ static $_signableFields = array('entityTable', 'entityID', 'fileID');
+
+ /**
+ * @param $fileID
+ * @param $entityID
+ * @param null $entityTable
+ *
+ * @return array
+ */
+ static function path($fileID, $entityID, $entityTable = NULL) {
+ $entityFileDAO = new CRM_Core_DAO_EntityFile();
+ if ($entityTable) {
+ $entityFileDAO->entity_table = $entityTable;
+ }
+ $entityFileDAO->entity_id = $entityID;
+ $entityFileDAO->file_id = $fileID;
+
+ if ($entityFileDAO->find(TRUE)) {
+ $fileDAO = new CRM_Core_DAO_File();
+ $fileDAO->id = $fileID;
+ if ($fileDAO->find(TRUE)) {
+ $config = CRM_Core_Config::singleton();
+ $path = $config->customFileUploadDir . $fileDAO->uri;
+
+ if (file_exists($path) && is_readable($path)) {
+ return array($path, $fileDAO->mime_type);
+ }
+ }
+ }
+
+ return array(NULL, NULL);
+ }
+
+
+ /**
+ * @param $data
+ * @param $fileTypeID
+ * @param $entityTable
+ * @param $entityID
+ * @param $entitySubtype
+ * @param bool $overwrite
+ * @param null|array $fileParams
+ * @param string $uploadName
+ * @param null $mimeType
+ *
+ * @throws Exception
+ */
+ static function filePostProcess(
+ $data,
+ $fileTypeID,
+ $entityTable,
+ $entityID,
+ $entitySubtype,
+ $overwrite = TRUE,
+ $fileParams = NULL,
+ $uploadName = 'uploadFile',
+ $mimeType = NULL
+ ) {
+ if (!$mimeType) {
+ CRM_Core_Error::fatal(ts('Mime Type is now a required parameter'));
+ }
+
+ $config = CRM_Core_Config::singleton();
+
+ $path = explode('/', $data);
+ $filename = $path[count($path) - 1];
+
+ // rename this file to go into the secure directory
+ if ($entitySubtype) {
+ $directoryName = $config->customFileUploadDir . $entitySubtype . DIRECTORY_SEPARATOR . $entityID;
+ }
+ else {
+ $directoryName = $config->customFileUploadDir;
+ }
+
+ CRM_Utils_File::createDir($directoryName);
+
+ if (!rename($data, $directoryName . DIRECTORY_SEPARATOR . $filename)) {
+ CRM_Core_Error::fatal(ts('Could not move custom file to custom upload directory'));
+ }
+
+ // to get id's
+ if ($overwrite && $fileTypeID) {
+ list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID);
+ }
+ else {
+ list($sql, $params) = self::sql($entityTable, $entityID, 0);
+ }
+
+ $dao = CRM_Core_DAO::executeQuery($sql, $params);
+ $dao->fetch();
+
+ $fileDAO = new CRM_Core_DAO_File();
+ $op = 'create';
+ if (isset($dao->cfID) && $dao->cfID) {
+ $op = 'edit';
+ $fileDAO->id = $dao->cfID;
+ unlink($directoryName . DIRECTORY_SEPARATOR . $dao->uri);
+ }
+
+ if (!empty($fileParams)) {
+ $fileDAO->copyValues($fileParams);
+ }
+
+ $fileDAO->uri = $filename;
+ $fileDAO->mime_type = $mimeType;
+ $fileDAO->file_type_id = $fileTypeID;
+ $fileDAO->upload_date = date('Ymdhis');
+ $fileDAO->save();
+
+ // need to add/update civicrm_entity_file
+ $entityFileDAO = new CRM_Core_DAO_EntityFile();
+ if (isset($dao->cefID) && $dao->cefID) {
+ $entityFileDAO->id = $dao->cefID;
+ }
+ $entityFileDAO->entity_table = $entityTable;
+ $entityFileDAO->entity_id = $entityID;
+ $entityFileDAO->file_id = $fileDAO->id;
+ $entityFileDAO->save();
+
+ //save static tags
+ if (!empty($fileParams['tag'])) {
+ CRM_Core_BAO_EntityTag::create($fileParams['tag'], 'civicrm_file', $entityFileDAO->id);
+ }
+
+ //save free tags
+ if (isset($fileParams['attachment_taglist']) && !empty($fileParams['attachment_taglist'])) {
+ CRM_Core_Form_Tag::postProcess($fileParams['attachment_taglist'], $entityFileDAO->id, 'civicrm_file', CRM_Core_DAO::$_nullObject);
+ }
+
+ // lets call the post hook here so attachments code can do the right stuff
+ CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO);
+ }
+
+ /**
+ * A static function wrapper that deletes the various objects that are
+ * connected to a file object (i.e. file, entityFile and customValue
+ */
+ public static function deleteFileReferences($fileID, $entityID, $fieldID) {
+ $fileDAO = new CRM_Core_DAO_File();
+ $fileDAO->id = $fileID;
+ if (!$fileDAO->find(TRUE)) {
+ CRM_Core_Error::fatal();
+ }
+
+ // lets call a pre hook before the delete, so attachments hooks can get the info before things
+ // disappear
+ CRM_Utils_Hook::pre('delete', 'File', $fileID, $fileDAO);
+
+ // get the table and column name
+ list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID);
+
+ $entityFileDAO = new CRM_Core_DAO_EntityFile();
+ $entityFileDAO->file_id = $fileID;
+ $entityFileDAO->entity_id = $entityID;
+ $entityFileDAO->entity_table = $tableName;
+
+ if (!$entityFileDAO->find(TRUE)) {
+ CRM_Core_Error::fatal();
+ }
+
+ $entityFileDAO->delete();
+ $fileDAO->delete();
+
+ // also set the value to null of the table and column
+ $query = "UPDATE $tableName SET $columnName = null WHERE $columnName = %1";
+ $params = array(1 => array($fileID, 'Integer'));
+ CRM_Core_DAO::executeQuery($query, $params);
+ }
+
+ /**
+ * The $useWhere is used so that the signature matches the parent class
+ */
+ public function delete($useWhere = FALSE) {
+ list($fileID, $entityID, $fieldID) = func_get_args();
+
+ self::deleteFileReferences($fileID, $entityID, $fieldID);
+ }
+
+ /**
+ * delete all the files and associated object associated with this
+ * combination
+ */
+ static function deleteEntityFile($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
+ if (empty($entityTable) || empty($entityID)) {
+ return;
+ }
+
+ $config = CRM_Core_Config::singleton();
+
+ list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID, $fileID);
+ $dao = CRM_Core_DAO::executeQuery($sql, $params);
+
+ $cfIDs = array();
+ $cefIDs = array();
+ while ($dao->fetch()) {
+ $cfIDs[$dao->cfID] = $dao->uri;
+ $cefIDs[] = $dao->cefID;
+ }
+
+ if (!empty($cefIDs)) {
+ $cefIDs = implode(',', $cefIDs);
+ $sql = "DELETE FROM civicrm_entity_file where id IN ( $cefIDs )";
+ CRM_Core_DAO::executeQuery($sql);
+ }
+
+ if (!empty($cfIDs)) {
+ // Delete file only if there no any entity using this file.
+ $deleteFiles = array();
+ foreach ($cfIDs as $fId => $fUri) {
+ //delete tags from entity tag table
+ $tagParams = array(
+ 'entity_table' => 'civicrm_file',
+ 'entity_id' => $fId
+ );
+
+ CRM_Core_BAO_EntityTag::del($tagParams);
+
+ if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fId, 'id', 'file_id')) {
+ unlink($config->customFileUploadDir . DIRECTORY_SEPARATOR . $fUri);
+ $deleteFiles[$fId] = $fId;
+ }
+ }
+
+ if (!empty($deleteFiles)) {
+ $deleteFiles = implode(',', $deleteFiles);
+ $sql = "DELETE FROM civicrm_file where id IN ( $deleteFiles )";
+ CRM_Core_DAO::executeQuery($sql);
+ }
+ }
+ }
+
+ /**
+ * get all the files and associated object associated with this
+ * combination
+ */
+ static function getEntityFile($entityTable, $entityID, $addDeleteArgs = FALSE) {
+ if (empty($entityTable) || !$entityID) {
+ $results = NULL;
+ return $results;
+ }
+
+ $config = CRM_Core_Config::singleton();
+
+ list($sql, $params) = self::sql($entityTable, $entityID, NULL);
+ $dao = CRM_Core_DAO::executeQuery($sql, $params);
+ $results = array();
+ while ($dao->fetch()) {
+ $result['fileID'] = $dao->cfID;
+ $result['entityID'] = $dao->cefID;
+ $result['mime_type'] = $dao->mime_type;
+ $result['fileName'] = $dao->uri;
+ $result['description'] = $dao->description;
+ $result['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri);
+ $result['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri;
+ $result['url'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$dao->cfID}&eid={$dao->entity_id}");
+ $result['href'] = "<a href=\"{$result['url']}\">{$result['cleanName']}</a>";
+ $result['tag'] = CRM_Core_BAO_EntityTag::getTag($dao->cfID, 'civicrm_file');
+ if ($addDeleteArgs) {
+ $result['deleteURLArgs'] = self::deleteURLArgs($dao->entity_table, $dao->entity_id, $dao->cfID);
+ }
+ $results[$dao->cfID] = $result;
+ }
+
+ //fix tag names
+ $tags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
+
+ foreach ($results as &$values) {
+ if (!empty($values['tag'])) {
+ $tagNames = array();
+ foreach ($values['tag'] as $tid) {
+ $tagNames[] = $tags[$tid];
+ }
+ $values['tag'] = implode(', ', $tagNames);
+ }
+ else {
+ $values['tag'] = '';
+ }
+ }
+
+ $dao->free();
+ return $results;
+ }
+
+ /**
+ * @param string $entityTable table-name or "*" (to reference files directly by file-id)
+ * @param int $entityID
+ * @param null $fileTypeID
+ * @param null $fileID
+ *
+ * @return array
+ */
+ static function sql($entityTable, $entityID, $fileTypeID = NULL, $fileID = NULL) {
+ if ($entityTable == '*') {
+ // $entityID is the ID of a specific file
+ $sql = "
+SELECT CF.id as cfID,
+ CF.uri as uri,
+ CF.mime_type as mime_type,
+ CF.description as description,
+ CEF.id as cefID,
+ CEF.entity_table as entity_table,
+ CEF.entity_id as entity_id
+FROM civicrm_file AS CF
+LEFT JOIN civicrm_entity_file AS CEF ON ( CEF.file_id = CF.id )
+WHERE CF.id = %2";
+
+ } else {
+ $sql = "
+SELECT CF.id as cfID,