Exemplo n.º 1
0
 function run()
 {
     require_once 'CRM/Utils/Request.php';
     require_once 'CRM/Core/DAO.php';
     $eid = CRM_Utils_Request::retrieve('eid', 'Positive', $this, true);
     $fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, false);
     $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, true);
     $quest = CRM_Utils_Request::retrieve('quest', 'String', $this);
     $action = CRM_Utils_Request::retrieve('action', 'String', $this);
     require_once 'CRM/Core/BAO/File.php';
     list($path, $mimeType) = CRM_Core_BAO_File::path($id, $eid, null, $quest);
     if (!$path) {
         CRM_Core_Error::statusBounce('Could not retrieve the file');
     }
     $buffer = file_get_contents($path);
     if (!$buffer) {
         CRM_Core_Error::statusBounce('The file is either empty or you do not have permission to retrieve the file');
     }
     if ($action & CRM_Core_Action::DELETE) {
         if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', CRM_Core_DAO::$_nullObject)) {
             CRM_Core_BAO_File::delete($id, $eid, $fid);
             CRM_Core_Session::setStatus(ts('The attached file has been deleted.'));
             $session = CRM_Core_Session::singleton();
             $toUrl = $session->popUserContext();
             CRM_Utils_System::redirect($toUrl);
         } else {
             $wrapper = new CRM_Utils_Wrapper();
             return $wrapper->run('CRM_Custom_Form_DeleteFile', ts('Domain Information Page'), null);
         }
     } else {
         require_once 'CRM/Utils/File.php';
         CRM_Utils_System::download(CRM_Utils_File::cleanFileName(basename($path)), $mimeType, $buffer);
     }
 }
Exemplo n.º 2
0
 public function run()
 {
     $eid = CRM_Utils_Request::retrieve('eid', 'Positive', $this, TRUE);
     $fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE);
     $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
     $quest = CRM_Utils_Request::retrieve('quest', 'String', $this);
     $action = CRM_Utils_Request::retrieve('action', 'String', $this);
     list($path, $mimeType) = CRM_Core_BAO_File::path($id, $eid, NULL, $quest);
     if (!$path) {
         CRM_Core_Error::statusBounce('Could not retrieve the file');
     }
     $buffer = file_get_contents($path);
     if (!$buffer) {
         CRM_Core_Error::statusBounce('The file is either empty or you do not have permission to retrieve the file');
     }
     if ($action & CRM_Core_Action::DELETE) {
         if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', CRM_Core_DAO::$_nullObject)) {
             CRM_Core_BAO_File::deleteFileReferences($id, $eid, $fid);
             CRM_Core_Session::setStatus(ts('The attached file has been deleted.'), ts('Complete'), 'success');
             $session = CRM_Core_Session::singleton();
             $toUrl = $session->popUserContext();
             CRM_Utils_System::redirect($toUrl);
         }
     } else {
         CRM_Utils_System::download(CRM_Utils_File::cleanFileName(basename($path)), $mimeType, $buffer);
     }
 }
Exemplo n.º 3
0
 /**
  * @param $out
  *
  * @return string
  */
 function putFile($out)
 {
     $config = CRM_Core_Config::singleton();
     $fileName = $config->uploadDir . 'Financial_Transactions_' . $this->_batchIds . '_' . date('YmdHis') . '.' . $this->getFileExtension();
     $this->_downloadFile[] = $config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($fileName));
     $buffer = fopen($fileName, 'w');
     fwrite($buffer, $out);
     fclose($buffer);
     return $fileName;
 }
Exemplo n.º 4
0
 function putFile($export)
 {
     $config = CRM_Core_Config::singleton();
     $fileName = $config->uploadDir . 'Financial_Transactions_' . $this->_batchIds . '_' . date('YmdHis') . '.' . $this->getFileExtension();
     $this->_downloadFile[] = $config->customFileUploadDir . CRM_Utils_File::cleanFileName(basename($fileName));
     $out = fopen($fileName, 'w');
     fputcsv($out, $export['headers']);
     unset($export['headers']);
     if (!empty($export)) {
         foreach ($export as $fields) {
             fputcsv($out, $fields);
         }
         fclose($out);
     }
     return $fileName;
 }
Exemplo n.º 5
0
 /**
  * @param array $files
  * @param null $destination
  * @param bool $overwrite
  *
  * @return bool
  */
 public function createZip($files = array(), $destination = NULL, $overwrite = FALSE)
 {
     //if the zip file already exists and overwrite is false, return false
     if (file_exists($destination) && !$overwrite) {
         return FALSE;
     }
     $valid_files = array();
     if (is_array($files)) {
         foreach ($files as $file) {
             //make sure the file exists
             if (file_exists($file)) {
                 $validFiles[] = $file;
             }
         }
     }
     if (count($validFiles)) {
         $zip = new ZipArchive();
         if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== TRUE) {
             return FALSE;
         }
         foreach ($validFiles as $file) {
             $zip->addFile($file, CRM_Utils_File::cleanFileName(basename($file)));
         }
         $zip->close();
         return file_exists($destination);
     } else {
         return FALSE;
     }
 }
Exemplo n.º 6
0
 /**
  * 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;
 }
Exemplo n.º 7
0
 /**
  * get all the files and associated object associated with this
  * combination
  */
 public function &getEntityFile($entityTable, $entityID)
 {
     $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['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>";
         $results[$dao->cfID] = $result;
     }
     $dao->free();
     return $results;
 }
Exemplo n.º 8
0
 /**
  * Build the entity-specific custom data into the group tree on a per-field basis
  *
  * @param object $dao
  *   Object representing the custom field to be populated into the groupTree.
  * @param array $groupTree
  *   (reference) the group tree being build.
  * @param string $table
  *   Table name.
  * @param int $groupID
  *   Custom group ID.
  * @param int $fieldID
  *   Custom field ID.
  */
 public static function buildCustomFieldData($dao, &$groupTree, $table, $groupID, $fieldID)
 {
     $column = $groupTree[$groupID]['fields'][$fieldID]['column_name'];
     $idName = "{$table}_id";
     $fieldName = "{$table}_{$column}";
     $dataType = $groupTree[$groupID]['fields'][$fieldID]['data_type'];
     if ($dataType == 'File') {
         if (isset($dao->{$fieldName})) {
             $config = CRM_Core_Config::singleton();
             $fileDAO = new CRM_Core_DAO_File();
             $fileDAO->id = $dao->{$fieldName};
             if ($fileDAO->find(TRUE)) {
                 $entityIDName = "{$table}_entity_id";
                 $customValue['id'] = $dao->{$idName};
                 $customValue['data'] = $fileDAO->uri;
                 $customValue['fid'] = $fileDAO->id;
                 $customValue['fileURL'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$fileDAO->id}&eid={$dao->{$entityIDName}}");
                 $customValue['displayURL'] = NULL;
                 $deleteExtra = ts('Are you sure you want to delete attached file.');
                 $deleteURL = array(CRM_Core_Action::DELETE => array('name' => ts('Delete Attached File'), 'url' => 'civicrm/file', 'qs' => 'reset=1&id=%%id%%&eid=%%eid%%&fid=%%fid%%&action=delete', 'extra' => 'onclick = "if (confirm( \'' . $deleteExtra . '\' ) ) this.href+=\'&amp;confirmed=1\'; else return false;"'));
                 $customValue['deleteURL'] = CRM_Core_Action::formLink($deleteURL, CRM_Core_Action::DELETE, array('id' => $fileDAO->id, 'eid' => $dao->{$entityIDName}, 'fid' => $fieldID), ts('more'), FALSE, 'file.manage.delete', 'File', $fileDAO->id);
                 $customValue['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs($table, $dao->{$entityIDName}, $fileDAO->id);
                 $customValue['fileName'] = CRM_Utils_File::cleanFileName(basename($fileDAO->uri));
                 if ($fileDAO->mime_type == "image/jpeg" || $fileDAO->mime_type == "image/pjpeg" || $fileDAO->mime_type == "image/gif" || $fileDAO->mime_type == "image/x-png" || $fileDAO->mime_type == "image/png") {
                     $customValue['displayURL'] = $customValue['fileURL'];
                     $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fileDAO->id, 'entity_id', 'file_id');
                     $customValue['imageURL'] = str_replace('persist/contribute', 'custom', $config->imageUploadURL) . $fileDAO->uri;
                     list($path) = CRM_Core_BAO_File::path($fileDAO->id, $entityId, NULL, NULL);
                     if ($path && file_exists($path)) {
                         list($imageWidth, $imageHeight) = getimagesize($path);
                         list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
                         $customValue['imageThumbWidth'] = $imageThumbWidth;
                         $customValue['imageThumbHeight'] = $imageThumbHeight;
                     }
                 }
             }
         } else {
             $customValue = array('id' => $dao->{$idName}, 'data' => '');
         }
     } else {
         $customValue = array('id' => $dao->{$idName}, 'data' => $dao->{$fieldName});
     }
     if (!array_key_exists('customValue', $groupTree[$groupID]['fields'][$fieldID])) {
         $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array();
     }
     if (empty($groupTree[$groupID]['fields'][$fieldID]['customValue'])) {
         $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array(1 => $customValue);
     } else {
         $groupTree[$groupID]['fields'][$fieldID]['customValue'][] = $customValue;
     }
 }
Exemplo n.º 9
0
 /**
  * Get custom groups/fields for type of entity.
  *
  * An array containing all custom groups and their custom fields is returned.
  *
  * @param string $entityType - of the contact whose contact type is needed
  * @param int    $entityId   - optional - id of entity if we need to populate the tree with custom values. 
  * @param int    $groupId    - optional group id (if we need it for a single group only)
  *                           - if groupId is 0 it gets for inline groups only
  *                           - if groupId is -1 we get for all groups
  *
  * @return array $groupTree  - array consisting of all groups and fields and optionally populated with custom data values.
  *
  * @access public
  *
  * @static
  *
  */
 public static function &getTree($entityType, &$form, $entityID = null, $groupID = null, $subType = null, $subName = null)
 {
     if ($entityID) {
         $entityID = CRM_Utils_Type::escape($entityID, 'Integer');
     }
     require_once 'CRM/Core/Action.php';
     // create a new tree
     $groupTree = array();
     $strSelect = $strFrom = $strWhere = $orderBy = '';
     $tableData = array();
     // using tableData to build the queryString
     $tableData = array('civicrm_custom_field' => array('id', 'label', 'column_name', 'data_type', 'html_type', 'default_value', 'attributes', 'is_required', 'is_view', 'help_pre', 'help_post', 'options_per_line', 'start_date_years', 'end_date_years', 'date_format', 'time_format', 'option_group_id'), 'civicrm_custom_group' => array('id', 'name', 'table_name', 'title', 'help_pre', 'help_post', 'collapse_display', 'is_multiple', 'extends', 'extends_entity_column_id', 'extends_entity_column_value', 'max_multiple'));
     // create select
     $select = array();
     foreach ($tableData as $tableName => $tableColumn) {
         foreach ($tableColumn as $columnName) {
             $alias = $tableName . "_" . $columnName;
             $select[] = "{$tableName}.{$columnName} as {$tableName}_{$columnName}";
         }
     }
     $strSelect = "SELECT " . implode(', ', $select);
     // from, where, order by
     $strFrom = "\nFROM     civicrm_custom_group\nLEFT JOIN civicrm_custom_field ON (civicrm_custom_field.custom_group_id = civicrm_custom_group.id)\n";
     // if entity is either individual, organization or household pls get custom groups for 'contact' too.
     if ($entityType == "Individual" || $entityType == 'Organization' || $entityType == 'Household') {
         $in = "'{$entityType}', 'Contact'";
     } else {
         if (strpos($entityType, "'") !== false) {
             // this allows the calling function to send in multiple entity types
             $in = $entityType;
         } else {
             // quote it
             $in = "'{$entityType}'";
         }
     }
     if ($subType) {
         $subType = CRM_Core_DAO::VALUE_SEPARATOR . trim($subType, CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR;
         $strWhere = "\nWHERE civicrm_custom_group.is_active = 1 \n  AND civicrm_custom_field.is_active = 1 \n  AND civicrm_custom_group.extends IN ({$in})\n  AND ( civicrm_custom_group.extends_entity_column_value LIKE '%{$subType}%'\n   OR   civicrm_custom_group.extends_entity_column_value IS NULL )\n";
         if ($subName) {
             $strWhere .= " AND civicrm_custom_group.extends_entity_column_id = {$subName} ";
         }
     } else {
         $strWhere = "\nWHERE civicrm_custom_group.is_active = 1 \n  AND civicrm_custom_field.is_active = 1 \n  AND civicrm_custom_group.extends IN ({$in})\n  AND civicrm_custom_group.extends_entity_column_value IS NULL\n";
     }
     $params = array();
     if ($groupID > 0) {
         // since we want a specific group id we add it to the where clause
         $strWhere .= " AND civicrm_custom_group.id = %1";
         $params[1] = array($groupID, 'Integer');
     } else {
         if (!$groupID) {
             // since groupID is false we need to show all Inline groups
             $strWhere .= " AND civicrm_custom_group.style = 'Inline'";
         }
     }
     require_once 'CRM/Core/Permission.php';
     // ensure that the user has access to these custom groups
     $strWhere .= " AND " . CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, 'civicrm_custom_group.');
     $orderBy = "\nORDER BY civicrm_custom_group.weight,\n         civicrm_custom_group.title,\n         civicrm_custom_field.weight,\n         civicrm_custom_field.label\n";
     // final query string
     $queryString = "{$strSelect} {$strFrom} {$strWhere} {$orderBy}";
     //crm_core_error::debug('$queryString', $queryString );
     // dummy dao needed
     $crmDAO =& CRM_Core_DAO::executeQuery($queryString, $params);
     $customValueTables = array();
     // process records
     while ($crmDAO->fetch()) {
         // get the id's
         $groupID = $crmDAO->civicrm_custom_group_id;
         $fieldId = $crmDAO->civicrm_custom_field_id;
         // create an array for groups if it does not exist
         if (!array_key_exists($groupID, $groupTree)) {
             $groupTree[$groupID] = array();
             $groupTree[$groupID]['id'] = $groupID;
             // populate the group information
             foreach ($tableData['civicrm_custom_group'] as $fieldName) {
                 $fullFieldName = "civicrm_custom_group_{$fieldName}";
                 if ($fieldName == 'id' || is_null($crmDAO->{$fullFieldName})) {
                     continue;
                 }
                 if ($fieldName == 'extends_entity_column_value' && $subType) {
                     // CRM-5507
                     $groupTree[$groupID]['subtype'] = trim($subType, CRM_Core_DAO::VALUE_SEPARATOR);
                 }
                 $groupTree[$groupID][$fieldName] = $crmDAO->{$fullFieldName};
             }
             $groupTree[$groupID]['fields'] = array();
             $customValueTables[$crmDAO->civicrm_custom_group_table_name] = array();
         }
         // add the fields now (note - the query row will always contain a field)
         // we only reset this once, since multiple values come is as multiple rows
         if (!array_key_exists($fieldId, $groupTree[$groupID]['fields'])) {
             $groupTree[$groupID]['fields'][$fieldId] = array();
         }
         $customValueTables[$crmDAO->civicrm_custom_group_table_name][$crmDAO->civicrm_custom_field_column_name] = 1;
         $groupTree[$groupID]['fields'][$fieldId]['id'] = $fieldId;
         // populate information for a custom field
         foreach ($tableData['civicrm_custom_field'] as $fieldName) {
             $fullFieldName = "civicrm_custom_field_{$fieldName}";
             if ($fieldName == 'id' || is_null($crmDAO->{$fullFieldName})) {
                 continue;
             }
             $groupTree[$groupID]['fields'][$fieldId][$fieldName] = $crmDAO->{$fullFieldName};
         }
     }
     require_once 'CRM/Core/Action.php';
     // now that we have all the groups and fields, lets get the values
     // since we need to know the table and field names
     // add info to groupTree
     if (!empty($customValueTables)) {
         $groupTree['info'] = array('tables' => $customValueTables);
         $select = $from = $where = array();
         foreach ($groupTree['info']['tables'] as $table => $fields) {
             $from[] = $table;
             $select[] = "{$table}.id as {$table}_id";
             $select[] = "{$table}.entity_id as {$table}_entity_id";
             foreach ($fields as $column => $dontCare) {
                 $select[] = "{$table}.{$column} as {$table}_{$column}";
             }
             if ($entityID) {
                 $where[] = "{$table}.entity_id = {$entityID}";
             }
         }
         $groupTree['info']['select'] = $select;
         $groupTree['info']['from'] = $from;
         $groupTree['info']['where'] = null;
         if ($entityID) {
             $groupTree['info']['where'] = $where;
             $select = implode(', ', $select);
             // this is a hack to find a table that has some values for this
             // entityID to make the below LEFT JOIN work (CRM-2518)
             $firstTable = null;
             foreach ($from as $table) {
                 $query = "\nSELECT id\nFROM   {$table}\nWHERE  entity_id = {$entityID}\n";
                 $recordExists = CRM_Core_DAO::singleValueQuery($query);
                 if ($recordExists) {
                     $firstTable = $table;
                     break;
                 }
             }
             if ($firstTable) {
                 $fromSQL = $firstTable;
                 foreach ($from as $table) {
                     if ($table != $firstTable) {
                         $fromSQL .= "\nLEFT JOIN {$table} USING (entity_id)";
                     }
                 }
                 $query = "\nSELECT {$select}\n  FROM {$fromSQL}\n WHERE {$firstTable}.entity_id = {$entityID}\n";
                 $dao = CRM_Core_DAO::executeQuery($query);
                 while ($dao->fetch()) {
                     foreach ($groupTree as $groupID => $group) {
                         if ($groupID === 'info') {
                             continue;
                         }
                         $table = $groupTree[$groupID]['table_name'];
                         foreach ($group['fields'] as $fieldID => $dontCare) {
                             $column = $groupTree[$groupID]['fields'][$fieldID]['column_name'];
                             $idName = "{$table}_id";
                             $fieldName = "{$table}_{$column}";
                             $dataType = $groupTree[$groupID]['fields'][$fieldID]['data_type'];
                             if ($dataType == 'File') {
                                 if (isset($dao->{$fieldName})) {
                                     require_once 'CRM/Core/DAO/File.php';
                                     $config = CRM_Core_Config::singleton();
                                     $fileDAO = new CRM_Core_DAO_File();
                                     $fileDAO->id = $dao->{$fieldName};
                                     if ($fileDAO->find(true)) {
                                         $entityIDName = "{$table}_entity_id";
                                         $customValue['id'] = $dao->{$idName};
                                         $customValue['data'] = $fileDAO->uri;
                                         $customValue['fid'] = $fileDAO->id;
                                         $customValue['fileURL'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$fileDAO->id}&eid={$dao->{$entityIDName}}");
                                         $customValue['displayURL'] = null;
                                         $deleteExtra = ts('Are you sure you want to delete attached file.');
                                         $deleteURL = array(CRM_Core_Action::DELETE => array('name' => ts('Delete Attached File'), 'url' => 'civicrm/file', 'qs' => 'reset=1&id=%%id%%&eid=%%eid%%&fid=%%fid%%&action=delete', 'extra' => 'onclick = "if (confirm( \'' . $deleteExtra . '\' ) ) this.href+=\'&amp;confirmed=1\'; else return false;"'));
                                         $customValue['deleteURL'] = CRM_Core_Action::formLink($deleteURL, CRM_Core_Action::DELETE, array('id' => $fileDAO->id, 'eid' => $dao->{$entityIDName}, 'fid' => $fieldID));
                                         $customValue['fileName'] = CRM_Utils_File::cleanFileName(basename($fileDAO->uri));
                                         if ($fileDAO->mime_type == "image/jpeg" || $fileDAO->mime_type == "image/pjpeg" || $fileDAO->mime_type == "image/gif" || $fileDAO->mime_type == "image/x-png" || $fileDAO->mime_type == "image/png") {
                                             $customValue['displayURL'] = $customValue['fileURL'];
                                             $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fileDAO->id, 'entity_id', 'id');
                                             require_once 'CRM/Core/BAO/File.php';
                                             list($path) = CRM_Core_BAO_File::path($fileDAO->id, $entityId, null, null);
                                             list($imageWidth, $imageHeight) = getimagesize($path);
                                             require_once 'CRM/Contact/BAO/Contact.php';
                                             list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
                                             $customValue['imageThumbWidth'] = $imageThumbWidth;
                                             $customValue['imageThumbHeight'] = $imageThumbHeight;
                                         }
                                     }
                                 } else {
                                     $customValue = array('id' => $dao->{$idName}, 'data' => '');
                                 }
                             } else {
                                 $customValue = array('id' => $dao->{$idName}, 'data' => $dao->{$fieldName});
                             }
                             if (!array_key_exists('customValue', $groupTree[$groupID]['fields'][$fieldID])) {
                                 $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array();
                             }
                             if (empty($groupTree[$groupID]['fields'][$fieldID]['customValue'])) {
                                 $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array(1 => $customValue);
                             } else {
                                 $groupTree[$groupID]['fields'][$fieldID]['customValue'][] = $customValue;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $groupTree;
 }
Exemplo n.º 10
0
/**
 * Attachment result formatting helper.
 *
 * @param CRM_Core_DAO_File $fileDao
 *   Maybe "File" or "File JOIN EntityFile".
 * @param CRM_Core_DAO_EntityFile $entityFileDao
 *   Maybe "EntityFile" or "File JOIN EntityFile".
 * @param bool $returnContent
 *   Whether to return the full content of the file.
 * @param bool $isTrusted
 *   Whether the current request is trusted to perform file-specific operations.
 *
 * @return array
 */
function _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $returnContent, $isTrusted)
{
    $config = CRM_Core_Config::singleton();
    $path = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $fileDao->uri;
    $result = array('id' => $fileDao->id, 'name' => CRM_Utils_File::cleanFileName($fileDao->uri), 'mime_type' => $fileDao->mime_type, 'description' => $fileDao->description, 'upload_date' => is_numeric($fileDao->upload_date) ? CRM_Utils_Date::mysqlToIso($fileDao->upload_date) : $fileDao->upload_date, 'entity_table' => $entityFileDao->entity_table, 'entity_id' => $entityFileDao->entity_id);
    $result['url'] = CRM_Utils_System::url('civicrm/file', 'reset=1&id=' . $result['id'] . '&eid=' . $result['entity_id'], TRUE, NULL, FALSE, TRUE);
    if ($isTrusted) {
        $result['path'] = $path;
    }
    if ($returnContent) {
        $result['content'] = file_get_contents($path);
    }
    return $result;
}
function wordmailmerge_civicrm_buildForm($formName, &$form)
{
    require_once 'CRM/Core/DAO/MessageTemplate.php';
    require_once 'CRM/Core/BAO/File.php';
    require_once 'CRM/Core/DAO.php';
    if ($formName == 'CRM_Admin_Form_MessageTemplates') {
        $action = $form->getVar('_action');
        $template = CRM_Core_Smarty::singleton();
        $form->assign('action', $action);
        $templatePath = realpath(dirname(__FILE__) . "/templates");
        $config = CRM_Core_Config::singleton();
        if ($action == CRM_Core_Action::UPDATE) {
            $msgTemplateId = $form->getVar('_defaultValues')['id'];
            $sql = "SELECT * FROM veda_civicrm_wordmailmerge WHERE msg_template_id = %1";
            $params = array(1 => array($msgTemplateId, 'Integer'));
            $dao = CRM_Core_DAO::executeQuery($sql, $params);
            while ($dao->fetch()) {
                $fileId = $dao->file_id;
            }
            if (!empty($fileId)) {
                $mysql = "SELECT * FROM civicrm_file WHERE id = %1";
                $params = array(1 => array($fileId, 'Integer'));
                $dao = CRM_Core_DAO::executeQuery($mysql, $params);
                while ($dao->fetch()) {
                    $default['fileID'] = $dao->id;
                    $default['mime_type'] = $dao->mime_type;
                    $default['fileName'] = $dao->uri;
                    $default['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri);
                    $default['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri;
                    $default['url'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$dao->id}&eid={$msgTemplateId}");
                    $default['href'] = "<a href=\"{$default['url']}\">{$default['cleanName']}</a>";
                    $default['tag'] = CRM_Core_BAO_EntityTag::getTag($dao->id, 'civicrm_file');
                    $default['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs('civicrm_msg_template', $msgTemplateId, $dao->id);
                }
                $defaults[$dao->id] = $default;
                $form->assign('defaults', $defaults);
            }
        }
        CRM_Core_BAO_File::buildAttachment($form, 'civicrm_msg_template', '', 1);
        $session = CRM_Core_Session::singleton();
        $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'reset=1'));
        CRM_Core_Region::instance('page-body')->add(array('template' => "{$templatePath}/CRM/Wordmailmerge/testfield.tpl"));
    }
}
 function postProcess()
 {
     $values = $this->_contactIds;
     $config = CRM_Core_Config::singleton();
     $msg_id = $this->_submitValues['message_template'];
     if (!empty($msg_id)) {
         $mysql = " SELECT * FROM veda_civicrm_wordmailmerge WHERE msg_template_id = %1";
         $params = array(1 => array($msg_id, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($mysql, $params);
         //$dao = CRM_Core_DAO::executeQuery($mysql);
         while ($dao->fetch()) {
             $fileId = $dao->file_id;
         }
         $sql = "SELECT * FROM civicrm_file WHERE id = %1";
         $params = array(1 => array($fileId, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($sql, $params);
         //$dao = CRM_Core_DAO::executeQuery($sql);
         while ($dao->fetch()) {
             $default['fileID'] = $dao->id;
             $default['mime_type'] = $dao->mime_type;
             $default['fileName'] = $dao->uri;
             $default['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri);
             $default['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri;
             $default['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs('civicrm_file', $msg_id, $dao->id);
         }
         $defaults[$dao->id] = $default;
         $this->assign('defaults', $defaults);
         $noofContact = count($this->_contactIds);
         require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong/tbs_class.php';
         require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong-opentbs/tbs_plugin_opentbs.php';
         $TBS = new clsTinyButStrong();
         // new instance of TBS
         $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
         // load the OpenTBS plugin
         $template = $default['fullPath'];
         // contactrows to check for duplicate address
         $contactrows = array();
         foreach ($values as $key => $value) {
             $SelectedcontactID = $values[$key];
             // get the details for all selected contacts
             list($contactDetails) = CRM_Utils_Token::getTokenDetails(array($SelectedcontactID), $this->_returnProperties, NULL, NULL, FALSE, $this->_allTokens);
             // populate contactrows array to check dupliacte address
             $contactrows[$SelectedcontactID] = $contactDetails[$SelectedcontactID];
         }
         // if merge_letter_for_same_address selected check for duplicate address
         if (isset($this->_submitValues['merge_letter_for_same_address']) && $this->_submitValues['merge_letter_for_same_address']) {
             CRM_Core_BAO_Address::mergeSameAddress($contactrows);
         }
         foreach ($values as $key => $value) {
             if ($key < $noofContact) {
                 $selectedCID = $values[$key];
                 $contactFormatted = array();
                 // if contact_id found in filtered contactrows array get contact details from contactrows
                 if (array_key_exists($selectedCID, $contactrows)) {
                     $contactFormatted[$selectedCID] = $contactrows[$selectedCID];
                     $membershipFormatted = array();
                     if ($this->_searchFrom == 'member' && isset($contactFormatted[$selectedCID]['membership_id'])) {
                         $membershipFormatted = CRM_Utils_Token::getMembershipTokenDetails($contactFormatted[$selectedCID]['membership_id']);
                     }
                     foreach ($this->_tokenMerge as $atKey => $atValue) {
                         // Replace hook tokens
                         $explodedTokenName = explode('.', $atValue['token_name']);
                         // this is fixed by assigning 'address_block' token into 'contact' token array // gopi@vedaconsulting.co.uk
                         //need to do proper fix seems token named as contact.address_block
                         // $atValue['token_name'] = ($atValue['token_name'] == 'address_block') ? 'contact.'.$atValue['token_name'] : $atValue['token_name'];
                         if (array_key_exists($atValue['token_name'], $contactFormatted[$selectedCID])) {
                             if (!empty($explodedTokenName[1]) && $explodedTokenName[0] != 'contact') {
                                 $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = $contactFormatted[$selectedCID][$atValue['token_name']];
                             } else {
                                 $vars[$key][$atValue['token_name']] = $contactFormatted[$selectedCID][$atValue['token_name']];
                             }
                         } else {
                             if ($explodedTokenName[0] == 'membership') {
                                 $explodedTokenName[1] = $explodedTokenName[1] == 'membership_id' ? 'id' : $explodedTokenName[1];
                                 $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = CRM_Utils_Token::getMembershipTokenReplacement($explodedTokenName[1], $membershipFormatted[$contactFormatted[$selectedCID]['membership_id']]);
                             } else {
                                 $vars[$key][$atValue['token_name']] = CRM_Utils_Token::getContactTokenReplacement($atValue['token_name'], $contactFormatted[$selectedCID], FALSE, FALSE);
                             }
                         }
                         //need to do proper fix, token_name.date seems not returning null value if not found
                         if ($explodedTokenName[0] == 'token_name' && !is_array($vars[$key]['token_name'])) {
                             $vars[$key][$atValue['token_name']] = '';
                         }
                     }
                     //to skip error, if by chance using membership token in 'find contact' search
                     if ($this->_searchFrom != 'member') {
                         foreach (CRM_Core_SelectValues::membershipTokens() as $token => $label) {
                             $token = str_replace(array('{', '}'), "", $token);
                             $tokenNames = explode('.', $token);
                             $vars[$key]['membership'][$tokenNames[1]] = $label;
                         }
                     }
                     foreach ($vars[$key] as $varKey => $varValue) {
                         $explodeValues = explode('.', $varKey);
                         if (isset($explodeValues[1]) && !empty($explodeValues[1])) {
                             $vars[$key][$explodeValues[0]][$explodeValues[1]] = $vars[$key][$varKey];
                             unset($vars[$key][$varKey]);
                         }
                     }
                     // blank lines removed while creating the address_block - gopi@vedaconsulting.co.uk
                     /*if (!empty($vars[$key]['contact']['address_block'])) {
                         $vars[$key]['contact']['address_block'] = str_replace('<br />', "", $vars[$key]['contact']['address_block']);
                       }*/
                     $TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8);
                     $TBS->MergeBlock(self::TOKEN_VAR_NAME, $vars);
                 }
             }
         }
         $output_file_name = 'CiviCRMWordExport.docx';
         $TBS->Show(OPENTBS_DOWNLOAD, $output_file_name);
         CRM_Utils_System::civiExit();
     }
     parent::postProcess();
 }