source: extensions/scheduler/main.inc.php @ 26398

Last change on this file since 26398 was 19382, checked in by plg, 12 years ago

bug fixed: no scheduler

File size: 4.0 KB
Line 
1<?php
2/*
3Plugin Name: Scheduler
4Version: auto
5Description: Schedule the availability of photos.
6Plugin URI: auto
7Author: plg
8Author URI: http://piwigo.org
9*/
10
11defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
12
13global $prefixeTable;
14
15// +-----------------------------------------------------------------------+
16// | Define plugin constants                                               |
17// +-----------------------------------------------------------------------+
18
19defined('SCHEDULER_ID') or define('SCHEDULER_ID', basename(dirname(__FILE__)));
20define('SCHEDULER_PATH' ,   PHPWG_PLUGINS_PATH . SCHEDULER_ID . '/');
21define('SCHEDULER_TABLE',   $prefixeTable . 'scheduler');
22define('SCHEDULER_ADMIN',   get_root_url() . 'admin.php?page=plugin-' . SCHEDULER_ID);
23define('SCHEDULER_PUBLIC',  get_absolute_root_url() . make_index_url(array('section' => 'scheduler')) . '/');
24define('SCHEDULER_DIR',     PWG_LOCAL_DIR . 'scheduler/');
25define('SCHEDULER_VERSION', 'auto');
26
27// +-----------------------------------------------------------------------+
28// | Add event handlers                                                    |
29// +-----------------------------------------------------------------------+
30
31// init the plugin
32add_event_handler('init', 'scheduler_init');
33
34// publish scheduled photos
35add_event_handler('init', 'scheduler_init_publish');
36
37if (defined('IN_ADMIN'))
38{
39  // new tab on photo page
40  add_event_handler('tabsheet_before_select', 'scheduler_tabsheet_before_select', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
41
42  // replace level for display on photo edition main page
43  add_event_handler('loc_begin_admin_page', 'scheduler_photo_edit_replace_level');
44
45  // file containing all previous handlers functions
46  include_once(SCHEDULER_PATH . 'include/admin_events.inc.php');
47}
48
49// files containing specific plugin functions
50include_once(SCHEDULER_PATH . 'include/functions.inc.php');
51
52/**
53 * plugin initialization
54 *   - check for upgrades
55 *   - unserialize configuration
56 *   - load language
57 */
58function scheduler_init()
59{
60  global $conf, $pwg_loaded_plugins;
61 
62  // apply upgrade if needed
63  if (
64    SCHEDULER_VERSION == 'auto' or
65    $pwg_loaded_plugins[SCHEDULER_ID]['version'] == 'auto' or
66    version_compare($pwg_loaded_plugins[SCHEDULER_ID]['version'], SCHEDULER_VERSION, '<')
67  )
68  {
69    // call install function
70    include_once(SCHEDULER_PATH . 'include/install.inc.php');
71    scheduler_install();
72   
73    // update plugin version in database
74    if ( $pwg_loaded_plugins[SCHEDULER_ID]['version'] != 'auto' and SCHEDULER_VERSION != 'auto' )
75    {
76      $query = '
77UPDATE '. PLUGINS_TABLE .'
78SET version = "'. SCHEDULER_VERSION .'"
79WHERE id = "'. SCHEDULER_ID .'"';
80      pwg_query($query);
81     
82      $pwg_loaded_plugins[SCHEDULER_ID]['version'] = SCHEDULER_VERSION;
83     
84      if (defined('IN_ADMIN'))
85      {
86        $_SESSION['page_infos'][] = 'Scheduler updated to version '. SCHEDULER_VERSION;
87      }
88    }
89  }
90 
91  // load plugin language file
92  load_language('plugin.lang', SCHEDULER_PATH);
93 
94  // prepare plugin configuration
95  // $conf['scheduler'] = unserialize($conf['scheduler']);
96}
97
98function scheduler_init_publish()
99{
100  $query = '
101SELECT *
102  FROM '.SCHEDULER_TABLE.'
103  WHERE scheduled_for <= NOW()
104;';
105  $result = pwg_query($query);
106
107  $updates = array();
108  $scheduler_delete_ids = array();
109 
110  while ($row = pwg_db_fetch_assoc($result))
111  {
112    array_push(
113      $updates,
114      array(
115        'id' => $row['image_id'],
116        'date_available' => $row['scheduled_for'],
117        'level' => $row['level'],
118        )
119      );
120
121    $scheduler_delete_ids[] = $row['image_id'];
122  }
123
124  if (count($scheduler_delete_ids) > 0)
125  {
126    mass_updates(
127      IMAGES_TABLE,
128      array(
129        'primary' => array('id'),
130        'update' => array_keys($updates[0])
131        ),
132      $updates
133      );
134
135    $query = '
136DELETE
137  FROM '.SCHEDULER_TABLE.'
138  WHERE image_id IN ('.implode(',', $scheduler_delete_ids).')
139;';
140    pwg_query($query);
141
142    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
143    invalidate_user_cache();
144  }
145}
146
147?>
Note: See TracBrowser for help on using the repository browser.