Beispiel #1
0
 /**
  * This function will take any file and make it permanently
  * available as a CiviCRM File entity.
  *
  * @param path          where to find the file
  * @param contact_id    which contact to connect to
  *
  * @param mimetype      the document's MIME type. Autodetect if null
  *
  * @return an array containing the created file object, including a generated url
  */
 public static function createPermanentFile($path, $name = null, $contact_id, $mimetype = null, $description = '')
 {
     $config = CRM_Core_Config::singleton();
     if (!file_exists($path)) {
         return null;
     }
     // TODO: check if a file object already exists?
     // move file to a permanent folder
     $newPath = $config->customFileUploadDir . basename($path);
     copy($path, $newPath);
     // find mime type
     if (empty($mimetype)) {
         $mimetype = mime_content_type($newPath);
     }
     // create the file object
     if (empty($description)) {
         $description = $name;
     }
     $file = civicrm_api('File', 'create', array('version' => 3, 'uri' => basename($newPath), 'mime_type' => $mimetype, 'description' => $description));
     if (!empty($file['is_error'])) {
         CRM_Core_Error::debug_log_message("de.systopia.donrec: couldn't create file object - " . $file['error_message']);
         return null;
     }
     if ($contact_id) {
         // link the file to a contact  (there is no API call for this...)
         $entityFile = new CRM_Core_DAO_EntityFile();
         $entityFile->file_id = $file['id'];
         $entityFile->entity_id = $contact_id;
         $entityFile->entity_table = 'civicrm_contact';
         $entityFile->save();
     }
     // build reply
     $reply = $file['values'];
     $reply['url'] = CRM_Utils_System::url("civicrm/file", "reset=1&id=" . $file['id'] . "&eid={$contact_id}");
     $reply['path'] = $newPath;
     return $reply;
 }
Beispiel #2
0
 static function copyEntityFile($oldEntityTable, $oldEntityId, $newEntityTable, $newEntityId)
 {
     $oldEntityFile = new CRM_Core_DAO_EntityFile();
     $oldEntityFile->entity_id = $oldEntityId;
     $oldEntityFile->entity_table = $oldEntityTable;
     $oldEntityFile->find();
     while ($oldEntityFile->fetch()) {
         $newEntityFile = new CRM_Core_DAO_EntityFile();
         $newEntityFile->entity_id = $newEntityId;
         $newEntityFile->entity_table = $newEntityTable;
         $newEntityFile->file_id = $oldEntityFile->file_id;
         $newEntityFile->save();
     }
 }
 /**
  * @param array $customParams
  *
  * @throws Exception
  */
 public static function create(&$customParams)
 {
     if (empty($customParams) || !is_array($customParams)) {
         return;
     }
     foreach ($customParams as $tableName => $tables) {
         foreach ($tables as $index => $fields) {
             $sqlOP = NULL;
             $hookID = NULL;
             $hookOP = NULL;
             $entityID = NULL;
             $isMultiple = FALSE;
             $set = array();
             $params = array();
             $count = 1;
             foreach ($fields as $field) {
                 if (!$sqlOP) {
                     $entityID = $field['entity_id'];
                     $hookID = $field['custom_group_id'];
                     $isMultiple = $field['is_multiple'];
                     if (array_key_exists('id', $field)) {
                         $sqlOP = "UPDATE {$tableName} ";
                         $where = " WHERE  id = %{$count}";
                         $params[$count] = array($field['id'], 'Integer');
                         $count++;
                         $hookOP = 'edit';
                     } else {
                         $sqlOP = "INSERT INTO {$tableName} ";
                         $where = NULL;
                         $hookOP = 'create';
                     }
                 }
                 // fix the value before we store it
                 $value = $field['value'];
                 $type = $field['type'];
                 switch ($type) {
                     case 'StateProvince':
                         $type = 'Integer';
                         if (is_array($value)) {
                             $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR;
                             $type = 'String';
                         } elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
                             //fix for multi select state, CRM-3437
                             $mulValues = explode(',', $value);
                             $validStates = array();
                             foreach ($mulValues as $key => $stateVal) {
                                 $states = array();
                                 $states['state_province'] = trim($stateVal);
                                 CRM_Utils_Array::lookupValue($states, 'state_province', CRM_Core_PseudoConstant::stateProvince(), TRUE);
                                 if (empty($states['state_province_id'])) {
                                     CRM_Utils_Array::lookupValue($states, 'state_province', CRM_Core_PseudoConstant::stateProvinceAbbreviation(), TRUE);
                                 }
                                 $validStates[] = CRM_Utils_Array::value('state_province_id', $states);
                             }
                             $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, $validStates);
                             $type = 'String';
                         } elseif (!$value) {
                             // CRM-3415
                             // using type of timestamp allows us to sneak in a null into db
                             // gross but effective hack
                             $value = NULL;
                             $type = 'Timestamp';
                         } else {
                             $type = 'String';
                         }
                         break;
                     case 'Country':
                         $type = 'Integer';
                         $mulValues = explode(',', $value);
                         if (is_array($value)) {
                             $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR;
                             $type = 'String';
                         } elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
                             //fix for multi select country, CRM-3437
                             $mulValues = explode(',', $value);
                             $validCountries = array();
                             foreach ($mulValues as $key => $countryVal) {
                                 $countries = array();
                                 $countries['country'] = trim($countryVal);
                                 CRM_Utils_Array::lookupValue($countries, 'country', CRM_Core_PseudoConstant::country(), TRUE);
                                 if (empty($countries['country_id'])) {
                                     CRM_Utils_Array::lookupValue($countries, 'country', CRM_Core_PseudoConstant::countryIsoCode(), TRUE);
                                 }
                                 $validCountries[] = CRM_Utils_Array::value('country_id', $countries);
                             }
                             $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, $validCountries);
                             $type = 'String';
                         } elseif (!$value) {
                             // CRM-3415
                             // using type of timestamp allows us to sneak in a null into db
                             // gross but effective hack
                             $value = NULL;
                             $type = 'Timestamp';
                         } else {
                             $type = 'String';
                         }
                         break;
                     case 'File':
                         if (!$field['file_id']) {
                             CRM_Core_Error::fatal();
                         }
                         // need to add/update civicrm_entity_file
                         $entityFileDAO = new CRM_Core_DAO_EntityFile();
                         $entityFileDAO->file_id = $field['file_id'];
                         $entityFileDAO->find(TRUE);
                         $entityFileDAO->entity_table = $field['table_name'];
                         $entityFileDAO->entity_id = $field['entity_id'];
                         $entityFileDAO->file_id = $field['file_id'];
                         $entityFileDAO->save();
                         $entityFileDAO->free();
                         $value = $field['file_id'];
                         $type = 'String';
                         break;
                     case 'Date':
                         $value = CRM_Utils_Date::isoToMysql($value);
                         break;
                     case 'Int':
                         if (is_numeric($value)) {
                             $type = 'Integer';
                         } else {
                             $type = 'Timestamp';
                         }
                         break;
                     case 'ContactReference':
                         if ($value == NULL) {
                             $type = 'Timestamp';
                         } else {
                             $type = 'Integer';
                         }
                         break;
                     case 'RichTextEditor':
                         $type = 'String';
                         break;
                     case 'Boolean':
                         //fix for CRM-3290
                         $value = CRM_Utils_String::strtoboolstr($value);
                         if ($value === FALSE) {
                             $type = 'Timestamp';
                         }
                         break;
                     default:
                         break;
                 }
                 if (strtolower($value) === "null") {
                     // when unsetting a value to null, we don't need to validate the type
                     // https://projectllr.atlassian.net/browse/VGQBMP-20
                     $set[$field['column_name']] = $value;
                 } else {
                     $set[$field['column_name']] = "%{$count}";
                     $params[$count] = array($value, $type);
                     $count++;
                 }
             }
             if (!empty($set)) {
                 $setClause = array();
                 foreach ($set as $n => $v) {
                     $setClause[] = "{$n} = {$v}";
                 }
                 $setClause = implode(',', $setClause);
                 if (!$where) {
                     // do this only for insert
                     $set['entity_id'] = "%{$count}";
                     $params[$count] = array($entityID, 'Integer');
                     $count++;
                     $fieldNames = implode(',', array_keys($set));
                     $fieldValues = implode(',', array_values($set));
                     $query = "{$sqlOP} ( {$fieldNames} ) VALUES ( {$fieldValues} )";
                     // for multiple values we dont do on duplicate key update
                     if (!$isMultiple) {
                         $query .= " ON DUPLICATE KEY UPDATE {$setClause}";
                     }
                 } else {
                     $query = "{$sqlOP} SET {$setClause} {$where}";
                 }
                 $dao = CRM_Core_DAO::executeQuery($query, $params);
                 CRM_Utils_Hook::custom($hookOP, $hookID, $entityID, $fields);
             }
         }
     }
 }
Beispiel #4
0
 /**
  * returns the list of fields that can be exported
  *
  * @access public
  * return array
  * @static
  */
 static function &export($prefix = false)
 {
     if (!self::$_export) {
         self::$_export = array();
         $fields = self::fields();
         foreach ($fields as $name => $field) {
             if (CRM_Utils_Array::value('export', $field)) {
                 if ($prefix) {
                     self::$_export['entity_file'] =& $fields[$name];
                 } else {
                     self::$_export[$name] =& $fields[$name];
                 }
             }
         }
     }
     return self::$_export;
 }
Beispiel #5
0
/**
 * Delete an existing File.
 *
 * @param array $params
 *   Array per getfields metadata.
 *
 * @return array
 *   API result array
 */
function civicrm_api3_file_delete($params)
{
    civicrm_api3_verify_mandatory($params, NULL, array('id'));
    $check = FALSE;
    $entityFileDAO = new CRM_Core_DAO_EntityFile();
    $entityFileDAO->file_id = $params['id'];
    if ($entityFileDAO->find()) {
        $check = $entityFileDAO->delete();
    }
    $fileDAO = new CRM_Core_DAO_File();
    $fileDAO->id = $params['id'];
    if ($fileDAO->find(TRUE)) {
        $check = $fileDAO->delete();
    }
    return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.');
}
/**
 * Attachment getfields helper.
 *
 * @return array
 *   list of fields (indexed by name)
 */
function _civicrm_api3_attachment_getfields()
{
    $fileFields = CRM_Core_DAO_File::fields();
    $entityFileFields = CRM_Core_DAO_EntityFile::fields();
    $spec = array();
    $spec['id'] = $fileFields['id'];
    $spec['name'] = array('title' => 'Name (write-once)', 'description' => 'The logical file name (not searchable)', 'type' => CRM_Utils_Type::T_STRING);
    $spec['field_name'] = array('title' => 'Field Name (write-once)', 'description' => 'Alternative to "entity_table" param - sets custom field value.', 'type' => CRM_Utils_Type::T_STRING);
    $spec['mime_type'] = $fileFields['mime_type'];
    $spec['description'] = $fileFields['description'];
    $spec['upload_date'] = $fileFields['upload_date'];
    $spec['entity_table'] = $entityFileFields['entity_table'];
    // Would be hard to securely handle changes.
    $spec['entity_table']['title'] = CRM_Utils_Array::value('title', $spec['entity_table'], 'Entity Table') . ' (write-once)';
    $spec['entity_id'] = $entityFileFields['entity_id'];
    $spec['entity_id']['title'] = CRM_Utils_Array::value('title', $spec['entity_id'], 'Entity ID') . ' (write-once)';
    // would be hard to securely handle changes
    $spec['url'] = array('title' => 'URL (read-only)', 'description' => 'URL for downloading the file (not searchable, expire-able)', 'type' => CRM_Utils_Type::T_STRING);
    $spec['path'] = array('title' => 'Path (read-only)', 'description' => 'Local file path (not searchable, local-only)', 'type' => CRM_Utils_Type::T_STRING);
    $spec['content'] = array('title' => 'Content', 'description' => 'File content (not searchable, not returned by default)', 'type' => CRM_Utils_Type::T_STRING);
    return $spec;
}
function saveDocument($contact_id, $filename, $mimetype, $filetype, $date, $date_from, $date_to, $comment)
{
    $docs = get_docs_table();
    $file = new CRM_Core_DAO_File();
    $file->mime_type = $mimetype;
    $file->uri = $filename;
    $file->upload_date = date('Ymd');
    $file->save();
    $entityFile = new CRM_Core_DAO_EntityFile();
    $entityFile->file_id = $file->id;
    $entityFile->entity_id = $contact_id;
    $entityFile->entity_table = $docs['table'];
    $entityFile->save();
    $stmt = "INSERT INTO {$docs['table']}\n                   SET {$docs['field_filetype']} = '{$filetype}'\n                     , {$docs['field_date']}     = '{$date}'\n                     , {$docs['field_from']}     = '{$date_from}'\n                     , {$docs['field_to']}       = '{$date_to}'\n                     , {$docs['field_comment']}  = '{$comment}'\n                     , {$docs['field_file']}     =  {$file->id}\n                     , entity_id = {$contact_id}\n          ";
    $res =& CRM_Core_DAO::executeQuery($stmt);
    return $file->id;
}
Beispiel #8
0
 function fileUpload()
 {
     $config = CRM_Core_Config::singleton();
     $postParams = $_POST;
     if ($postParams['entityID'] == 'undefined') {
         $result = civicrm_api3('HRJob', 'get', array('options' => array('sort' => "id DESC")));
         foreach ($result['values'] as $key => $val) {
             $id = $val['id'];
             break;
         }
         $postParams['entityID'] = ++$id;
     }
     $files = $_FILES;
     if (is_array($files) && !empty($files)) {
         foreach ($files as $k => $v) {
             $fileName = CRM_Utils_File::makeFileName(basename($v['name']));
             $maxSize = @filesize($v['tmp_name']);
             $dest = $config->customFileUploadDir;
             if ($dest != '' && substr($dest, -1) != '/') {
                 $dest .= '/';
             }
             $fileName = $fileName != '' ? $fileName : basename($v['name']);
             if (is_uploaded_file($v['tmp_name'])) {
                 if (move_uploaded_file($v['tmp_name'], $dest . $fileName)) {
                     $fileparam['name'] = $fileName;
                     $fileparam['uri'] = $fileName;
                     $file_params = array('version' => 3, 'file_type_id' => null, 'mime_type' => $v['type'], 'name' => $fileName, 'uri' => $fileName, 'upload_date' => date('Y-m-d h:i:s', time()));
                     $file_save = civicrm_api('File', 'Create', $file_params);
                     $newEntityFile = new CRM_Core_DAO_EntityFile();
                     $newEntityFile->entity_id = $postParams['entityID'];
                     $newEntityFile->entity_table = $postParams['entityTable'];
                     $newEntityFile->file_id = $file_save['id'];
                     $newEntityFile->save();
                     $fileName = basename($v['name']);
                 }
             }
         }
     }
     CRM_Utils_System::civiExit();
 }
 /**
  * Process the form submission.
  */
 public function postProcess()
 {
     if ($this->_action & CRM_Core_Action::DELETE) {
         CRM_Core_BAO_MessageTemplate::del($this->_id);
     } elseif ($this->_action & CRM_Core_Action::VIEW) {
         // currently, the above action is used solely for previewing default workflow templates
         CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
     } else {
         $params = array();
         // store the submitted values in an array
         $params = $this->controller->exportValues($this->_name);
         if ($this->_action & CRM_Core_Action::UPDATE) {
             $params['id'] = $this->_id;
         }
         if (!empty($params['file_type'])) {
             unset($params['msg_html']);
             unset($params['msg_text']);
             CRM_Utils_File::formatFile($params, 'file_id');
         } elseif (!empty($this->_id)) {
             $entityFileDAO = new CRM_Core_DAO_EntityFile();
             $entityFileDAO->entity_id = $this->_id;
             $entityFileDAO->entity_table = 'civicrm_msg_template';
             if ($entityFileDAO->find(TRUE)) {
                 $fileDAO = new CRM_Core_DAO_File();
                 $fileDAO->id = $entityFileDAO->file_id;
                 $fileDAO->find(TRUE);
                 $entityFileDAO->delete();
                 $fileDAO->delete();
             }
         }
         if ($this->_workflow_id) {
             $params['workflow_id'] = $this->_workflow_id;
             $params['is_active'] = TRUE;
         }
         $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params);
         CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', array(1 => $messageTemplate->msg_title)), ts('Saved'), 'success');
         if ($this->_workflow_id) {
             CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'));
         } else {
             CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1'));
         }
     }
 }
Beispiel #10
0
 public static function fileUpload()
 {
     $config = CRM_Core_Config::singleton();
     $postParams = $_POST;
     $result = 0;
     $dest = $config->customFileUploadDir;
     if ($dest != '' && substr($dest, -1) != '/') {
         $dest .= '/';
     }
     $files = $_FILES;
     if (is_array($files) && !empty($files)) {
         foreach ($files as $k => $v) {
             $fileName = self::makeFileName(basename($v['name']), $dest);
             $maxSize = @filesize($v['tmp_name']);
             $fileName = $fileName != '' ? $fileName : basename($v['name']);
             if (is_uploaded_file($v['tmp_name'])) {
                 if (move_uploaded_file($v['tmp_name'], $dest . $fileName)) {
                     $fileparam['name'] = $fileName;
                     $fileparam['uri'] = $fileName;
                     $file_params = array('version' => 3, 'file_type_id' => null, 'mime_type' => $v['type'], 'name' => $fileName, 'uri' => $fileName, 'upload_date' => date('Y-m-d h:i:s', time()));
                     $file_save = civicrm_api('File', 'Create', $file_params);
                     $newEntityFile = new CRM_Core_DAO_EntityFile();
                     $newEntityFile->entity_id = $postParams['entityID'];
                     $newEntityFile->entity_table = $postParams['entityTable'];
                     $newEntityFile->file_id = $file_save['id'];
                     $newEntityFile->save();
                     $fileName = basename($v['name']);
                     $result++;
                 }
             }
         }
     }
     echo html_entity_decode(stripcslashes(json_encode(array('values' => array(array('result' => $result))), true)));
     CRM_Utils_System::civiExit();
 }
function civicrm_api3_civi_outlook_processattachments($params)
{
    //Get mime type of the attachment
    $mimeType = CRM_Utils_Type::escape($_REQUEST['mimeType'], 'String');
    $params['mime_type'] = $mimeType;
    $config = CRM_Core_Config::singleton();
    $directoryName = $config->customFileUploadDir;
    CRM_Utils_File::createDir($directoryName);
    //Process the below only if there is any attachment found
    if (CRM_Utils_Array::value("name", $_FILES['file'])) {
        $tmp_name = $_FILES['file']['tmp_name'];
        $name = str_replace(' ', '_', $_FILES['file']['name']);
        //Replace any spaces in name with underscore
        $fileExtension = new SplFileInfo($name);
        if ($fileExtension->getExtension()) {
            $explodeName = explode("." . $fileExtension->getExtension(), $name);
            $name = $explodeName[0] . "_" . md5($name) . "." . $fileExtension->getExtension();
        }
        $_FILES['file']['uri'] = $name;
        move_uploaded_file($tmp_name, "{$directoryName}{$name}");
        foreach ($_FILES['file'] as $key => $value) {
            $params[$key] = $value;
        }
        $result = civicrm_api3('File', 'create', $params);
        if (CRM_Utils_Array::value('id', $result)) {
            if (CRM_Utils_Array::value('activityID', $params)) {
                $lastActivityID = $params['activityID'];
            }
            $entityFileDAO = new CRM_Core_DAO_EntityFile();
            $entityFileDAO->entity_table = 'civicrm_activity';
            $entityFileDAO->entity_id = $lastActivityID;
            $entityFileDAO->file_id = $result['id'];
            $entityFileDAO->save();
        }
    }
    return civicrm_api3_create_success($entityFileDAO, $params);
}
/**
 * Deletes an existing entity file assignment.
 * Required parameters : 1.  id of an entity-file
 *                       2.  entity_id and entity_table of an entity-file
 *
 * @param   array $params   an associative array of name/value property values of civicrm_entity_file.
 *
 * @return  null if successfull, object of CRM_Core_Error otherwise
 * @access public
 */
function civicrm_entity_file_delete(&$params)
{
    require_once 'CRM/Core/DAO/EntityFile.php';
    //if ( ! isset($params['id']) && ( !isset($params['entity_id']) || !isset($params['entity_file']) ) ) {
    if (!isset($params['id']) && (!isset($params['entity_id']) || !isset($params['entity_table']))) {
        return civicrm_create_error('Required parameters missing');
    }
    $entityFileDAO = new CRM_Core_DAO_EntityFile();
    $properties = array('id', 'entity_id', 'entity_table', 'file_id');
    foreach ($properties as $name) {
        if (array_key_exists($name, $params)) {
            $entityFileDAO->{$name} = $params[$name];
        }
    }
    return $entityFileDAO->delete() ? NULL : civicrm_create_error('Error while deleting');
}