]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/views/view.last.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Import / views / view.last.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description: view handler for last step of the import process
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  ********************************************************************************/
44 require_once('modules/Import/views/ImportView.php');
45 require_once('modules/Import/ImportCacheFiles.php');
46 require_once('modules/Import/sources/ImportFile.php');
47 require_once('modules/Import/views/ImportListView.php');
48 require_once('include/ListView/ListViewFacade.php');
49
50
51 class ImportViewLast extends ImportView
52 {
53     protected $pageTitleKey = 'LBL_STEP_5_TITLE';
54
55     var $lvf;
56
57         /**
58      * @see SugarView::display()
59      */
60         public function display()
61     {
62         global $mod_strings, $app_strings, $current_user, $sugar_config, $current_language;
63
64
65
66         $this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
67         $this->ss->assign("TYPE", $_REQUEST['type']);
68         $this->ss->assign("HEADER", $app_strings['LBL_IMPORT']." ". $mod_strings['LBL_MODULE_NAME']);
69         $this->ss->assign("MODULE_TITLE", $this->getModuleTitle(false));
70         // lookup this module's $mod_strings to get the correct module name
71         $module_mod_strings =
72             return_module_language($current_language, $_REQUEST['import_module']);
73         $this->ss->assign("MODULENAME",$module_mod_strings['LBL_MODULE_NAME']);
74
75         // read status file to get totals for records imported, errors, and duplicates
76         $count        = 0;
77         $errorCount   = 0;
78         $dupeCount    = 0;
79         $createdCount = 0;
80         $updatedCount = 0;
81         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
82         while (( $row = fgetcsv($fp, 8192) ) !== FALSE)
83         {
84             $count         += (int) $row[0];
85             $errorCount    += (int) $row[1];
86             $dupeCount     += (int) $row[2];
87             $createdCount  += (int) $row[3];
88             $updatedCount  += (int) $row[4];
89         }
90         fclose($fp);
91
92         $this->ss->assign("showUndoButton",FALSE);
93         if($createdCount > 0)
94         {
95                 $this->ss->assign("showUndoButton",TRUE);
96         }
97
98         if ($errorCount > 0 &&  ($createdCount <= 0 && $updatedCount <= 0))
99             $activeTab = 2;
100         else if($dupeCount > 0 &&  ($createdCount <= 0 && $updatedCount <= 0))
101             $activeTab = 1;
102         else
103             $activeTab = 0;
104
105         $this->ss->assign("JAVASCRIPT", $this->_getJS($activeTab));
106         $this->ss->assign("errorCount",$errorCount);
107         $this->ss->assign("dupeCount",$dupeCount);
108         $this->ss->assign("createdCount",$createdCount);
109         $this->ss->assign("updatedCount",$updatedCount);
110         $this->ss->assign("errorFile",ImportCacheFiles::convertFileNameToUrl(ImportCacheFiles::getErrorFileName()));
111         $this->ss->assign("errorrecordsFile", ImportCacheFiles::convertFileNameToUrl(ImportCacheFiles::getErrorRecordsWithoutErrorFileName()));
112         $this->ss->assign("dupeFile", ImportCacheFiles::convertFileNameToUrl(ImportCacheFiles::getDuplicateFileName()));
113
114         if ( $this->bean->object_name == "Prospect" )
115         {
116                 $this->ss->assign("PROSPECTLISTBUTTON", $this->_addToProspectListButton());
117         }
118         else {
119             $this->ss->assign("PROSPECTLISTBUTTON","");
120         }
121
122         $resultsTable = "";
123         foreach ( UsersLastImport::getBeansByImport($_REQUEST['import_module']) as $beanname )
124         {
125             // load bean
126             if ( !( $this->bean instanceof $beanname ) )
127             {
128                 $this->bean = new $beanname;
129             }
130            $resultsTable .= $this->getListViewResults();
131         }
132         if(empty($resultsTable))
133         {
134             $resultsTable = $this->getListViewResults();
135         }
136
137         $this->ss->assign("RESULTS_TABLE", $resultsTable);
138         $this->ss->assign("ERROR_TABLE", $this->getListViewTableFromFile(ImportCacheFiles::getErrorRecordsFileName(), 'errors') );
139         $this->ss->assign("DUP_TABLE", $this->getListViewTableFromFile(ImportCacheFiles::getDuplicateFileDisplayName(), 'dup'));
140         $content = $this->ss->fetch('modules/Import/tpls/last.tpl');
141         $this->ss->assign("CONTENT",$content);
142         $this->ss->display('modules/Import/tpls/wizardWrapper.tpl');
143     }
144
145     protected function getListViewResults()
146     {
147         global $mod_strings, $current_language;
148         // build listview to show imported records
149         $lvf = !empty($this->lvf) ? $this->lvf : new ListViewFacade($this->bean, $this->bean->module_dir, 0);
150
151         $params = array();
152         if(!empty($_REQUEST['orderBy']))
153         {
154             $params['orderBy'] = $_REQUEST['orderBy'];
155             $params['overrideOrder'] = true;
156             if(!empty($_REQUEST['sortOrder'])) $params['sortOrder'] = $_REQUEST['sortOrder'];
157         }
158         $beanname = ($this->bean->object_name == 'Case' ? 'aCase' : $this->bean->object_name);
159         // add users_last_import joins so we only show records done in this import
160         $params['custom_from']  = ', users_last_import';
161         $params['custom_where'] = " AND users_last_import.assigned_user_id = '{$GLOBALS['current_user']->id}'
162                 AND users_last_import.bean_type = '{$beanname}'
163                 AND users_last_import.bean_id = {$this->bean->table_name}.id
164                 AND users_last_import.deleted = 0
165                 AND {$this->bean->table_name}.deleted = 0";
166
167         $lvf->lv->mergeduplicates = false;
168         $lvf->lv->showMassupdateFields = false;
169         if ( $lvf->type == 2 )
170             $lvf->template = 'include/ListView/ListViewNoMassUpdate.tpl';
171
172         $module_mod_strings = return_module_language($current_language, $this->bean->module_dir);
173         $lvf->setup('', '', $params, $module_mod_strings, 0, -1, '', strtoupper($beanname), array(), 'id');
174         global $app_list_strings;
175         return $lvf->display($app_list_strings['moduleList'][$this->bean->module_dir], 'main', TRUE);
176
177     }
178
179     protected function getListViewTableFromFile($fileName, $tableName)
180     {
181         $has_header = $_REQUEST['has_header'] == 'on' ? TRUE : FALSE;
182         $if = new ImportFile($fileName, ",", '"', FALSE, FALSE);
183         $if->setHeaderRow($has_header);
184         $lv = new ImportListView($if,array('offset'=> 0), $tableName);
185         return $lv->display(TRUE);
186     }
187
188     /**
189      * Returns JS used in this view
190      */
191     private function _getJS($activeTab)
192     {
193         return <<<EOJAVASCRIPT
194
195 document.getElementById('importmore').onclick = function(){
196     document.getElementById('importlast').action.value = 'Step1';
197     return true;
198 }
199
200 document.getElementById('finished').onclick = function(){
201     document.getElementById('importlast').module.value = document.getElementById('importlast').import_module.value;
202     document.getElementById('importlast').action.value = 'index';
203         return true;
204
205 }
206
207 if ( typeof(SUGAR) == 'undefined' )
208     SUGAR = {};
209 if ( typeof(SUGAR.IV) == 'undefined' )
210     SUGAR.IV = {};
211
212 SUGAR.IV = {
213
214     getTable : function(tableID, offset) {
215         var callback = {
216             success: function(o)
217             {
218                 var tableKey = tableID + '_table';
219                 document.getElementById(tableKey).innerHTML = o.responseText;
220             },
221             failure: function(o) {},
222         };
223         var has_header = document.getElementById('importlast').has_header.value
224         var url = 'index.php?action=RefreshTable&module=Import&offset=' + offset + '&tableID=' + tableID + '&has_header=' + has_header;
225         YAHOO.util.Connect.asyncRequest('GET', url, callback);
226     },
227     togglePages : function(activePage)
228     {
229         var num_tabs = 3;
230         var pageId = 'pageNumIW_' + activePage;
231         activeDashboardPage = activePage;
232         activeTab = activePage;
233
234         //hide all pages first for display purposes
235         for(var i=0; i < num_tabs; i++)
236         {
237             var pageDivId = 'pageNumIW_'+i+'_div';
238             var pageDivElem = document.getElementById(pageDivId);
239             pageDivElem.style.display = 'none';
240         }
241
242         for(var i=0; i < num_tabs; i++)
243         {
244             var tabId = 'pageNumIW_'+i;
245             var anchorId = 'pageNumIW_'+i+'_anchor';
246             var pageDivId = 'pageNumIW_'+i+'_div';
247
248             var tabElem = document.getElementById(tabId);
249             var anchorElem = document.getElementById(anchorId);
250             var pageDivElem = document.getElementById(pageDivId);
251
252             if(tabId == pageId)
253             {
254                 tabElem.className = 'active';
255                 anchorElem.className = 'current';
256                 pageDivElem.style.display = '';
257             }
258             else
259             {
260                 tabElem.className = '';
261                 anchorElem.className = '';
262             }
263         }
264     }
265 }
266
267 SUGAR.IV.togglePages('$activeTab');
268
269
270 EOJAVASCRIPT;
271     }
272     /**
273      * Returns a button to add this list of prospects to a Target List
274      *
275      * @return string html code to display button
276      */
277     private function _addToProspectListButton()
278     {
279         global $app_strings, $sugar_version, $sugar_config, $current_user;
280
281         $query = "SELECT distinct prospects.id, prospects.assigned_user_id, prospects.first_name, prospects.last_name, prospects.phone_work, prospects.title,
282                                 email_addresses.email_address email1, users.user_name as assigned_user_name
283                                 FROM users_last_import,prospects
284                 LEFT JOIN users ON prospects.assigned_user_id=users.id
285                                 LEFT JOIN email_addr_bean_rel on prospects.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='Prospect' and email_addr_bean_rel.primary_address=1 and email_addr_bean_rel.deleted=0
286                                 LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id
287                                 WHERE users_last_import.assigned_user_id = '{$current_user->id}' AND users_last_import.bean_type='Prospect' AND users_last_import.bean_id=prospects.id
288                                 AND users_last_import.deleted=0 AND prospects.deleted=0";
289
290         $popup_request_data = array(
291             'call_back_function' => 'set_return_and_save_background',
292             'form_name' => 'DetailView',
293             'field_to_name_array' => array(
294                 'id' => 'subpanel_id',
295             ),
296             'passthru_data' => array(
297                 'child_field' => 'notused',
298                 'return_url' => 'notused',
299                 'link_field_name' => 'notused',
300                 'module_name' => 'notused',
301                 'refresh_page'=>'1',
302                 'return_type'=>'addtoprospectlist',
303                 'parent_module'=>'ProspectLists',
304                 'parent_type'=>'ProspectList',
305                 'child_id'=>'id',
306                 'link_attribute'=>'prospects',
307                 'link_type'=>'default',  //polymorphic or default
308             )
309         );
310
311         $popup_request_data['passthru_data']['query'] = urlencode($query);
312
313         $json = getJSONobj();
314         $encoded_popup_request_data = $json->encode($popup_request_data);
315
316         return <<<EOHTML
317 <script type="text/javascript" src="include/SubPanel/SubPanelTiles.js?s={$sugar_version}&c={$sugar_config['js_custom_version']}"></script>
318 <input align=right" type="button" name="select_button" id="select_button" class="button"
319      title="{$app_strings['LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL']}"
320      value="{$app_strings['LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL']}"
321      onclick='open_popup("ProspectLists",600,400,"",true,true,$encoded_popup_request_data,"Single","true");' />
322 EOHTML;
323
324     }
325 }
326 ?>