| [ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* +*********************************************************************************** 4 * The contents of this file are subject to the vtiger CRM Public License Version 1.0 5 * ("License"); You may not use this file except in compliance with the License 6 * The Original Code is: vtiger CRM Open Source 7 * The Initial Developer of the Original Code is vtiger. 8 * Portions created by vtiger are Copyright (C) vtiger. 9 * All Rights Reserved. 10 * *********************************************************************************** */ 11 12 class Import_Utils_Helper { 13 14 static $AUTO_MERGE_NONE = 0; 15 static $AUTO_MERGE_IGNORE = 1; 16 static $AUTO_MERGE_OVERWRITE = 2; 17 static $AUTO_MERGE_MERGEFIELDS = 3; 18 19 static $supportedFileEncoding = array('UTF-8'=>'UTF-8', 'ISO-8859-1'=>'ISO-8859-1'); 20 static $supportedDelimiters = array(','=>'comma', ';'=>'semicolon'); 21 static $supportedFileExtensions = array('csv','vcf'); 22 23 public function getSupportedFileExtensions() { 24 return self::$supportedFileExtensions; 25 } 26 27 public function getSupportedFileEncoding() { 28 return self::$supportedFileEncoding; 29 } 30 31 public function getSupportedDelimiters() { 32 return self::$supportedDelimiters; 33 } 34 35 public static function getAutoMergeTypes() { 36 return array( 37 self::$AUTO_MERGE_IGNORE => 'Skip', 38 self::$AUTO_MERGE_OVERWRITE => 'Overwrite', 39 self::$AUTO_MERGE_MERGEFIELDS => 'Merge'); 40 } 41 42 public static function getMaxUploadSize() { 43 global $upload_maxsize; 44 return $upload_maxsize; 45 } 46 47 public static function getImportDirectory() { 48 global $import_dir; 49 $importDir = dirname(__FILE__). '/../../../'.$import_dir; 50 return $importDir; 51 } 52 53 public static function getImportFilePath($user) { 54 $importDirectory = self::getImportDirectory(); 55 return $importDirectory. "IMPORT_".$user->id; 56 } 57 58 59 public static function getFileReaderInfo($type) { 60 $configReader = new Import_Config_Model(); 61 $importTypeConfig = $configReader->get('importTypes'); 62 if(isset($importTypeConfig[$type])) { 63 return $importTypeConfig[$type]; 64 } 65 return null; 66 } 67 68 public static function getFileReader($request, $user) { 69 $fileReaderInfo = self::getFileReaderInfo($request->get('type')); 70 if(!empty($fileReaderInfo)) { 71 require_once $fileReaderInfo['classpath']; 72 $fileReader = new $fileReaderInfo['reader'] ($request, $user); 73 } else { 74 $fileReader = null; 75 } 76 return $fileReader; 77 } 78 79 public static function getDbTableName($user) { 80 $configReader = new Import_Config_Model(); 81 $userImportTablePrefix = $configReader->get('userImportTablePrefix'); 82 83 $tableName = $userImportTablePrefix; 84 if(method_exists($user, 'getId')){ 85 $tableName .= $user->getId(); 86 } else { 87 $tableName .= $user->id; 88 } 89 return $tableName; 90 } 91 92 public static function showErrorPage($errorMessage, $errorDetails=false, $customActions=false) { 93 $viewer = new Vtiger_Viewer(); 94 95 $viewer->assign('ERROR_MESSAGE', $errorMessage); 96 $viewer->assign('ERROR_DETAILS', $errorDetails); 97 $viewer->assign('CUSTOM_ACTIONS', $customActions); 98 $viewer->assign('MODULE','Import'); 99 100 $viewer->view('ImportError.tpl', 'Import'); 101 } 102 103 public static function showImportLockedError($lockInfo) { 104 105 $errorMessage = vtranslate('ERR_MODULE_IMPORT_LOCKED', 'Import'); 106 $errorDetails = array(vtranslate('LBL_MODULE_NAME', 'Import') => getTabModuleName($lockInfo['tabid']), 107 vtranslate('LBL_USER_NAME', 'Import') => getUserFullName($lockInfo['userid']), 108 vtranslate('LBL_LOCKED_TIME', 'Import') => $lockInfo['locked_since']); 109 110 self::showErrorPage($errorMessage, $errorDetails); 111 } 112 113 public static function showImportTableBlockedError($moduleName, $user) { 114 115 $errorMessage = vtranslate('ERR_UNIMPORTED_RECORDS_EXIST', 'Import'); 116 $customActions = array('LBL_CLEAR_DATA' => "location.href='index.php?module={$moduleName}&view=Import&mode=clearCorruptedData'"); 117 118 self::showErrorPage($errorMessage, '', $customActions); 119 } 120 121 public static function isUserImportBlocked($user) { 122 $adb = PearDatabase::getInstance(); 123 $tableName = self::getDbTableName($user); 124 125 if(Vtiger_Utils::CheckTable($tableName)) { 126 $result = $adb->query('SELECT 1 FROM '.$tableName.' WHERE status = '. Import_Data_Action::$IMPORT_RECORD_NONE); 127 if($adb->num_rows($result) > 0) { 128 return true; 129 } 130 } 131 return false; 132 } 133 134 public static function clearUserImportInfo($user) { 135 $adb = PearDatabase::getInstance(); 136 $tableName = self::getDbTableName($user); 137 138 $adb->query('DROP TABLE IF EXISTS '.$tableName); 139 Import_Lock_Action::unLock($user); 140 Import_Queue_Action::removeForUser($user); 141 } 142 143 public static function getAssignedToUserList($module) { 144 $cache = Vtiger_Cache::getInstance(); 145 if($cache->getUserList($module,$current_user->id)){ 146 return $cache->getUserList($module,$current_user->id); 147 } else { 148 $userList = get_user_array(FALSE, "Active", $current_user->id); 149 $cache->setUserList($module,$userList,$current_user->id); 150 return $userList; 151 } 152 } 153 154 public static function getAssignedToGroupList($module) { 155 $cache = Vtiger_Cache::getInstance(); 156 if($cache->getGroupList($module,$current_user->id)){ 157 return $cache->getGroupList($module,$current_user->id); 158 } else { 159 $groupList = get_group_array(FALSE, "Active", $current_user->id); 160 $cache->setGroupList($module,$groupList,$current_user->id); 161 return $groupList; 162 } 163 } 164 165 public static function hasAssignPrivilege($moduleName, $assignToUserId) { 166 $assignableUsersList = self::getAssignedToUserList($moduleName); 167 if(array_key_exists($assignToUserId, $assignableUsersList)) { 168 return true; 169 } 170 $assignableGroupsList = self::getAssignedToGroupList($moduleName); 171 if(array_key_exists($assignToUserId, $assignableGroupsList)) { 172 return true; 173 } 174 return false; 175 } 176 177 public static function validateFileUpload($request) { 178 $current_user = Users_Record_Model::getCurrentUserModel(); 179 180 $uploadMaxSize = self::getMaxUploadSize(); 181 $importDirectory = self::getImportDirectory(); 182 $temporaryFileName = self::getImportFilePath($current_user); 183 184 if($_FILES['import_file']['error']) { 185 $request->set('error_message', self::fileUploadErrorMessage($_FILES['import_file']['error'])); 186 return false; 187 } 188 if(!is_uploaded_file($_FILES['import_file']['tmp_name'])) { 189 $request->set('error_message', vtranslate('LBL_FILE_UPLOAD_FAILED', 'Import')); 190 return false; 191 } 192 if ($_FILES['import_file']['size'] > $uploadMaxSize) { 193 $request->set('error_message', vtranslate('LBL_IMPORT_ERROR_LARGE_FILE', 'Import'). 194 $uploadMaxSize.' '.vtranslate('LBL_IMPORT_CHANGE_UPLOAD_SIZE', 'Import')); 195 return false; 196 } 197 if(!is_writable($importDirectory)) { 198 $request->set('error_message', vtranslate('LBL_IMPORT_DIRECTORY_NOT_WRITABLE', 'Import')); 199 return false; 200 } 201 202 $fileCopied = move_uploaded_file($_FILES['import_file']['tmp_name'], $temporaryFileName); 203 if(!$fileCopied) { 204 $request->set('error_message', vtranslate('LBL_IMPORT_FILE_COPY_FAILED', 'Import')); 205 return false; 206 } 207 $fileReader = Import_Utils_Helper::getFileReader($request, $current_user); 208 209 if($fileReader == null) { 210 $request->set('error_message', vtranslate('LBL_INVALID_FILE', 'Import')); 211 return false; 212 } 213 214 $hasHeader = $fileReader->hasHeader(); 215 $firstRow = $fileReader->getFirstRowData($hasHeader); 216 if($firstRow === false) { 217 $request->set('error_message', vtranslate('LBL_NO_ROWS_FOUND', 'Import')); 218 return false; 219 } 220 return true; 221 } 222 223 static function fileUploadErrorMessage($error_code) { 224 switch ($error_code) { 225 case 1: 226 return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 227 case 2: 228 return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 229 case 3: 230 return 'The uploaded file was only partially uploaded'; 231 case 4: 232 return 'No file was uploaded'; 233 case 6: 234 return 'Missing a temporary folder'; 235 case 7: 236 return 'Failed to write file to disk'; 237 case 8: 238 return 'File upload stopped by extension'; 239 default: 240 return 'Unknown upload error'; 241 } 242 } 243 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |