]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/views/view.last.php
Release 6.4.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-2011 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         /**
56      * @see SugarView::display()
57      */
58         public function display()
59     {
60         global $mod_strings, $app_strings, $current_user, $sugar_config, $current_language;
61
62
63
64         $this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
65         $this->ss->assign("TYPE", $_REQUEST['type']);
66         $this->ss->assign("HEADER", $app_strings['LBL_IMPORT']." ". $mod_strings['LBL_MODULE_NAME']);
67         $this->ss->assign("MODULE_TITLE", $this->getModuleTitle(false));
68         // lookup this module's $mod_strings to get the correct module name
69         $module_mod_strings =
70             return_module_language($current_language, $_REQUEST['import_module']);
71         $this->ss->assign("MODULENAME",$module_mod_strings['LBL_MODULE_NAME']);
72
73         // read status file to get totals for records imported, errors, and duplicates
74         $count        = 0;
75         $errorCount   = 0;
76         $dupeCount    = 0;
77         $createdCount = 0;
78         $updatedCount = 0;
79         $fp = sugar_fopen(ImportCacheFiles::getStatusFileName(),'r');
80         while (( $row = fgetcsv($fp, 8192) ) !== FALSE)
81         {
82             $count         += (int) $row[0];
83             $errorCount    += (int) $row[1];
84             $dupeCount     += (int) $row[2];
85             $createdCount  += (int) $row[3];
86             $updatedCount  += (int) $row[4];
87         }
88         fclose($fp);
89
90         $this->ss->assign("showUndoButton",FALSE);
91         if($createdCount > 0)
92         {
93                 $this->ss->assign("showUndoButton",TRUE);
94         }
95
96         if ($errorCount > 0 &&  ($createdCount <= 0 && $updatedCount <= 0))
97             $activeTab = 2;
98         else if($dupeCount > 0 &&  ($createdCount <= 0 && $updatedCount <= 0))
99             $activeTab = 1;
100         else
101             $activeTab = 0;
102
103         $this->ss->assign("JAVASCRIPT", $this->_getJS($activeTab));
104         $this->ss->assign("errorCount",$errorCount);
105         $this->ss->assign("dupeCount",$dupeCount);
106         $this->ss->assign("createdCount",$createdCount);
107         $this->ss->assign("updatedCount",$updatedCount);
108         $this->ss->assign("errorFile",ImportCacheFiles::getErrorFileName());
109         $this->ss->assign("errorrecordsFile",ImportCacheFiles::getErrorRecordsWithoutErrorFileName());
110         $this->ss->assign("dupeFile",ImportCacheFiles::getDuplicateFileName());
111
112         if ( $this->bean->object_name == "Prospect" )
113         {
114                 $this->ss->assign("PROSPECTLISTBUTTON", $this->_addToProspectListButton());
115         }
116         else {
117             $this->ss->assign("PROSPECTLISTBUTTON","");
118         }
119
120         $resultsTable = "";
121         foreach ( UsersLastImport::getBeansByImport($_REQUEST['import_module']) as $beanname )
122         {
123             // load bean
124             if ( !( $this->bean instanceof $beanname ) )
125             {
126                 $this->bean = new $beanname;
127             }
128            $resultsTable .= $this->getListViewResults();
129         }
130         if(empty($resultsTable))
131         {
132             $resultsTable = $this->getListViewResults();
133         }
134
135         $this->ss->assign("RESULTS_TABLE", $resultsTable);
136         $this->ss->assign("ERROR_TABLE", $this->getListViewTableFromFile(ImportCacheFiles::getErrorRecordsFileName(), 'errors') );
137         $this->ss->assign("DUP_TABLE", $this->getListViewTableFromFile(ImportCacheFiles::getDuplicateFileDisplayName(), 'dup'));
138         $content = $this->ss->fetch('modules/Import/tpls/last.tpl');
139         $this->ss->assign("CONTENT",$content);
140         $this->ss->display('modules/Import/tpls/wizardWrapper.tpl');
141     }
142
143     protected function getListViewResults()
144     {
145         global $mod_strings, $current_language;
146         // build listview to show imported records
147         $lvf = new ListViewFacade($this->bean, $this->bean->module_dir, 0);
148
149         $params = array();
150         if(!empty($_REQUEST['orderBy']))
151         {
152             $params['orderBy'] = $_REQUEST['orderBy'];
153             $params['overrideOrder'] = true;
154             if(!empty($_REQUEST['sortOrder'])) $params['sortOrder'] = $_REQUEST['sortOrder'];
155         }
156         $beanname = ($this->bean->object_name == 'Case' ? 'aCase' : $this->bean->object_name);
157         // add users_last_import joins so we only show records done in this import
158         $params['custom_from']  = ', users_last_import';
159         $params['custom_where'] = " AND users_last_import.assigned_user_id = '{$GLOBALS['current_user']->id}'
160                 AND users_last_import.bean_type = '{$beanname}'
161                 AND users_last_import.bean_id = {$this->bean->table_name}.id
162                 AND users_last_import.deleted = 0
163                 AND {$this->bean->table_name}.deleted = 0";
164         $where = " {$this->bean->table_name}.id IN (
165                         SELECT users_last_import.bean_id
166                             FROM users_last_import
167                             WHERE users_last_import.assigned_user_id = '{$GLOBALS['current_user']->id}'
168                                 AND users_last_import.bean_type = '{$beanname}'
169                                 AND users_last_import.deleted = 0 )";
170
171         $lvf->lv->mergeduplicates = false;
172         $lvf->lv->showMassupdateFields = false;
173         if ( $lvf->type == 2 )
174             $lvf->template = 'include/ListView/ListViewNoMassUpdate.tpl';
175
176         $module_mod_strings = return_module_language($current_language, $this->bean->module_dir);
177         $lvf->setup('', $where, $params, $module_mod_strings, 0, -1, '', strtoupper($beanname), array(), 'id');
178         global $app_list_strings;
179         return $lvf->display($app_list_strings['moduleList'][$this->bean->module_dir], 'main', TRUE);
180
181     }
182
183     protected function getListViewTableFromFile($fileName, $tableName)
184     {
185         $has_header = $_REQUEST['has_header'] == 'on' ? TRUE : FALSE;
186         $if = new ImportFile($fileName, ",", '"', FALSE, FALSE);
187         $if->setHeaderRow($has_header);
188         $lv = new ImportListView($if,array('offset'=> 0), $tableName);
189         return $lv->display(TRUE);
190     }
191
192     /**
193      * Returns JS used in this view
194      */
195     private function _getJS($activeTab)
196     {
197         return <<<EOJAVASCRIPT
198
199 document.getElementById('importmore').onclick = function(){
200     document.getElementById('importlast').action.value = 'Step1';
201     return true;
202 }
203
204 document.getElementById('finished').onclick = function(){
205     document.getElementById('importlast').module.value = document.getElementById('importlast').import_module.value;
206     document.getElementById('importlast').action.value = 'index';
207         return true;
208
209 }
210
211 if ( typeof(SUGAR) == 'undefined' )
212     SUGAR = {};
213 if ( typeof(SUGAR.IV) == 'undefined' )
214     SUGAR.IV = {};
215
216 SUGAR.IV = {
217
218     getTable : function(tableID, offset) {
219         var callback = {
220             success: function(o)
221             {
222                 var tableKey = tableID + '_table';
223                 document.getElementById(tableKey).innerHTML = o.responseText;
224             },
225             failure: function(o) {},
226         };
227         var has_header = document.getElementById('importlast').has_header.value
228         var url = 'index.php?action=RefreshTable&module=Import&offset=' + offset + '&tableID=' + tableID + '&has_header=' + has_header;
229         YAHOO.util.Connect.asyncRequest('GET', url, callback);
230     },
231     togglePages : function(activePage)
232     {
233         var num_tabs = 3;
234         var pageId = 'pageNumIW_' + activePage;
235         activeDashboardPage = activePage;
236         activeTab = activePage;
237
238         //hide all pages first for display purposes
239         for(var i=0; i < num_tabs; i++)
240         {
241             var pageDivId = 'pageNumIW_'+i+'_div';
242             var pageDivElem = document.getElementById(pageDivId);
243             pageDivElem.style.display = 'none';
244         }
245
246         for(var i=0; i < num_tabs; i++)
247         {
248             var tabId = 'pageNumIW_'+i;
249             var anchorId = 'pageNumIW_'+i+'_anchor';
250             var pageDivId = 'pageNumIW_'+i+'_div';
251
252             var tabElem = document.getElementById(tabId);
253             var anchorElem = document.getElementById(anchorId);
254             var pageDivElem = document.getElementById(pageDivId);
255
256             if(tabId == pageId)
257             {
258                 tabElem.className = 'active';
259                 anchorElem.className = 'current';
260                 pageDivElem.style.display = '';
261             }
262             else
263             {
264                 tabElem.className = '';
265                 anchorElem.className = '';
266             }
267         }
268     }
269 }
270
271 SUGAR.IV.togglePages('$activeTab');
272
273
274 EOJAVASCRIPT;
275     }
276     /**
277      * Returns a button to add this list of prospects to a Target List
278      *
279      * @return string html code to display button
280      */
281     private function _addToProspectListButton()
282     {
283         global $app_strings, $sugar_version, $sugar_config, $current_user;
284
285         $query = "SELECT distinct prospects.id, prospects.assigned_user_id, prospects.first_name, prospects.last_name, prospects.phone_work, prospects.title,
286                                 email_addresses.email_address email1, users.user_name as assigned_user_name
287                                 FROM users_last_import,prospects
288                 LEFT JOIN users ON prospects.assigned_user_id=users.id
289                                 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
290                                 LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id
291                                 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
292                                 AND users_last_import.deleted=0 AND prospects.deleted=0";
293
294         $popup_request_data = array(
295             'call_back_function' => 'set_return_and_save_background',
296             'form_name' => 'DetailView',
297             'field_to_name_array' => array(
298                 'id' => 'subpanel_id',
299             ),
300             'passthru_data' => array(
301                 'child_field' => 'notused',
302                 'return_url' => 'notused',
303                 'link_field_name' => 'notused',
304                 'module_name' => 'notused',
305                 'refresh_page'=>'1',
306                 'return_type'=>'addtoprospectlist',
307                 'parent_module'=>'ProspectLists',
308                 'parent_type'=>'ProspectList',
309                 'child_id'=>'id',
310                 'link_attribute'=>'prospects',
311                 'link_type'=>'default',  //polymorphic or default
312             )
313         );
314
315         $popup_request_data['passthru_data']['query'] = urlencode($query);
316
317         $json = getJSONobj();
318         $encoded_popup_request_data = $json->encode($popup_request_data);
319
320         return <<<EOHTML
321 <script type="text/javascript" src="include/SubPanel/SubPanelTiles.js?s={$sugar_version}&c={$sugar_config['js_custom_version']}"></script>
322 <input align=right" type="button" name="select_button" id="select_button" class="button"
323      title="{$app_strings['LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL']}"
324      accesskey="{$app_strings['LBL_ADD_TO_PROSPECT_LIST_BUTTON_KEY']}"
325      value="{$app_strings['LBL_ADD_TO_PROSPECT_LIST_BUTTON_LABEL']}"
326      onclick='open_popup("ProspectLists",600,400,"",true,true,$encoded_popup_request_data,"Single","true");' />
327 EOHTML;
328
329     }
330 }
331 ?>