root/extensions/community/main.inc.php @ 9516

Revision 9516, 4.9 KB (checked in by plg, 2 years ago)

display the number of pending photos outside the dedicated admin screen (on the admin homepage, in the admin menubar, in the tab name)

Line 
1<?php
2/*
3Plugin Name: Community
4Version: auto
5Description: Non admin users can add photos
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=303
7Author: plg
8Author URI: http://piwigo.wordpress.com
9*/
10
11if (!defined('PHPWG_ROOT_PATH'))
12{
13  die('Hacking attempt!');
14}
15
16define('COMMUNITY_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
17
18global $prefixeTable;
19define('COMMUNITY_PERMISSIONS_TABLE', $prefixeTable.'community_permissions');
20define('COMMUNITY_PENDINGS_TABLE', $prefixeTable.'community_pendings');
21
22include_once(COMMUNITY_PATH.'include/functions_community.inc.php');
23load_language('plugin.lang', COMMUNITY_PATH);
24
25/* Plugin admin */
26add_event_handler('get_admin_plugin_menu_links', 'community_admin_menu');
27function community_admin_menu($menu)
28{
29  global $page;
30 
31  $query = '
32SELECT
33    COUNT(*)
34  FROM '.COMMUNITY_PENDINGS_TABLE.'
35  WHERE state = \'moderation_pending\'
36;';
37  $result = pwg_query($query);
38  list($page['community_nb_pendings']) = pwg_db_fetch_row($result);
39
40  $name = 'Community';
41  if ($page['community_nb_pendings'] > 0)
42  {
43    $style = 'background-color:#666;';
44    $style.= 'color:white;';
45    $style.= 'padding:1px 5px;';
46    $style.= '-moz-border-radius:10px;';
47    $style.= '-webkit-border-radius:10px;';
48    $style.= '-border-radius:10px;';
49    $style.= 'margin-left:5px;';
50   
51    $name.= '<span style="'.$style.'">'.$page['community_nb_pendings'].'</span>';
52
53    if (defined('IN_ADMIN') and IN_ADMIN and $page['page'] == 'intro')
54    {
55      global $template;
56     
57      $template->set_prefilter('intro', 'community_pendings_on_intro');
58      $template->assign(
59        array(
60          'COMMUNITY_PENDINGS' => sprintf(
61            '<a href="%s">'.l10n('%u pending photos').'</a>',
62            get_root_url().'admin.php?page=plugin-community-pendings',
63            $page['community_nb_pendings']
64            ),
65          )
66        );
67    }
68  }
69
70  array_push(
71    $menu,
72    array(
73      'NAME' => $name,
74      'URL'  => get_root_url().'admin.php?page=plugin-community'
75      )
76    );
77
78  return $menu;
79}
80
81function community_pendings_on_intro($content, &$smarty)
82{
83  $pattern = '#<li>\s*{\$DB_ELEMENTS\}#ms';
84  $replacement = '<li>{$COMMUNITY_PENDINGS}</li><li>{$DB_ELEMENTS}';
85  return preg_replace($pattern, $replacement, $content);
86}
87
88add_event_handler('loc_end_section_init', 'community_section_init');
89function community_section_init()
90{
91  global $tokens, $page;
92 
93  if ($tokens[0] == 'add_photos')
94  {
95    $page['section'] = 'add_photos';
96  }
97}
98
99add_event_handler('loc_end_index', 'community_index');
100function community_index()
101{
102  global $page;
103 
104  if (isset($page['section']) and $page['section'] == 'add_photos')
105  {
106    include(COMMUNITY_PATH.'add_photos.php');
107  }
108}
109
110add_event_handler('blockmanager_apply' , 'community_gallery_menu');
111function community_gallery_menu($menu_ref_arr)
112{
113  global $conf, $user;
114
115  // conditional : depending on community permissions, display the "Add
116  // photos" link in the gallery menu
117  $user_permissions = community_get_user_permissions($user['id']);
118 
119  if (!$user_permissions['upload_whole_gallery'] and count($user_permissions['upload_categories']) == 0)
120  {
121    return;
122  }
123
124  $menu = & $menu_ref_arr[0];
125
126  if (($block = $menu->get_block('mbMenu')) != null )
127  {
128    load_language('plugin.lang', COMMUNITY_PATH);
129
130    array_splice(
131      $block->data,
132      count($block->data),
133      0,
134      array(
135        '' => array(
136          'URL' => make_index_url(array('section' => 'add_photos')),
137          'TITLE' => l10n('Upload your own photos'),
138          'NAME' => l10n('Upload Photos')
139          )
140        )
141      );
142  }
143}
144
145
146add_event_handler('ws_invoke_allowed', 'community_switch_user_to_admin', EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
147
148function community_switch_user_to_admin($res, $methodName, $params)
149{
150  global $user;
151
152  $methods_of_permission_level[1] = array(
153    'pwg.categories.getList',
154    'pwg.tags.getAdminList',
155    'pwg.tags.add',
156    'pwg.images.exist',
157    'pwg.images.add',
158    'pwg.images.setInfo',
159    'pwg.images.addChunk',
160    'pwg.images.checkUpload',
161    );
162
163  // permission_level 2 has all methods of level 1 + others
164  $methods_of_permission_level[2] = array_merge(
165    $methods_of_permission_level[1],
166    array(
167      'pwg.categories.add',
168      'pwg.categories.setInfo',
169      )
170    );
171   
172  $query = '
173SELECT
174    permission_level
175  FROM '.COMMUNITY_TABLE.'
176  WHERE user_id = '.$user['id'].'
177;';
178  $result = pwg_query($query);
179  if (1 == mysql_num_rows($result))
180  {
181    list($permission_level) = mysql_fetch_row($result);
182
183    if (in_array($methodName, $methods_of_permission_level[$permission_level]))
184    {
185      $user['status'] = 'admin';
186    }
187  }
188
189  return $res;
190}
191
192add_event_handler('delete_user', 'community_delete_user');
193function community_delete_user($user_id)
194{
195  $query = '
196DELETE
197  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
198  WHERE user_id = '.$user_id.'
199;';
200  pwg_query($query);
201
202  community_reject_user_pendings($user_id);
203}
204?>
Note: See TracBrowser for help on using the browser.