]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/ImportCacheFiles.php
Merge pull request #100 from collinlee/master
[Github/sugarcrm.git] / modules / Import / ImportCacheFiles.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7  * 
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.
14  * 
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
18  * details.
19  * 
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
23  * 02110-1301 USA.
24  * 
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.
27  * 
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.
31  * 
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  ********************************************************************************/
38
39 /*********************************************************************************
40
41  * Description: Static class to that is used to get the filenames for the various
42  * cache files used
43  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
44  * All Rights Reserved.
45  ********************************************************************************/
46
47 class ImportCacheFiles
48 {
49     /**#@+
50      * Cache file names
51      */
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';
59     /**#@-*/
60
61     /**
62      * List of all cache file names
63      * 
64      * @var array
65      */
66     protected static $all_files = array(
67         self::FILE_MISCELLANEOUS,
68         self::FILE_DUPLICATES,
69         self::FILE_DUPLICATES_DISPLAY,
70         self::FILE_ERRORS,
71         self::FILE_ERROR_RECORDS,
72         self::FILE_ERROR_RECORDS_ONLY,
73         self::FILE_STATUS,
74     );
75
76     /**
77      * Get import directory name
78      * @return string
79      */
80     public static function getImportDir()
81     {
82         return "upload://import";
83     }
84
85
86     /**
87      * convertFileName
88      *
89      * This function returns the name of an upload file link converted as a url in e.g. href
90      *
91      * @param string $file_name String value of the upload file name
92      * @return string The converted URL of the file name
93      */
94     public static function convertFileNameToUrl($file_name)
95     {
96         require_once('include/upload_file.php');
97         $file_name = str_replace('upload://import', UploadStream::getDir() . '/import', $file_name);
98         return $file_name;
99     }
100
101
102     /**
103      * Returns the filename for a temporary file
104      *
105      * @param  string $type string to prepend to the filename, typically to indicate the file's use
106      * @return string filename
107      */
108     private static function _createFileName($type = self::FILE_MISCELLANEOUS)
109     {
110         global $current_user;
111         $importdir = self::getImportDir();
112         // ensure dir exists and writable
113         UploadStream::ensureDir($importdir, true);
114
115         return "$importdir/{$type}_{$current_user->id}.csv";
116     }
117
118     /**
119      * Ensure that all cache files are writable or can be created
120      * 
121      * @return bool
122      */
123     public static function ensureWritable()
124     {
125         foreach (self::$all_files as $type)
126         {
127             $filename = self::_createFileName($type);
128             if (file_exists($filename) && !is_writable($filename))
129             {
130                 return false;
131             }
132             elseif (!is_writable(dirname($filename)))
133             {
134                 return false;
135             }
136         }
137         return true;
138     }
139
140     /**
141      * Returns the duplicates filename (the ones used to download to csv file
142      *
143      * @return string filename
144      */
145     public static function getDuplicateFileName()
146     {
147         return self::_createFileName(self::FILE_DUPLICATES);
148     }
149
150     /**
151      * Returns the duplicates display filename (the one used for display in html)
152      *
153      * @return string filename
154      */
155     public static function getDuplicateFileDisplayName()
156     {
157         return self::_createFileName(self::FILE_DUPLICATES_DISPLAY);
158     }
159
160     /**
161      * Returns the error filename
162      *
163      * @return string filename
164      */
165     public static function getErrorFileName()
166     {
167         return self::_createFileName(self::FILE_ERRORS);
168     }
169
170     /**
171      * Returns the error records filename
172      *
173      * @return string filename
174      */
175     public static function getErrorRecordsFileName()
176     {
177         return self::_createFileName(self::FILE_ERROR_RECORDS);
178     }
179
180     /**
181      * Returns the error records filename
182      *
183      * @return string filename
184      */
185     public static function getErrorRecordsWithoutErrorFileName()
186     {
187         return self::_createFileName(self::FILE_ERROR_RECORDS_ONLY);
188     }
189
190     /**
191      * Returns the status filename
192      *
193      * @return string filename
194      */
195     public static function getStatusFileName()
196     {
197         return self::_createFileName(self::FILE_STATUS);
198     }
199
200     /**
201      * Clears out all cache files in the import directory
202      */
203     public static function clearCacheFiles()
204     {
205         global $sugar_config;
206         $importdir = self::getImportDir();
207         if ( is_dir($importdir) ) {
208             $files = dir($importdir);
209             while (false !== ($file = $files->read())) {
210                 if ( !is_dir($file) && stristr($file,'.csv') )
211                     unlink("$importdir/$file");
212             }
213         }
214     }
215 }