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

Last change on this file since 9517 was 9517, checked in by plg, 13 years ago

force loading admin language file (which contains all strings for the upload form)

File size: 5.0 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');
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('init', 'community_load_language');
89function community_load_language()
90{
91  if (!defined('IN_ADMIN') or !IN_ADMIN)
92  {
93    load_language('admin.lang');
94  }
95}
96
97
98add_event_handler('loc_end_section_init', 'community_section_init');
99function community_section_init()
100{
101  global $tokens, $page;
102 
103  if ($tokens[0] == 'add_photos')
104  {
105    $page['section'] = 'add_photos';
106  }
107}
108
109add_event_handler('loc_end_index', 'community_index');
110function community_index()
111{
112  global $page;
113 
114  if (isset($page['section']) and $page['section'] == 'add_photos')
115  {
116    include(COMMUNITY_PATH.'add_photos.php');
117  }
118}
119
120add_event_handler('blockmanager_apply' , 'community_gallery_menu');
121function community_gallery_menu($menu_ref_arr)
122{
123  global $conf, $user;
124
125  // conditional : depending on community permissions, display the "Add
126  // photos" link in the gallery menu
127  $user_permissions = community_get_user_permissions($user['id']);
128 
129  if (!$user_permissions['upload_whole_gallery'] and count($user_permissions['upload_categories']) == 0)
130  {
131    return;
132  }
133
134  $menu = & $menu_ref_arr[0];
135
136  if (($block = $menu->get_block('mbMenu')) != null )
137  {
138    load_language('plugin.lang', COMMUNITY_PATH);
139
140    array_splice(
141      $block->data,
142      count($block->data),
143      0,
144      array(
145        '' => array(
146          'URL' => make_index_url(array('section' => 'add_photos')),
147          'TITLE' => l10n('Upload your own photos'),
148          'NAME' => l10n('Upload Photos')
149          )
150        )
151      );
152  }
153}
154
155
156add_event_handler('ws_invoke_allowed', 'community_switch_user_to_admin', EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
157
158function community_switch_user_to_admin($res, $methodName, $params)
159{
160  global $user;
161
162  $methods_of_permission_level[1] = array(
163    'pwg.categories.getList',
164    'pwg.tags.getAdminList',
165    'pwg.tags.add',
166    'pwg.images.exist',
167    'pwg.images.add',
168    'pwg.images.setInfo',
169    'pwg.images.addChunk',
170    'pwg.images.checkUpload',
171    );
172
173  // permission_level 2 has all methods of level 1 + others
174  $methods_of_permission_level[2] = array_merge(
175    $methods_of_permission_level[1],
176    array(
177      'pwg.categories.add',
178      'pwg.categories.setInfo',
179      )
180    );
181   
182  $query = '
183SELECT
184    permission_level
185  FROM '.COMMUNITY_TABLE.'
186  WHERE user_id = '.$user['id'].'
187;';
188  $result = pwg_query($query);
189  if (1 == mysql_num_rows($result))
190  {
191    list($permission_level) = mysql_fetch_row($result);
192
193    if (in_array($methodName, $methods_of_permission_level[$permission_level]))
194    {
195      $user['status'] = 'admin';
196    }
197  }
198
199  return $res;
200}
201
202add_event_handler('delete_user', 'community_delete_user');
203function community_delete_user($user_id)
204{
205  $query = '
206DELETE
207  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
208  WHERE user_id = '.$user_id.'
209;';
210  pwg_query($query);
211
212  community_reject_user_pendings($user_id);
213}
214?>
Note: See TracBrowser for help on using the repository browser.