Changeset 25237


Ignore:
Timestamp:
Oct 30, 2013, 1:54:46 PM (7 years ago)
Author:
plg
Message:

feature 1668, user manager redesign: ability to add a new user (call to pwg.users.add through AJAX)

Move the "send connection settings" code to function register_user (avoid code duplication).

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/add_core_tabs.inc.php

    r25084 r25237  
    6161    case 'comments':
    6262      $sheets[''] = array('caption' => l10n('User comments'), 'url' => '');
     63      break;
     64
     65    case 'users':
     66      $sheets[''] = array('caption' => '<span class="icon-users"> </span>'.l10n('User list'), 'url' => '');
    6367      break;
    6468     
  • trunk/admin/themes/default/template/user_list.tpl

    r25198 r25237  
    99var selectedMessage_all = "{'All %d photos are selected'|@translate}";
    1010var applyOnDetails_pattern = "{'on the %d selected users'|@translate}";
     11var newUser_pattern = "&#x2714; {'User %s added'|translate}";
    1112var missingConfirm = "{'You need to confirm deletion'|translate}";
     13var missingUsername = "{'Please, enter a login'|translate}";
    1214
    1315var allUsers = [{$all_users}];
     
    1719{footer_script}{literal}
    1820jQuery(document).ready(function() {
     21  /**
     22   * Add user
     23   */
     24  jQuery("#addUser").click(function() {
     25    jQuery("#addUserForm").toggle();
     26    jQuery("#showAddUser .infos").hide();
     27    jQuery("input[name=username]").focus();
     28    return false;
     29  });
     30
     31  jQuery("#addUserClose").click(function() {
     32    jQuery("#addUserForm").hide();
     33    return false;
     34  });
     35
     36  jQuery("#addUserForm").submit(function() {
     37    jQuery.ajax({
     38      url: "ws.php?format=json&method=pwg.users.add",
     39      type:"POST",
     40      data: jQuery(this).serialize(),
     41      beforeSend: function() {
     42        jQuery("#addUserForm .errors").hide();
     43
     44        if (jQuery("input[name=username]").val() == "") {
     45          jQuery("#addUserForm .errors").html('&#x2718; '+missingUsername).show();
     46          return false;
     47        }
     48
     49        jQuery("#addUserForm .loading").show();
     50      },
     51      success:function(data) {
     52        oTable.fnDraw();
     53        jQuery("#addUserForm .loading").hide();
     54
     55        var data = jQuery.parseJSON(data);
     56        if (data.stat == 'ok') {
     57          jQuery("#addUserForm input[type=text], #addUserForm input[type=password]").val("");
     58
     59          var new_user = data.result.users[0];
     60          allUsers.push(parseInt(new_user.id));
     61          jQuery("#showAddUser .infos").html(sprintf(newUser_pattern, new_user.username)).show();
     62          checkSelection();
     63
     64          jQuery("#addUserForm").hide();
     65        }
     66        else {
     67          jQuery("#addUserForm .errors").html('&#x2718; '+data.message).show();
     68        }
     69      },
     70      error:function(XMLHttpRequest, textStatus, errorThrows) {
     71        jQuery("#addUserForm .loading").hide();
     72      }
     73    });
     74
     75    return false;
     76  });
     77
     78  /**
     79   * Table with users
     80   */
     81
    1982  /* first column must be prefixed with the open/close icon */
    2083  var aoColumns = [
     
    265328table.dataTable {clear:right;padding-top:10px;}
    266329.bulkAction {margin-top:10px;}
    267 .actionButtons {margin-left:0;}
    268 #applyActionBlock .infos {background-image:none; padding:2px 5px; margin:0;border-radius:5px;}
     330#addUserForm p {margin-left:0;}
     331#applyActionBlock .actionButtons {margin-left:0;}
     332span.infos, span.errors {background-image:none; padding:2px 5px; margin:0;border-radius:5px;}
    269333</style>
    270334{/literal}
     
    274338</div>
    275339
    276 <form style="display:none" class="filter" method="post" name="add_user" action="{$F_ADD_ACTION}">
     340<p class="showCreateAlbum" id="showAddUser">
     341  <a href="#" id="addUser">{'Add a user'|translate}</a>
     342  <span class="infos" style="display:none"></span>
     343</p>
     344
     345<form id="addUserForm" style="display:none" method="post" name="add_user" action="{$F_ADD_ACTION}">
    277346  <fieldset>
    278347    <legend>{'Add a user'|@translate}</legend>
    279     <label>{'Username'|@translate} <input type="text" name="login" maxlength="50" size="20"></label>
    280     {if $Double_Password}
    281                 <label>{'Password'|@translate} <input type="password" name="password"></label>
    282                 <label>{'Confirm Password'|@translate} <input type="password" name="password_conf" id="password_conf"></label>
    283                 {else}
    284                 <label>{'Password'|@translate} <input type="text" name="password"></label>
    285                 {/if}
    286                 <label>{'Email address'|@translate} <input type="text" name="email"></label>
    287     <label>{'Send connection settings by email'|@translate} <input type="checkbox" name="send_password_by_mail" value="1" checked="checked"></label>
    288     <label>&nbsp; <input class="submit" type="submit" name="submit_add" value="{'Submit'|@translate}"></label>
     348
     349    <p>
     350      <strong>{'Username'|translate}</strong><br>
     351      <input type="text" name="username" maxlength="50" size="20">
     352    </p>
     353
     354    <p>
     355      <strong>{'Password'|translate}</strong><br>
     356      <input type="{if $Double_Password}password{else}text{/if}" name="password">
     357    </p>
     358   
     359{if $Double_Password}
     360    <p>
     361      <strong>{'Confirm Password'|@translate}</strong><br>
     362      <input type="password" name="password_confirm">
     363    </p>
     364{/if}
     365
     366    <p>
     367      <strong>{'Email address'|@translate}</strong><br>
     368      <input type="text" name="email">
     369    </p>
     370
     371    <p>
     372      <label><input type="checkbox" name="send_password_by_mail"> <strong>{'Send connection settings by email'|@translate}</strong></label>
     373    </p>
     374
     375    <p class="actionButtons">
     376      <input class="submit" name="submit_add" type="submit" value="{'Submit'|@translate}">
     377      <a href="#" id="addUserClose">{'Cancel'|@translate}</a>
     378      <span class="loading" style="display:none"><img src="themes/default/images/ajax-loader-small.gif"></span>
     379      <span class="errors" style="display:none"></span>
     380    </p>
    289381  </fieldset>
    290382</form>
  • trunk/admin/user_list.php

    r25194 r25237  
    2525 * Add users and manage users list
    2626 */
     27
     28// +-----------------------------------------------------------------------+
     29// | tabs                                                                  |
     30// +-----------------------------------------------------------------------+
     31
     32include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
     33
     34$my_base_url = get_root_url().'admin.php?page=';
     35
     36$tabsheet = new tabsheet();
     37$tabsheet->set_id('users');
     38$tabsheet->select('user_list');
     39$tabsheet->assign();
    2740
    2841// +-----------------------------------------------------------------------+
     
    7487    'users' => $users,
    7588    'all_users' => join(',', $user_ids),
     89    'Double_Password' => $conf['double_password_type_in_admin']
    7690    )
    7791  );
  • trunk/include/functions_user.inc.php

    r25116 r25237  
    126126 * @param string $password
    127127 * @param string $mail_adress
    128  * @param bool $with_notifications
     128 * @param bool $notify_admin
    129129 * @param &array $errors
     130 * @param bool $notify_user
    130131 * @return int|bool
    131132 */
    132 function register_user($login, $password, $mail_address,
    133   $with_notification = true, &$errors = array())
     133function register_user($login, $password, $mail_address, $notify_admin=true, &$errors = array(), $notify_user=false)
    134134{
    135135  global $conf;
     
    170170  }
    171171
    172   $errors = trigger_event('register_user_check',
    173               $errors,
    174               array(
    175                 'username'=>$login,
    176                 'password'=>$password,
    177                 'email'=>$mail_address,
    178               )
    179             );
     172  $errors = trigger_event(
     173    'register_user_check',
     174    $errors,
     175    array(
     176      'username'=>$login,
     177      'password'=>$password,
     178      'email'=>$mail_address,
     179      )
     180    );
    180181
    181182  // if no error until here, registration of the user
    182183  if (count($errors) == 0)
    183184  {
    184     $insert =
    185       array(
    186         $conf['user_fields']['username'] => pwg_db_real_escape_string($login),
    187         $conf['user_fields']['password'] => $conf['password_hash']($password),
    188         $conf['user_fields']['email'] => $mail_address
    189         );
     185    $insert = array(
     186      $conf['user_fields']['username'] => pwg_db_real_escape_string($login),
     187      $conf['user_fields']['password'] => $conf['password_hash']($password),
     188      $conf['user_fields']['email'] => $mail_address
     189      );
    190190
    191191    single_insert(USERS_TABLE, $insert);
     
    204204    while ($row = pwg_db_fetch_assoc($result))
    205205    {
    206         $inserts[] = array(
    207           'user_id' => $user_id,
    208           'group_id' => $row['id']
     206      $inserts[] = array(
     207        'user_id' => $user_id,
     208        'group_id' => $row['id']
    209209        );
    210210    }
     
    216216
    217217    $override = null;
    218     if ($with_notification and $conf['browser_language'])
    219     {
    220       if ( !get_browser_language($override['language']) )
     218    if ($notify_admin and $conf['browser_language'])
     219    {
     220      if (!get_browser_language($override['language']))
     221      {
    221222        $override=null;
     223      }
    222224    }
    223225    create_user_infos($user_id, $override);
    224226
    225     if ($with_notification and $conf['email_admin_on_new_user'])
     227    if ($notify_admin and $conf['email_admin_on_new_user'])
    226228    {
    227229      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
    228       $admin_url = get_absolute_root_url()
    229                    .'admin.php?page=user_list&username='.$login;
    230 
    231       $keyargs_content = array
    232       (
     230      $admin_url = get_absolute_root_url().'admin.php?page=user_list&username='.$login;
     231
     232      $keyargs_content = array(
    233233        get_l10n_args('User: %s', stripslashes($login)),
    234234        get_l10n_args('Email: %s', $_POST['mail_address']),
    235235        get_l10n_args('', ''),
    236236        get_l10n_args('Admin: %s', $admin_url)
    237       );
    238 
    239       pwg_mail_notification_admins
    240       (
     237        );
     238
     239      pwg_mail_notification_admins(
    241240        get_l10n_args('Registration of %s', stripslashes($login)),
    242241        $keyargs_content
    243       );
    244     }
    245 
    246     trigger_action('register_user',
     242        );
     243    }
     244
     245    if ($notify_user and email_check_format($mail_address))
     246    {
     247      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
     248           
     249      $keyargs_content = array(
     250        get_l10n_args('Hello %s,', $login),
     251        get_l10n_args('Thank you for registering at %s!', $conf['gallery_title']),
     252        get_l10n_args('', ''),
     253        get_l10n_args('Here are your connection settings', ''),
     254        get_l10n_args('Username: %s', $login),
     255        get_l10n_args('Password: %s', $password),
     256        get_l10n_args('Email: %s', $mail_address),
     257        get_l10n_args('', ''),
     258        get_l10n_args('If you think you\'ve received this email in error, please contact us at %s', get_webmaster_mail_address()),
     259        );
     260       
     261      pwg_mail(
     262        $mail_address,
     263        array(
     264          'subject' => '['.$conf['gallery_title'].'] '.l10n('Registration'),
     265          'content' => l10n_args($keyargs_content),
     266          'content_format' => 'text/plain',
     267          )
     268        );
     269    }
     270
     271    trigger_action(
     272      'register_user',
    247273      array(
    248274        'id'=>$user_id,
    249275        'username'=>$login,
    250276        'email'=>$mail_address,
    251        )
     277        )
    252278      );
    253279     
  • trunk/include/ws_functions.inc.php

    r25196 r25237  
    35623562function ws_users_add($params, &$service)
    35633563{
    3564   $user_id = register_user($params['username'], $params['password'], $params['email'], false, $errors);
     3564  global $conf;
     3565
     3566  if ($conf['double_password_type_in_admin'])
     3567  {
     3568    if ($params['password'] != $params['password_confirm'])
     3569    {
     3570      return new PwgError(WS_ERR_INVALID_PARAM, l10n('The passwords do not match'));
     3571    }
     3572  }
     3573
     3574  $user_id = register_user(
     3575    $params['username'],
     3576    $params['password'],
     3577    $params['email'],
     3578    false, // notify admin
     3579    $errors,
     3580    $params['send_password_by_mail']
     3581    );
    35653582 
    35663583  if (!$user_id)
  • trunk/register.php

    r25121 r25237  
    6161  }
    6262
    63   register_user($_POST['login'],
    64                 $_POST['password'],
    65                 $_POST['mail_address'],
    66                 true,
    67                 $page['errors']);
     63  register_user(
     64    $_POST['login'],
     65    $_POST['password'],
     66    $_POST['mail_address'],
     67    true,
     68    $page['errors'],
     69    isset($_POST['send_password_by_mail'])
     70    );
    6871
    6972  if (count($page['errors']) == 0)
    7073  {
    7174    // email notification
    72     if (isset($_POST['send_password_by_mail']) and isset($_POST['mail_address']))
     75    if (isset($_POST['send_password_by_mail']) and email_check_format($_POST['mail_address']))
    7376    {
    74       include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
    75            
    76       $keyargs_content = array(
    77         get_l10n_args('Hello %s,', $_POST['login']),
    78         get_l10n_args('Thank you for registering at %s!', $conf['gallery_title']),
    79         get_l10n_args('', ''),
    80         get_l10n_args('Here are your connection settings', ''),
    81         get_l10n_args('Username: %s', $_POST['login']),
    82         get_l10n_args('Password: %s', $_POST['password']),
    83         get_l10n_args('Email: %s', $_POST['mail_address']),
    84         get_l10n_args('', ''),
    85         get_l10n_args('If you think you\'ve received this email in error, please contact us at %s', get_webmaster_mail_address()),
    86         );
    87        
    88       pwg_mail(
    89         $_POST['mail_address'],
    90         array(
    91           'subject' => '['.$conf['gallery_title'].'] '.l10n('Registration'),
    92           'content' => l10n_args($keyargs_content),
    93           'content_format' => 'text/plain',
    94           )
    95         );
    96        
    9777      $_SESSION['page_infos'][] = l10n('Successfully registered, you will soon receive an email with your connection settings. Welcome!');
    9878    }
  • trunk/ws.php

    r25196 r25237  
    827827        'username' => array(),
    828828        'password' => array('default'=>null),
     829        'password_confirm' => array('flags'=>WS_PARAM_OPTIONAL),
    829830        'email' =>    array('default'=>null),
     831        'send_password_by_mail' => array('default'=>false, 'type'=>WS_TYPE_BOOL),
    830832        ),
    831833      '<b>Admin & POST only.</b> Registers a new user.',
Note: See TracChangeset for help on using the changeset viewer.