source: extensions/community/main.inc.php @ 9926

Last change on this file since 9926 was 9845, checked in by plg, 14 years ago

bug fixed: when deleting a category, automatically delete related community permissions

File size: 5.5 KB
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');
23
24/* Plugin admin */
25add_event_handler('get_admin_plugin_menu_links', 'community_admin_menu');
26function community_admin_menu($menu)
27{
28  global $page;
29 
30  $query = '
31SELECT
32    COUNT(*)
33  FROM '.COMMUNITY_PENDINGS_TABLE.'
34  WHERE state = \'moderation_pending\'
35;';
36  $result = pwg_query($query);
37  list($page['community_nb_pendings']) = pwg_db_fetch_row($result);
38
39  $name = 'Community';
40  if ($page['community_nb_pendings'] > 0)
41  {
42    $style = 'background-color:#666;';
43    $style.= 'color:white;';
44    $style.= 'padding:1px 5px;';
45    $style.= '-moz-border-radius:10px;';
46    $style.= '-webkit-border-radius:10px;';
47    $style.= '-border-radius:10px;';
48    $style.= 'margin-left:5px;';
49   
50    $name.= '<span style="'.$style.'">'.$page['community_nb_pendings'].'</span>';
51
52    if (defined('IN_ADMIN') and IN_ADMIN and $page['page'] == 'intro')
53    {
54      global $template;
55     
56      $template->set_prefilter('intro', 'community_pendings_on_intro');
57      $template->assign(
58        array(
59          'COMMUNITY_PENDINGS' => sprintf(
60            '<a href="%s">'.l10n('%u pending photos').'</a>',
61            get_root_url().'admin.php?page=plugin-community-pendings',
62            $page['community_nb_pendings']
63            ),
64          )
65        );
66    }
67  }
68
69  array_push(
70    $menu,
71    array(
72      'NAME' => $name,
73      'URL'  => get_root_url().'admin.php?page=plugin-community'
74      )
75    );
76
77  return $menu;
78}
79
80function community_pendings_on_intro($content, &$smarty)
81{
82  $pattern = '#<li>\s*{\$DB_ELEMENTS\}#ms';
83  $replacement = '<li>{$COMMUNITY_PENDINGS}</li><li>{$DB_ELEMENTS}';
84  return preg_replace($pattern, $replacement, $content);
85}
86
87add_event_handler('init', 'community_load_language');
88function community_load_language()
89{
90  if (!defined('IN_ADMIN') or !IN_ADMIN)
91  {
92    load_language('admin.lang');
93  }
94 
95  load_language('plugin.lang', COMMUNITY_PATH);
96}
97
98
99add_event_handler('loc_end_section_init', 'community_section_init');
100function community_section_init()
101{
102  global $tokens, $page;
103 
104  if ($tokens[0] == 'add_photos')
105  {
106    $page['section'] = 'add_photos';
107  }
108}
109
110add_event_handler('loc_end_index', 'community_index');
111function community_index()
112{
113  global $page;
114 
115  if (isset($page['section']) and $page['section'] == 'add_photos')
116  {
117    include(COMMUNITY_PATH.'add_photos.php');
118  }
119}
120
121add_event_handler('blockmanager_apply' , 'community_gallery_menu');
122function community_gallery_menu($menu_ref_arr)
123{
124  global $conf, $user;
125
126  // conditional : depending on community permissions, display the "Add
127  // photos" link in the gallery menu
128  $user_permissions = community_get_user_permissions($user['id']);
129
130  if (count($user_permissions['upload_categories']) == 0 and !$user_permissions ['create_whole_gallery'])
131  {
132    return;
133  }
134
135  $menu = & $menu_ref_arr[0];
136
137  if (($block = $menu->get_block('mbMenu')) != null )
138  {
139    load_language('plugin.lang', COMMUNITY_PATH);
140
141    array_splice(
142      $block->data,
143      count($block->data),
144      0,
145      array(
146        '' => array(
147          'URL' => make_index_url(array('section' => 'add_photos')),
148          'TITLE' => l10n('Upload your own photos'),
149          'NAME' => l10n('Upload Photos')
150          )
151        )
152      );
153  }
154}
155
156
157add_event_handler('ws_invoke_allowed', 'community_switch_user_to_admin', EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
158
159function community_switch_user_to_admin($res, $methodName, $params)
160{
161  global $user;
162
163  $methods_of_permission_level[1] = array(
164    'pwg.categories.getList',
165    'pwg.tags.getAdminList',
166    'pwg.tags.add',
167    'pwg.images.exist',
168    'pwg.images.add',
169    'pwg.images.setInfo',
170    'pwg.images.addChunk',
171    'pwg.images.checkUpload',
172    );
173
174  // permission_level 2 has all methods of level 1 + others
175  $methods_of_permission_level[2] = array_merge(
176    $methods_of_permission_level[1],
177    array(
178      'pwg.categories.add',
179      'pwg.categories.setInfo',
180      )
181    );
182   
183  $query = '
184SELECT
185    permission_level
186  FROM '.COMMUNITY_TABLE.'
187  WHERE user_id = '.$user['id'].'
188;';
189  $result = pwg_query($query);
190  if (1 == mysql_num_rows($result))
191  {
192    list($permission_level) = mysql_fetch_row($result);
193
194    if (in_array($methodName, $methods_of_permission_level[$permission_level]))
195    {
196      $user['status'] = 'admin';
197    }
198  }
199
200  return $res;
201}
202
203add_event_handler('delete_user', 'community_delete_user');
204function community_delete_user($user_id)
205{
206  $query = '
207DELETE
208  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
209  WHERE user_id = '.$user_id.'
210;';
211  pwg_query($query);
212
213  community_reject_user_pendings($user_id);
214}
215
216add_event_handler('delete_categories', 'community_delete_category');
217function community_delete_category($category_ids)
218{
219  // $category_ids includes all the sub-category ids
220  $query = '
221DELETE
222  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
223  WHERE category_id IN ('.implode(',', $category_ids).')
224;';
225  pwg_query($query);
226 
227  community_update_cache_key();
228}
229
230add_event_handler('invalidate_user_cache', 'community_refresh_cache_update_time');
231function community_refresh_cache_update_time()
232{
233  community_update_cache_key();
234}
235?>
Note: See TracBrowser for help on using the repository browser.