source: extensions/AddUsersNotes/main.inc.php @ 27697

Last change on this file since 27697 was 27697, checked in by plg, 10 years ago

compatibility with Piwigo 2.6 new user manager

relies on a new trigger ws_users_getList (for Piwigo 2.6.2)

Replace table piwigo_user_notes by column piwigo_user_infos.usernotes (simpler
coding for the plugin and automatic management of column in user manager table)

File size: 3.5 KB
Line 
1<?php
2/*
3Plugin Name: Add Users Notes
4Version: auto
5Description: Adds admin notes to users profiles
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=580
7Author: ddtddt
8Author URI:
9*/
10
11if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
12
13global $prefixeTable;
14
15// +-----------------------------------------------------------------------+
16// | Define plugin constants                                               |
17// +-----------------------------------------------------------------------+
18
19define('USERNOTES_ID', basename(dirname(__FILE__)));
20define('USERNOTES_PATH', PHPWG_PLUGINS_PATH.USERNOTES_ID.'/');
21define('USERNOTES_VERSION', 'auto');
22
23// init the plugin
24add_event_handler('init', 'usernotes_init');
25
26/**
27 * plugin initialization
28 *   - check for upgrades
29 *   - load language
30 */
31function usernotes_init()
32{
33  // apply upgrade if needed
34  include_once(USERNOTES_PATH.'maintain.inc.php');
35  $maintain = new AddUsersNotes_maintain(USERNOTES_ID);
36  $maintain->autoUpdate(USERNOTES_VERSION, 'install');
37 
38  // load plugin language file
39  load_language('plugin.lang', USERNOTES_PATH);
40}
41
42add_event_handler('loc_begin_admin_page', 'usernotes_add_column');
43function usernotes_add_column()
44{
45  global $template;
46 
47        $template->set_prefilter('user_list', 'usernotes_add_column_prefilter');
48}
49
50function usernotes_add_column_prefilter($content, &$smarty)
51{
52  // add the "Notes" column in the user table
53  $search = '<th>{\'registration date\'|@translate}</th>';
54  $content = str_replace($search, $search.'<th>{\'Notes\'|@translate}</th>', $content);
55
56  // add the "Notes" field in user profile form
57  $search = '#</div>\s*<div class="userPropertiesSet userPrefs">#ms';
58  $replace = '<div class="userProperty"><strong>{\'Notes\'|translate}</strong><br><input type="text" name="usernotes" value="<%- user.usernotes %>" style="width:338px;"></div></div><div class="userPropertiesSet userPrefs">';
59  $content = preg_replace($search, $replace, $content);
60
61  return $content;
62}
63
64add_event_handler('user_list_columns', 'usernotes_user_list_columns', EVENT_HANDLER_PRIORITY_NEUTRAL, 1);
65function usernotes_user_list_columns($aColumns)
66{
67  $aColumns[] = 'usernotes';
68 
69  return $aColumns;
70}
71
72add_event_handler('ws_invoke_allowed', 'usernotes_ws_users_setInfo', EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
73function usernotes_ws_users_setInfo($res, $methodName, $params)
74{
75  if ($methodName != 'pwg.users.setInfo')
76  {
77    return $res;
78  }
79
80  if (!isset($_POST['usernotes']))
81  {
82    return $res;
83  }
84 
85  if (count($params['user_id']) == 0)
86  {
87    return $res;
88  }
89 
90  $updates = array();
91
92  foreach ($params['user_id'] as $user_id)
93  {
94    $updates[] = array(
95      'user_id' => $user_id,
96      'usernotes' => $_POST['usernotes'],
97      );
98  }
99 
100  if (count($updates) > 0)
101  {
102    mass_updates(
103      USER_INFOS_TABLE,
104      array(
105        'primary' => array('user_id'),
106        'update'  => array('usernotes')
107        ),
108      $updates
109      );
110  }
111 
112  return $res;
113}
114
115add_event_handler('ws_users_getList', 'usernotes_ws_users_getList', EVENT_HANDLER_PRIORITY_NEUTRAL, 1);
116function usernotes_ws_users_getList($users)
117{
118  $user_ids = array();
119  foreach ($users as $user_id => $user)
120  {
121    $user_ids[] = $user_id;
122  }
123
124  if (count($user_ids) == 0)
125  {
126    return $users;
127  }
128 
129  $query = '
130SELECT
131    user_id,
132    usernotes
133  FROM '.USER_INFOS_TABLE.'
134  WHERE user_id IN ('.implode(',', $user_ids).')
135;';
136  $result = pwg_query($query);
137  while ($row = pwg_db_fetch_assoc($result))
138  {
139    $users[$row['user_id']]['usernotes'] = $row['usernotes'];
140  }
141
142  return $users;
143}
144?>
Note: See TracBrowser for help on using the repository browser.