2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
4 /*********************************************************************************
5 * SugarCRM Community Edition is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Affero General Public License version 3 as published by the
10 * Free Software Foundation with the addition of the following permission added
11 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20 * You should have received a copy of the GNU Affero General Public License along with
21 * this program; if not, see http://www.gnu.org/licenses or write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
28 * The interactive user interfaces in modified source and object code versions
29 * of this program must display Appropriate Legal Notices, as required under
30 * Section 5 of the GNU Affero General Public License version 3.
32 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33 * these Appropriate Legal Notices must retain the display of the "Powered by
34 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35 * technical reasons, the Appropriate Legal Notices must display the words
36 * "Powered by SugarCRM".
37 ********************************************************************************/
39 /*********************************************************************************
41 * Description: Static class to that is used to get the filenames for the various
43 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
44 * All Rights Reserved.
45 ********************************************************************************/
47 class ImportCacheFiles
52 const FILE_MISCELLANEOUS = 'misc';
53 const FILE_DUPLICATES = 'dupes';
54 const FILE_DUPLICATES_DISPLAY = 'dupesdisplay';
55 const FILE_ERRORS = 'error';
56 const FILE_ERROR_RECORDS = 'errorrecords';
57 const FILE_ERROR_RECORDS_ONLY = 'errorrecordsonly';
58 const FILE_STATUS = 'status';
62 * List of all cache file names
66 protected static $all_files = array(
67 self::FILE_MISCELLANEOUS,
68 self::FILE_DUPLICATES,
69 self::FILE_DUPLICATES_DISPLAY,
71 self::FILE_ERROR_RECORDS,
72 self::FILE_ERROR_RECORDS_ONLY,
77 * Get import directory name
80 public static function getImportDir()
82 return "upload://import";
87 * Function generates a download link for the given import file
89 * @param string $fileName String value of the upload file name
90 * @return string The converted URL of the file name
92 public static function convertFileNameToUrl($fileName)
94 $fileName = str_replace(self::getImportDir() . "/", "", $fileName);
95 $fileName = "index.php?entryPoint=download&id=ImportErrors&type=import&tempName=" . $fileName . "&isTempFile=1";
101 * Returns the filename for a temporary file
103 * @param string $type string to prepend to the filename, typically to indicate the file's use
104 * @return string filename
106 private static function _createFileName($type = self::FILE_MISCELLANEOUS)
108 global $current_user;
109 $importdir = self::getImportDir();
110 // ensure dir exists and writable
111 UploadStream::ensureDir($importdir, true);
113 return "$importdir/{$type}_{$current_user->id}.csv";
117 * Ensure that all cache files are writable or can be created
121 public static function ensureWritable()
123 foreach (self::$all_files as $type)
125 $filename = self::_createFileName($type);
126 if (file_exists($filename) && !is_writable($filename))
130 elseif (!is_writable(dirname($filename)))
139 * Returns the duplicates filename (the ones used to download to csv file
141 * @return string filename
143 public static function getDuplicateFileName()
145 return self::_createFileName(self::FILE_DUPLICATES);
149 * Returns the duplicates display filename (the one used for display in html)
151 * @return string filename
153 public static function getDuplicateFileDisplayName()
155 return self::_createFileName(self::FILE_DUPLICATES_DISPLAY);
159 * Returns the error filename
161 * @return string filename
163 public static function getErrorFileName()
165 return self::_createFileName(self::FILE_ERRORS);
169 * Returns the error records filename
171 * @return string filename
173 public static function getErrorRecordsFileName()
175 return self::_createFileName(self::FILE_ERROR_RECORDS);
179 * Returns the error records filename
181 * @return string filename
183 public static function getErrorRecordsWithoutErrorFileName()
185 return self::_createFileName(self::FILE_ERROR_RECORDS_ONLY);
189 * Returns the status filename
191 * @return string filename
193 public static function getStatusFileName()
195 return self::_createFileName(self::FILE_STATUS);
199 * Clears out all cache files in the import directory
201 public static function clearCacheFiles()
203 global $sugar_config;
204 $importdir = self::getImportDir();
205 if ( is_dir($importdir) ) {
206 $files = dir($importdir);
207 while (false !== ($file = $files->read())) {
208 if ( !is_dir($file) && stristr($file,'.csv') )
209 unlink("$importdir/$file");