2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
40 $mod_strings_users = $mod_strings;
43 if(!is_admin($current_user)
44 && !is_admin_for_module($GLOBALS['current_user'],'Users')
46 sugar_die("You cannot access this page.");
51 $return_module = isset($_REQUEST['return_module']) ? $_REQUEST['return_module'] : '';
52 $return_action = isset($_REQUEST['return_action']) ? $_REQUEST['return_action'] : '';
53 $return_id = isset($_REQUEST['return_id']) ? $_REQUEST['return_id'] : '';
54 if(!empty($return_module))
55 $cancel_location = "index.php?module=".$return_module."&action=".$return_action."&record=".$return_id;
57 $cancel_location = "index.php?module=Users&action=index";
59 echo "<h2 class='moduleTitle' style=\"margin-bottom:0px;\">{$mod_strings_users['LBL_REASS_SCRIPT_TITLE']}</h2>";
61 // Include Metadata for processing
62 require_once("modules/Users/metadata/reassignScriptMetadata.php");
63 if(file_exists("custom/modules/Users/reassignScriptMetadata_override.php")){
64 include("custom/modules/Users/reassignScriptMetadata_override.php");
67 if(!empty($_GET['record'])){
68 unset($_SESSION['reassignRecords']);
69 $_SESSION['reassignRecords']['fromuser'] = $_GET['record'];
72 if(!isset($_POST['fromuser']) && !isset($_GET['execute'])){
73 ///////////////////// BEGIN STEP 1 - Select users/modules /////////////////////////
74 $exclude_modules = array(
83 if(isset($_GET['clear']) && $_GET['clear'] == 'true'){
84 unset($_SESSION['reassignRecords']);
87 <form method=post action="index.php?module=Users&action=reassignUserRecords" name='EditView' id='EditView'>
88 <table cellspacing='1' cellpadding='1' border='0'>
90 <td><?php echo $mod_strings_users['LBL_REASS_DESC_PART1']."<BR><br>"?></td>
94 <input type=submit class="button" value="<?php echo $mod_strings_users['LBL_REASS_BUTTON_CONTINUE']; ?>" name=steponesubmit>
95 <input type=button class="button" value="<?php echo $mod_strings_users['LBL_REASS_BUTTON_CLEAR']; ?>" onclick='clearCurrentRecords();'>
96 <input type=button class="button" value="<?php echo $app_strings['LBL_CANCEL_BUTTON_LABEL']; ?>" onclick='document.location="<?php echo $cancel_location ?>"'>
100 <table border='0' cellspacing='0' cellpadding='0' class='edit view'>
104 <?php echo $mod_strings_users['LBL_REASS_USER_FROM']; ?>
106 <select name="fromuser" id='fromuser'>
108 $active_users = get_user_array(FALSE);
109 $inactive_users = get_user_array(FALSE, "Inactive");
110 $all_users = array_merge($active_users, $inactive_users);
111 // sb - issue with php array_merge causing array index '1' to change to '0'
112 if(isset($all_users[0])){
113 $all_users[1] = 'admin';
114 unset($all_users[0]);
117 echo get_select_options_with_id($all_users, isset($_SESSION['reassignRecords']['fromuser']) ? $_SESSION['reassignRecords']['fromuser'] : '');
122 <?php echo $mod_strings_users['LBL_REASS_USER_TO']; ?>
124 <select name="touser" id="touser">
126 echo get_select_options_with_id($all_users, isset($_SESSION['reassignRecords']['touser']) ? $_SESSION['reassignRecords']['touser'] : '');
132 <?php echo $mod_strings_users['LBL_REASS_MOD_REASSIGN']; ?>
134 <select size="6" name='modules[]' multiple="true" id='modulemultiselect' onchange="updateDivDisplay(this);">
136 if(!isset($_SESSION['reassignRecords']['assignedModuleListCache'])){
137 $beanListDup = $beanList;
138 foreach($beanListDup as $m => $p){
139 if(empty($beanFiles[$p])){
140 unset($beanListDup[$m]);
143 require_once($beanFiles[$p]);
145 if( !isset($obj->field_defs['assigned_user_id']) ||
147 isset($obj->field_defs['assigned_user_id']) &&
148 isset($obj->field_defs['assigned_user_id']['source']) &&
149 $obj->field_defs['assigned_user_id']['source'] == "non-db"
153 unset($beanListDup[$m]);
157 $beanListDup = array_diff($beanListDup, $exclude_modules);
160 $beanListDupDisp=array() ;
161 foreach($beanListDup as $m => $p){
162 $beanListDupDisp[$app_list_strings['moduleList'][$m]]=$p;
164 $_SESSION['reassignRecords']['assignedModuleListCache'] = $beanListDup;
165 $_SESSION['reassignRecords']['assignedModuleListCacheDisp'] = $beanListDupDisp;
167 $beanListDup = array_flip($_SESSION['reassignRecords']['assignedModuleListCache']);
168 $beanListFlip = array_flip($_SESSION['reassignRecords']['assignedModuleListCacheDisp']);
169 asort($beanListFlip);
171 if(!empty($_SESSION['reassignRecords']['modules'])){
172 foreach($_SESSION['reassignRecords']['modules'] as $mod => $arr)
175 echo get_select_options_with_id($beanListFlip, $selected);
184 foreach($moduleFilters as $modFilter => $fieldArray){
185 $display = (!empty($fieldArray['display_default']) && $fieldArray['display_default'] == true ? "block" : "none");
187 $t_mod_strings=return_module_language($GLOBALS['current_language'], $modFilter);
188 echo "<div id=\"div_{$app_list_strings['moduleList'][$modFilter]}\" style=\"display:$display\">\n";
189 echo "<h5 style=\"padding-left:0px; margin-bottom:4px;\">{$app_list_strings['moduleList'][$modFilter]} ", " {$mod_strings_users['LBL_REASS_FILTERS']}</h5>\n";
190 foreach($fieldArray['fields'] as $meta){
192 $name = (!empty($meta['name']) ? $meta['name'] : "");
193 $size = (!empty($meta['size']) ? "size=\"{$meta['size']}\"" : "");
195 echo $t_mod_strings[$meta['vname']] ."\n<BR>\n";
196 switch($meta['type']){
201 $multi = "multiple=\"true\"";
203 // NO BREAK - Continue into select
207 if(!empty($_SESSION['reassignRecords']['filters'][$meta['name']])){
208 $sel = $_SESSION['reassignRecords']['filters'][$meta['name']];
210 $extra = get_select_options_with_id($meta['dropdown'], $sel);
211 $extra .= "\n</select>";
214 //echo "Skipping field {$meta['name']} since the type is not supported<BR>";
217 echo "<$tag $size name=\"$name\" $multi>\n$extra";
226 <table cellspacing='1' cellpadding='1' border='0'>
229 <input type=submit class="button" value="<?php echo $mod_strings_users['LBL_REASS_BUTTON_CONTINUE']; ?>" name=steponesubmit>
230 <input type=button class="button" value="<?php echo $mod_strings_users['LBL_REASS_BUTTON_CLEAR']; ?>" onclick='clearCurrentRecords();'>
231 <input type=button class="button" value="<?php echo $app_strings['LBL_CANCEL_BUTTON_LABEL']; ?>" onclick='document.location="<?php echo $cancel_location ?>"'>
238 ///////////////////// END STEP 1 - Select users/modules /////////////////////////
240 else if(!isset($_GET['execute'])){
241 ///////////////////// BEGIN STEP 2 - Confirm Selections /////////////////////////
242 if(empty($_POST['modules'])){
243 sugar_die($mod_strings_users['ERR_REASS_SELECT_MODULE']);
245 if($_POST['fromuser'] == $_POST['touser']){
246 sugar_die($mod_strings_users['ERR_REASS_DIFF_USERS']);
249 global $current_user;
250 // Set the from and to user names so that we can display them in the results
251 $fromusername = $_POST['fromuser'];
252 $tousername = $_POST['touser'];
254 $query = "select user_name, id from users where id in ('{$_POST['fromuser']}', '{$_POST['touser']}')";
255 $res = $GLOBALS['db']->query($query);
256 while($row = $GLOBALS['db']->fetchByAssoc($res)){
257 if($row['id'] == $_POST['fromuser'])
258 $fromusername = $row['user_name'];
259 if($row['id'] == $_POST['touser'])
260 $tousername = $row['user_name'];
262 echo "{$mod_strings_users['LBL_REASS_DESC_PART2']}\n";
263 echo "<form action=\"index.php?module=Users&action=reassignUserRecords&execute=true\" method=post>\n";
264 echo "<BR>{$mod_strings_users['LBL_REASS_NOTES_TITLE']}\n";
266 echo "<li>* {$mod_strings_users['LBL_REASS_NOTES_ONE']}\n";
267 echo "<li>* {$mod_strings_users['LBL_REASS_NOTES_TWO']}\n";
268 echo "<li>* {$mod_strings_users['LBL_REASS_NOTES_THREE']}\n";
270 $help_img = SugarThemeRegistry::current()->getImage('helpInline','border="0" onmouseout="return nd();" onmouseover="return overlib(\''.$mod_strings['LBL_REASS_VERBOSE_HELP'].'\', FGCLASS, \'olFgClass\', CGCLASS, \'olCgClass\', BGCLASS, \'olBgClass\', TEXTFONTCLASS, \'olFontClass\', CAPTIONFONTCLASS, \'olCapFontClass\', CLOSEFONTCLASS, \'olCloseFontClass\');"');
271 echo "<BR><input type=checkbox name=verbose> {$mod_strings_users['LBL_REASS_VERBOSE_OUTPUT']}".$help_img."<BR>\n";
272 unset($_SESSION['reassignRecords']['modules']);
273 $beanListFlip = array_flip($_SESSION['reassignRecords']['assignedModuleListCache']);
274 foreach($_POST['modules'] as $module){
275 if(!array_key_exists($module, $beanListFlip)){
276 //echo "$module not found as key in \$beanListFlip. Skipping $module.<BR>";
279 $p_module = $beanListFlip[$module];
281 require_once($beanFiles[$module]);
282 $object = new $module();
283 if(empty($object->table_name)){
284 // echo "<h5>Could not find the database table for $p_module.</h5>";
288 echo "<h5>{$mod_strings_users['LBL_REASS_ASSESSING']} {$app_list_strings['moduleList'][$p_module]}</h5>";
290 echo "<table border='0' cellspacing='0' cellpadding='0' class='detail view'>\n";
294 $q_select = "select id";
295 $q_update = "update ";
296 $q_set = " set assigned_user_id = '{$_POST['touser']}', ".
297 "date_modified = '".date("Y-m-d H:i:s")."', ".
298 "modified_user_id = '{$current_user->id}' ";
299 $q_tables = " {$object->table_name} ";
300 $q_where = "where {$object->table_name}.deleted=0 and {$object->table_name}.assigned_user_id = '{$_POST['fromuser']}' ";
302 // Process conditions based on metadata
303 if(isset($moduleFilters[$p_module]['fields']) && is_array($moduleFilters[$p_module]['fields'])){
304 $custom_added = false;
305 foreach($moduleFilters[$p_module]['fields'] as $meta){
306 if(!empty($_POST[$meta['name']]))
307 $_SESSION['reassignRecords']['filters'][$meta['name']] = $_POST[$meta['name']];
308 $is_custom = isset($meta['custom_table']) && $meta['custom_table'] == true;
309 if($is_custom && !$custom_added){
310 $q_tables .= "inner join {$object->table_name}_cstm on {$object->table_name}.id = {$object->table_name}_cstm.id_c ";
311 $custom_added = true;
313 $addcstm = ($is_custom ? "_cstm" : "");
314 switch($meta['type']){
317 $q_where .= " and {$object->table_name}{$addcstm}.{$meta['dbname']} = '{$_POST[$meta['name']]}' ";
320 if(empty($_POST[$meta['name']])){
325 foreach($_POST[$meta['name']] as $onevalue){
327 $empty_check .= " OR {$object->table_name}{$addcstm}.{$meta['dbname']} is null ";
328 $in_string .= "'$onevalue', ";
330 $in_string = substr($in_string, 0, count($in_string) - 3);
331 $q_where .= " and ({$object->table_name}{$addcstm}.{$meta['dbname']} in ($in_string) $empty_check)";
334 //echo "Skipping field {$meta['name']} since the type is not supported<BR>";
340 $query = "$q_select from $q_tables $q_where";
341 $countquery = "select count(*) AS count from $q_tables $q_where";
342 $updatequery = "$q_update $q_tables $q_set $q_where";
344 $_SESSION['reassignRecords']['fromuser'] = $_POST['fromuser'];
345 $_SESSION['reassignRecords']['touser'] = $_POST['touser'];
346 $_SESSION['reassignRecords']['fromusername'] = $fromusername;
347 $_SESSION['reassignRecords']['tousername'] = $tousername;
348 $_SESSION['reassignRecords']['modules'][$module]['query'] = $query;
349 $_SESSION['reassignRecords']['modules'][$module]['update'] = $updatequery;
351 $res = $GLOBALS['db']->query($countquery);
352 $row = $GLOBALS['db']->fetchByAssoc($res);
354 echo "{$row['count']} {$mod_strings_users['LBL_REASS_RECORDS_FROM']} {$app_list_strings['moduleList'][$p_module]} {$mod_strings_users['LBL_REASS_WILL_BE_UPDATED']}\n<BR>\n";
355 echo "<input type=checkbox name={$module}_workflow> {$mod_strings_users['LBL_REASS_WORK_NOTIF_AUDIT']}<BR>\n";
356 echo "</td></tr></table>\n";
359 echo "<BR><input type=button class=\"button\" value=\"{$mod_strings_users['LBL_REASS_BUTTON_GO_BACK']}\" onclick='document.location=\"index.php?module=Users&action=reassignUserRecords\"'>\n";
360 echo " <input type=submit class=\"button\" value=\"{$mod_strings_users['LBL_REASS_BUTTON_CONTINUE']}\">\n";
361 echo " <input type=button class=\"button\" value=\"{$mod_strings_users['LBL_REASS_BUTTON_RESTART']}\" onclick='document.location=\"index.php?module=Users&action=reassignUserRecords&clear=true\"'>\n";
366 //print_r($_SESSION['reassignRecords']);
367 ///////////////////// END STEP 2 - Confirm Selections /////////////////////////
369 /////////////////// BEGIN STEP 3 - Execute reassignment ///////////////////////
370 else if(isset($_GET['execute']) && $_GET['execute'] == true){
371 $fromuser = $_SESSION['reassignRecords']['fromuser'];
372 $touser = $_SESSION['reassignRecords']['touser'];
373 $fromusername = $_SESSION['reassignRecords']['fromusername'];
374 $tousername = $_SESSION['reassignRecords']['tousername'];
376 $beanListFlip = array_flip($_SESSION['reassignRecords']['assignedModuleListCache']);
378 foreach($_SESSION['reassignRecords']['modules'] as $module => $queries){
379 $p_module = $beanListFlip[$module];
381 if(isset($_POST[$module."_workflow"]) && $_POST[$module."_workflow"] = "on")
384 $query = $workflow ? $queries['query'] : $queries['update'];
386 echo "<h5>{$mod_strings_users['LBL_PROCESSING']} {$app_list_strings['moduleList'][$p_module]}</h5>";
388 $res = $GLOBALS['db']->query($query, true);
390 //echo "<i>Workflow and Notifications <b>".($workflow ? "enabled" : "disabled")."</b> for this module record reassignment</i>\n<BR>\n";
391 echo "<table border='0' cellspacing='0' cellpadding='0' class='detail view'>\n";
395 $affected_rows = $GLOBALS['db']->getAffectedRowCount();
396 echo "{$mod_strings_users['LBL_UPDATE_FINISH']}: $affected_rows {$mod_strings_users['LBL_AFFECTED']}<BR>\n";
399 $successarr = array();
402 require_once($beanFiles[$module]);
403 while($row = $GLOBALS['db']->fetchByAssoc($res)){
404 $bean = new $module();
405 if(empty($row['id'])){
408 $bean->retrieve($row['id']);
410 // So that we don't create new blank records.
411 if(!isset($bean->id)){
414 $bean->assigned_user_id = $touser;
417 $linkname = "record with id {$bean->id}";
418 if(!empty($bean->name)){
419 $linkname = $bean->name;
421 else if(!empty($bean->last_name)){
422 $linkname = $locale->getLocaleFormattedName($bean->first_name, $bean->last_name);
424 else if(!empty($bean->document_name)){
425 $linkname = $bean->document_name;
427 $successstr = "{$mod_strings_users['LBL_REASS_SUCCESS_ASSIGN']} {$bean->object_name} \"<i><a href=\"index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}\">$linkname</a></i>\" {$mod_strings_users['LBL_REASS_FROM']} $fromusername {$mod_strings_users['LBL_REASS_TO']} $tousername";
428 $successarr[] = $successstr;
431 $failarr[] = "{$mod_strings_users['LBL_REASS_FAILED_SAVE']} \"<i><a href=\"index.php?module={$bean->module_dir}&action=DetailView&record={$bean->id}\">$linkname</a></i>\".";
435 if(isset($_POST['verbose']) && $_POST['verbose'] == "on"){
436 echo "<h5>{$mod_strings_users['LBL_REASS_THE_FOLLOWING']} {$app_list_strings['moduleList'][$p_module]} {$mod_strings_users['LBL_REASS_HAVE_BEEN_UPDATED']}</h5>\n";
437 foreach($successarr as $ord){
440 if(empty($successarr))
441 echo "{$mod_strings_users['LBL_REASS_NONE']}\n<BR>\n";
443 echo "<h5>{$mod_strings_users['LBL_REASS_THE_FOLLOWING']} {$app_list_strings['moduleList'][$p_module]} {$mod_strings_users['LBL_REASS_CANNOT_PROCESS']}</h5>\n";
444 foreach($failarr as $failure){
445 echo $failure."\n<BR>\n";
448 echo "{$mod_strings_users['LBL_REASS_NONE']}\n<BR>\n";
451 echo "{$mod_strings_users['LBL_REASS_UPDATE_COMPLETE']}\n<BR>\n";
452 echo " ".count($successarr)." {$mod_strings_users['LBL_REASS_SUCCESSFUL']}\n<BR>\n";
453 echo " ".count($failarr)." {$mod_strings_users['LBL_REASS_FAILED']}\n";
457 echo "</td></tr></table>\n";
460 echo "<BR><input type=button class=\"button\" value=\"{$mod_strings_users['LBL_REASS_BUTTON_RETURN']}\" onclick='document.location=\"index.php?module=Users&action=reassignUserRecords\"'>\n";
462 /////////////////// END STEP 3 - Execute reassignment ///////////////////////
465 <script type="text/javascript" src="include/javascript/sugar_grp_overlib.js"></script>
466 <script type="text/javascript">
468 function clearCurrentRecords()
472 document.getElementById('fromuser').selectedIndex = 0;
473 document.getElementById('touser').selectedIndex = 0;
474 document.getElementById('modulemultiselect').selectedIndex = -1;
475 updateDivDisplay(document.getElementById('modulemultiselect'));
479 YAHOO.util.Connect.asyncRequest('POST', 'index.php?module=Users&action=clearreassignrecords&to_pdf=1', callback, null);
482 var allselected = [];
483 function updateDivDisplay(multiSelectObj){
484 for(var i = 0; i < multiSelectObj.options.length; i++){
485 if(multiSelectObj.options[i].selected != allselected[i]){
486 allselected[i] = multiSelectObj.options[i].selected;
489 theElement = document.getElementById('div_' + multiSelectObj.options[i].text);
490 if(theElement != null){
491 theElement.style.display = 'block';
495 theElement = document.getElementById('div_' + multiSelectObj.options[i].text);
496 if(theElement != null){
497 theElement.style.display = 'none';
504 if(!isset($_POST['fromuser']) && !isset($_GET['execute'])){
506 updateDivDisplay(document.getElementById('modulemultiselect'));