Ignore:
Timestamp:
Nov 14, 2009, 12:45:42 AM (14 years ago)
Author:
LucMorizur
Message:

[Event Cats] Continue duplication management

Location:
extensions/event_cats/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/event_cats/include/ec_conf.inc.php

    r4239 r4263  
    4040    => '10',              // automatically
    4141                          //
    42   'reg_display'           // the way the "register" link is displayed,
    43     => '0',               // together with the original $lang value for this
    44                           // label
     42  'duplic_gen'            // whether Generic accounts allow to be duplicated
     43    => '0',               //
     44                          //
     45  'duplic_type'           // the kind of account type (Contacts, Friends,
     46    => '0',               // Family) allowed to duplicate account
    4547                          //
    4648  'howto'                 // whether the small aknowledgement message has
     
    7880if (!$v) {
    7981  $ec_conf = array_values($ec_conf_default);
    80   change_ec_conf('activated', $u);
     82  change_ec_conf('activated', $u); // writes in the DB
    8183}
    8284// now :
  • extensions/event_cats/include/evntcats_main_funcs.inc.php

    r4247 r4263  
    3333
    3434include_once(EVNTCATS_PATH.'include/ec_conf.inc.php');
     35
     36if (!isset($ec_lists)) {
     37  $ec_lists               = array();
     38  $ec_lists['add_pages']  = array();
     39  $ec_lists['categories'] = array();
     40  $ec_lists['user_ids']   = array();
     41  $ec_lists['ec_table']   = array();
     42}
    3543
    3644// +-----------------------------------------------------------------------+
     
    163171
    164172/*
     173 * build_ec_addp()
     174 * builds $ec_lists['add_pages'].
     175 *
     176 * @param
     177 *   (no parameter)
     178 * @return
     179 *   (no return value)
     180 */
     181function build_ec_addp() {
     182  global $ec_lists, $ec_ap_ok;
     183  if ($ec_ap_ok) {
     184    $res = pwg_query("SELECT `id`, `title` FROM `".ADD_PAGES_TABLE."`");
     185    while ($r = mysql_fetch_assoc($res)) {
     186      $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id=';
     187      $a = explode($c ,$r['title']);
     188      $ec_lists['add_pages'][$r['id']] = $a[0];
     189    }
     190  }
     191}
     192
     193/*
    165194 * build_ec_categories($dsp)
    166195 * builds $ec_lists['categories'].
     
    183212
    184213/*
     214 * build_ec_userids()
     215 * builds $ec_lists['user_ids'].
     216 *
     217 * @param
     218 *   (no parameter)
     219 * @return
     220 *   (no return value)
     221 */
     222function build_ec_userids() {
     223  global $ec_lists, $conf;
     224  $ec_lists['user_ids'] = simple_hash_from_query("
     225    SELECT
     226      ".$conf['user_fields']['id']." AS id,
     227      ".$conf['user_fields']['username']." AS username
     228    FROM `".USERS_TABLE."`
     229    WHERE id > 2;",
     230    'id', 'username'
     231  );
     232}
     233
     234/*
    185235 * build_ec_table()
    186236 * builds a table showing the content of the <pwg>_event_cats database table,
     
    195245function build_ec_table() {
    196246  global $ec_lists, $ec_ap_ok;
    197  
    198   $ec_lists['ec_table'] = array();
    199247 
    200248  $q = pwg_query("
     
    276324  global $ec_ap_ok, $template, $ec_lists, $conf;
    277325 
    278   $ec_lists = array();
    279 
    280326  // Construction of $ec_lists['add_pages'] array var
    281   $ec_lists['add_pages'] = array();
    282   if ($ec_ap_ok) {
    283     $res = pwg_query("SELECT `id`, `title` FROM `".ADD_PAGES_TABLE."`");
    284     while ($r = mysql_fetch_assoc($res)) {
    285       $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id=';
    286       $a = explode($c ,$r['title']);
    287       $ec_lists['add_pages'][$r['id']] = $a[0];
    288     }
    289   }
     327  build_ec_addp();
    290328 
    291329  // Construction of $ec_lists['categories'] array var
     
    293331 
    294332  // Construction of $ec_lists['user_ids'] array var
    295   $ec_lists['user_ids'] = array();
    296   $q = pwg_query("
    297     SELECT
    298       ".$conf['user_fields']['id']." AS id,
    299       ".$conf['user_fields']['username']." AS username
    300     FROM `".USERS_TABLE."`
    301     WHERE id > 2
    302   ");
    303   while ($r = mysql_fetch_assoc($q))
    304    $ec_lists['user_ids'][$r['id']] = $r['username'];
     333  build_ec_userids();
    305334 
    306335  // Construction of $ec_lists['ec_table'] array var
     
    308337}
    309338
     339// +-----------------------------------------------------------------------+
     340// | Duplication analysis functions                                        |
     341// +-----------------------------------------------------------------------+
     342
     343/*
     344 * build_dup_arrays($append_tpl = false)
     345 * builds arrays telling which accounts are allowed to display a duplicate
     346 * account link. Returns an array of all the user ids allowed to duplicate.
     347 *
     348 * @param
     349 *   $append_tpl : tells if $template must be appended with built arrays
     350 * @return
     351 *   (no return value)
     352 */
     353function build_dup_arrays($append_tpl = false) {
     354  global $template, $ec_lists, $conf, $ec_debug;
     355 
     356  if (count($ec_lists['user_ids']) == 0) build_ec_userids();
     357 
     358  // A lot of below code has simply been copied from file cat_perm.php .
     359  // Many thanks to people who wrote it !
     360 
     361  $groups = simple_hash_from_query("
     362    SELECT `id`, `name`
     363      FROM `".GROUPS_TABLE."`
     364      ORDER BY `name` ASC;
     365    ",
     366    'id', 'name'
     367  );
     368 
     369  // groups granted to duplication
     370  $group_granted_ids = order_by_name(
     371    array_from_query("
     372      SELECT `arg2`
     373        FROM ".EVNTCATS_TABLE."
     374        WHERE `code` IS NULL
     375          AND `arg1` = '1'
     376          AND `arg2` IS NOT NULL
     377      ;",
     378      'arg2'
     379    ),
     380    $groups
     381  );
     382 
     383  // users directly granted to duplication
     384  $users_granted_direct_ids = order_by_name(
     385    array_from_query("
     386      SELECT `arg2`
     387        FROM ".EVNTCATS_TABLE."
     388        WHERE `code` IS NULL
     389          AND `arg1` = '2'
     390          AND `arg2` IS NOT NULL
     391      ;",
     392      'arg2'
     393    ),
     394    $ec_lists['user_ids']
     395  );
     396 
     397  // Calculate users granted to duplication thanks to belonging to a
     398  // certain group (in groups, level (friends, family, contacts),
     399  // or user status (generic))
     400  $user_granted_ids = array();
     401
     402  $users_granted_thks_gen_ids = array();
     403  if (read_ec_conf('duplic_gen') != '0') {
     404    $users_granted_thks_gen_ids = order_by_name(
     405      array_diff(
     406        array_from_query("
     407          SELECT `user_id`
     408          FROM `".USER_INFOS_TABLE."`
     409          WHERE `status` = 'generic';",
     410          'user_id'
     411        ),
     412        $users_granted_direct_ids
     413      ),
     414      $ec_lists['user_ids']
     415    );
     416    foreach ($users_granted_thks_gen_ids as $user_id)
     417     $user_granted_ids[$user_id] = l10n('user_status_generic');
     418  } global $ec_debug; $ec_debug['1'] = $user_granted_ids;
     419
     420  $users_granted_thks_types_ids = array();
     421  $c = intval(read_ec_conf('duplic_type'));
     422  if ($c != 0) {
     423    $t = array();
     424    if (($c & 1) != 0) $t[] = '1';
     425    if (($c & 2) != 0) $t[] = '2';
     426    if (($c & 4) != 0) $t[] = '4';
     427    if (count($t) > 0) {
     428      $types = array(
     429        '1' => l10n('Level 1'),
     430        '2' => l10n('Level 2'),
     431        '4' => l10n('Level 4'),
     432      );
     433      $user_granted_from_type = array();
     434      $result = pwg_query("
     435        SELECT `user_id`, `level`
     436        FROM `".USER_INFOS_TABLE."`
     437        WHERE `level` IN (".implode(',', $t).");
     438      ");
     439      while ($row = mysql_fetch_array($result)) {
     440        if (!isset($user_granted_from_type[$row['level']])) {
     441          $user_granted_from_type[$row['level']] = array();
     442        }
     443        $user_granted_from_type[$row['level']][] = $row['user_id'];
     444      }
     445      $user_granted_by_type_ids = array();
     446      foreach ($user_granted_from_type as $type_users)
     447       $user_granted_by_type_ids = array_merge(
     448        $user_granted_by_type_ids,
     449        $type_users
     450       );
     451      $users_granted_thks_types_ids = order_by_name(
     452        array_diff(
     453          array_unique($user_granted_by_type_ids),
     454          $users_granted_direct_ids
     455        ),
     456        $ec_lists['user_ids']
     457      );
     458      foreach ($users_granted_thks_types_ids as $user_id)
     459       foreach ($user_granted_from_type as $type_id => $type_users) {
     460        if (in_array($user_id, $type_users)) {
     461          $user_granted_ids[$user_id]= $types[$type_id];
     462          break;
     463        }
     464       }
     465    }
     466  } $ec_debug['2'] = $user_granted_ids;
     467
     468  $users_granted_thks_groups_ids = array();
     469  if (count($group_granted_ids) > 0) {
     470    $granted_groups = array();
     471
     472    $result = pwg_query("
     473      SELECT `user_id`, `group_id`
     474      FROM `".USER_GROUP_TABLE."`
     475      WHERE `group_id` IN (".implode(',', $group_granted_ids).");
     476    ");
     477    while ($row = mysql_fetch_array($result)) {
     478      if (!isset($granted_groups[$row['group_id']])) {
     479        $granted_groups[$row['group_id']] = array();
     480      }
     481      $granted_groups[$row['group_id']][] = $row['user_id'];
     482    }
     483   
     484    $user_granted_by_group_ids = array();
     485   
     486    foreach ($granted_groups as $group_users)
     487     $user_granted_by_group_ids = array_merge(
     488      $user_granted_by_group_ids,
     489      $group_users
     490     );
     491    $users_granted_thks_groups_ids = order_by_name(
     492      array_diff(
     493        array_unique($user_granted_by_group_ids),
     494        $users_granted_direct_ids
     495      ),
     496      $ec_lists['user_ids']
     497    );
     498    foreach ($users_granted_thks_groups_ids as $user_id)
     499     foreach ($granted_groups as $group_id => $group_users)
     500      if (in_array($user_id, $group_users)) {
     501        $user_granted_ids[$user_id]= $groups[$group_id];
     502        break;
     503      }
     504   
     505  } $ec_debug['3'] = $user_granted_ids;
     506
     507  if ($append_tpl) {
     508    $user_denied_ids = order_by_name(
     509      array_diff(
     510        array_keys($ec_lists['user_ids']),
     511        $users_granted_thks_gen_ids,
     512        $users_granted_thks_types_ids,
     513        $users_granted_thks_groups_ids,
     514        $users_granted_direct_ids
     515      ),
     516      $ec_lists['user_ids']
     517    );
     518   
     519    foreach ($user_granted_ids as $u => $g) $template->append(
     520      'user_granted_indirects',
     521      array(
     522        'USER'  => $ec_lists['user_ids'][$u],
     523        'GROUP' => $g
     524      )
     525    );
     526   
     527    $ec_debug['4'] = $user_granted_ids;
     528    $template->assign('all_groups', $groups);
     529    $template->assign('group_granted_ids', $group_granted_ids);
     530    $template->assign('group_denied_ids',order_by_name(
     531      array_diff(array_keys($groups), $group_granted_ids), $groups
     532    ));
     533    $template->assign('all_users', $ec_lists['user_ids']);
     534    $template->assign('users_granted_direct_ids', $users_granted_direct_ids);
     535    $template->assign('user_denied_ids', $user_denied_ids);
     536  }
     537   $user_granted_ids = array_merge(
     538    $user_granted_ids,
     539    $users_granted_direct_ids
     540   );
     541  return $user_granted_ids;
     542}
     543
     544/*
     545 * dup_allowed($user_id)
     546 * returns true if the user_id is allowed to display a duplicate link
     547 *
     548 * @param
     549 *   $user_id : the user_id
     550 * @return
     551 *   true if the user_id is allowed to display a duplicate link
     552 */
     553function dup_allowed($user_id) {
     554  return true;
     555}
     556
    310557?>
Note: See TracChangeset for help on using the changeset viewer.