source: temp/test-xoops.ec-cube.net/html/include/notification_functions.php @ 405

Revision 405, 13.5 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: notification_functions.php,v 1.3 2005/09/04 20:46:09 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.xoops.org/ http://jp.xoops.org/  http://www.myweb.ne.jp/  //
29// Project: The XOOPS Project (http://www.xoops.org/)                        //
30// ------------------------------------------------------------------------- //
31
32// RMV-NOTIFY
33
34// FIXME: Do some caching, so we don't retrieve the same category / event
35// info many times.
36
37
38/**
39 * Determine if notification is enabled for the selected module.
40 *
41 * @param  string  $style      Subscription style: 'block' or 'inline'
42 * @param  int     $module_id  ID of the module  (default current module)
43 * @return bool
44 */
45function notificationEnabled ($style, $module_id=null)
46{
47    if (isset($GLOBALS['xoopsModuleConfig']['notification_enabled'])) {
48        $status = $GLOBALS['xoopsModuleConfig']['notification_enabled'];
49    } else {
50        if (!isset($module_id)) {
51            return false;
52        }
53        $module_handler =& xoops_gethandler('module');
54        $module =& $module_handler->get($module_id);
55        if (!empty($module) && $module->getVar('hasnotification') == 1) {
56            $config_handler =& xoops_gethandler('config');
57            $config = $config_handler->getConfigsByCat(0,$module_id);
58            $status = $config['notification_enabled'];
59        } else {
60            return false;
61        }
62    }
63    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
64    if (($style == 'block') && ($status == XOOPS_NOTIFICATION_ENABLEBLOCK || $status == XOOPS_NOTIFICATION_ENABLEBOTH)) {
65        return true;
66    }
67    if (($style == 'inline') && ($status == XOOPS_NOTIFICATION_ENABLEINLINE || $status == XOOPS_NOTIFICATION_ENABLEBOTH)) {
68        return true;
69    }
70    // if ($status != XOOPS_NOTIFICATION_DISABLE) {
71    //      return true;
72    // }
73    return false;
74}
75
76/**
77 * Get an associative array of info for a particular notification
78 * category in the selected module.  If no category is selected,
79 * return an array of info for all categories.
80 *
81 * @param  string  $name       Category name (default all categories)
82 * @param  int     $module_id  ID of the module (default current module)
83 * @return mixed
84 */
85function &notificationCategoryInfo ($category_name='', $module_id=null)
86{
87    if (!isset($module_id)) {
88        global $xoopsModule;
89        $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
90        $module =& $xoopsModule;
91    } else {
92        $module_handler =& xoops_gethandler('module');
93        $module =& $module_handler->get($module_id);
94    }
95    $not_config =& $module->getInfo('notification');
96    if (empty($category_name)) {
97        return $not_config['category'];
98    }
99    foreach ($not_config['category'] as $category) {
100        if ($category['name'] == $category_name) {
101            return $category;
102        }
103    }
104    return false;
105}
106
107/**
108 * Get associative array of info for the category to which comment events
109 * belong.
110 *
111 * @todo This could be more efficient... maybe specify in
112 *        $modversion['comments'] the notification category.
113 *       This would also serve as a way to enable notification
114 *        of comments, and also remove the restriction that
115 *        all notification categories must have unique item_name. (TODO)
116 *
117 * @param  int  $module_id  ID of the module (default current module)
118 * @return mixed            Associative array of category info
119 */
120function &notificationCommentCategoryInfo($module_id=null)
121{
122    $all_categories =& notificationCategoryInfo ('', $module_id);
123    if (empty($all_categories)) {
124        return false;
125    }
126    foreach ($all_categories as $category) {
127        $all_events =& notificationEvents ($category['name'], false, $module_id);
128        if (empty($all_events)) {
129            continue;
130        }
131        foreach ($all_events as $event) {
132            if ($event['name'] == 'comment') {
133                return $category;
134            }
135        }
136    }
137    return false;
138}
139
140// TODO: some way to include or exclude admin-only events...
141
142/**
143 * Get an array of info for all events (each event has associative array)
144 * in the selected category of the selected module.
145 *
146 * @param  string  $category_name  Category name
147 * @param  bool    $enabled_only   If true, return only enabled events
148 * @param  int     $module_id      ID of the module (default current module)
149 * @return mixed
150 */
151function &notificationEvents ($category_name, $enabled_only, $module_id=null)
152{
153    if (!isset($module_id)) {
154        global $xoopsModule;
155        $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
156        $module =& $xoopsModule;
157    } else {
158        $module_handler =& xoops_gethandler('module');
159        $module =& $module_handler->get($module_id);
160    }
161    $not_config =& $module->getInfo('notification');
162    $config_handler =& xoops_gethandler('config');
163    $mod_config = $config_handler->getConfigsByCat(0,$module_id);
164
165    $category =& notificationCategoryInfo($category_name, $module_id);
166
167    global $xoopsConfig;
168    $event_array = array();
169
170    $override_comment = false;
171    $override_commentsubmit = false;
172    $override_bookmark = false;
173   
174    foreach ($not_config['event'] as $event) {
175        if ($event['category'] == $category_name) {
176            $event['mail_template_dir'] = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/mail_template/';
177            if (!$enabled_only || notificationEventEnabled ($category, $event, $module)) {
178                $event_array[] = $event;
179            }
180            if ($event['name'] == 'comment') {
181                $override_comment = true;
182            }
183            if ($event['name'] == 'comment_submit') {
184                $override_commentsubmit = true;
185            }
186            if ($event['name'] == 'bookmark') {
187                $override_bookmark = true;
188            }
189        }
190    }
191
192    include_once XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/notification.php';
193
194    // Insert comment info if applicable
195   
196    if ($module->getVar('hascomments')) {
197        $com_config = $module->getInfo('comments');
198        if (!empty($category['item_name']) && $category['item_name'] == $com_config['itemName']) {
199            $mail_template_dir = XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/mail_template/';
200            include_once XOOPS_ROOT_PATH . '/include/comment_constants.php';
201            $config_handler =& xoops_gethandler('config');
202            $com_config = $config_handler->getConfigsByCat(0,$module_id);
203            if (!$enabled_only) {
204                $insert_comment = true;
205                $insert_submit = true;
206            } else {
207                $insert_comment = false;
208                $insert_submit = false;
209                switch($com_config['com_rule']) {
210                case XOOPS_COMMENT_APPROVENONE:
211                    // comments disabled, no comment events
212                    break;
213                case XOOPS_COMMENT_APPROVEALL:
214                    // all comments are automatically approved, no 'submit'
215                    if (!$override_comment) {
216                        $insert_comment = true;
217                    }
218                    break;
219                case XOOPS_COMMENT_APPROVEUSER:
220                case XOOPS_COMMENT_APPROVEADMIN:
221                    // comments first submitted, require later approval
222                    if (!$override_comment) {
223                        $insert_comment = true;
224                    }
225                    if (!$override_commentsubmit) {
226                        $insert_submit = true;
227                    }
228                    break;
229                }
230            }
231            if ($insert_comment) {
232                $event = array ('name'=>'comment', 'category'=>$category['name'], 'title'=>_NOT_COMMENT_NOTIFY, 'caption'=>_NOT_COMMENT_NOTIFYCAP, 'description'=>_NOT_COMMENT_NOTIFYDSC, 'mail_template_dir'=>$mail_template_dir, 'mail_template'=>'comment_notify', 'mail_subject'=>_NOT_COMMENT_NOTIFYSBJ);
233                if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
234                    $event_array[] = $event;
235                }
236            }
237            if ($insert_submit) {
238                $event = array ('name'=>'comment_submit', 'category'=>$category['name'], 'title'=>_NOT_COMMENTSUBMIT_NOTIFY, 'caption'=>_NOT_COMMENTSUBMIT_NOTIFYCAP, 'description'=>_NOT_COMMENTSUBMIT_NOTIFYDSC, 'mail_template_dir'=>$mail_template_dir, 'mail_template'=>'commentsubmit_notify', 'mail_subject'=>_NOT_COMMENTSUBMIT_NOTIFYSBJ, 'admin_only'=>1);
239                if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
240                    $event_array[] = $event;
241                }
242            }
243               
244
245        }
246    }
247
248    // Insert bookmark info if appropriate
249
250    if (!empty($category['allow_bookmark'])) {
251        if (!$override_bookmark) {
252            $event = array ('name'=>'bookmark', 'category'=>$category['name'], 'title'=>_NOT_BOOKMARK_NOTIFY, 'caption'=>_NOT_BOOKMARK_NOTIFYCAP, 'description'=>_NOT_BOOKMARK_NOTIFYDSC);
253            if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
254                $event_array[] = $event;
255            }
256        }   
257    }
258
259
260    return $event_array;
261   
262}
263
264/**
265 * Determine whether a particular notification event is enabled.
266 * Depends on module config options.
267 *
268 * @todo  Check that this works correctly for comment and other
269 *   events which depend on additional config options...
270 *
271 * @param  array  $category  Category info array
272 * @param  array  $event     Event info array
273 * @param  object $module    Module
274 * @return bool
275 **/
276function notificationEventEnabled (&$category, &$event, &$module)
277{
278    $config_handler =& xoops_gethandler('config');
279    $mod_config = $config_handler->getConfigsByCat(0,$module->getVar('mid'));
280
281    $option_name = notificationGenerateConfig ($category, $event, 'option_name');
282    if (in_array($option_name, $mod_config['notification_events'])) {
283        return true;
284    }
285    $notification_handler =& xoops_gethandler('notification');
286
287    return false;
288}
289
290
291/**
292 * Get associative array of info for the selected event in the selected
293 * category (for the selected module).
294 *
295 * @param  string  $category_name  Notification category
296 * @param  string  $event_name     Notification event
297 * @param  int     $module_id      ID of the module (default current module)
298 * @return mixed
299 */
300function &notificationEventInfo ($category_name, $event_name, $module_id=null)
301{
302    $all_events =& notificationEvents ($category_name, false, $module_id);
303    foreach ($all_events as $event) {
304        if ($event['name'] == $event_name) {
305            return $event;
306        }
307    }
308    return false;
309}
310
311
312/**
313 * Get an array of associative info arrays for subscribable categories
314 * for the selected module.
315 *
316 * @param  int  $module_id  ID of the module
317 * @return mixed
318 */
319
320function &notificationSubscribableCategoryInfo ($module_id=null)
321{
322    $all_categories =& notificationCategoryInfo ('', $module_id);
323
324    // FIXME: better or more standardized way to do this?
325    $script_url = explode('/', xoops_getenv('PHP_SELF'));
326    $script_name = $script_url[count($script_url)-1];
327
328    $sub_categories = array();
329
330    foreach ($all_categories as $category) {
331
332        // Check the script name
333
334        $subscribe_from = $category['subscribe_from'];
335        if (!is_array($subscribe_from)) {
336            if ($subscribe_from == '*') {
337                $subscribe_from = array($script_name);
338                // FIXME: this is just a hack: force a match
339            } else {
340                $subscribe_from = array($subscribe_from);
341            }
342        }
343        if (!in_array($script_name, $subscribe_from)) {
344            continue;
345        }   
346
347        // If 'item_name' is missing, automatic match.  Otherwise
348        // check if that argument exists...
349
350        if (empty($category['item_name'])) {
351            $category['item_name'] = '';
352            $category['item_id'] = 0;
353            $sub_categories[] = $category;
354        } else {
355            $item_name = $category['item_name'];
356            $id = ($item_name != '' && isset($_GET[$item_name])) ? intval($_GET[$item_name]) : 0;
357            if ($id > 0)  {
358                $category['item_id'] = $id;
359                $sub_categories[] = $category;
360            }
361        }
362    }
363    return $sub_categories;
364
365}
366
367/**
368 * Generate module config info for a particular category, event pair.
369 * The selectable config options are given names depending on the
370 * category and event names, and the text depends on the category
371 * and event titles.  These are pieced together in this function in
372 * case we wish to alter the syntax.
373 *
374 * @param  array  $category  Array of category info
375 * @param  array  $event     Array of event info
376 * @param  string $type      The particular name to generate
377 * return string
378 **/
379function notificationGenerateConfig (&$category, &$event, $type)
380{
381    switch ($type) {
382    case 'option_value':
383    case 'name':
384        return 'notify:' . $category['name'] . '-' . $event['name'];
385        break;
386    case 'option_name':
387        return $category['name'] . '-' . $event['name'];
388        break;
389    default:
390        return false;
391        break;
392    }
393}
394?>
Note: See TracBrowser for help on using the repository browser.