[19232] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | Plugin Name: Scheduler |
---|
| 4 | Version: auto |
---|
| 5 | Description: Schedule the availability of photos. |
---|
| 6 | Plugin URI: auto |
---|
| 7 | Author: plg |
---|
| 8 | Author URI: http://piwigo.org |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | defined('PHPWG_ROOT_PATH') or die('Hacking attempt!'); |
---|
| 12 | |
---|
| 13 | global $prefixeTable; |
---|
| 14 | |
---|
| 15 | // +-----------------------------------------------------------------------+ |
---|
| 16 | // | Define plugin constants | |
---|
| 17 | // +-----------------------------------------------------------------------+ |
---|
| 18 | |
---|
| 19 | defined('SCHEDULER_ID') or define('SCHEDULER_ID', basename(dirname(__FILE__))); |
---|
| 20 | define('SCHEDULER_PATH' , PHPWG_PLUGINS_PATH . SCHEDULER_ID . '/'); |
---|
| 21 | define('SCHEDULER_TABLE', $prefixeTable . 'scheduler'); |
---|
| 22 | define('SCHEDULER_ADMIN', get_root_url() . 'admin.php?page=plugin-' . SCHEDULER_ID); |
---|
| 23 | define('SCHEDULER_PUBLIC', get_absolute_root_url() . make_index_url(array('section' => 'scheduler')) . '/'); |
---|
| 24 | define('SCHEDULER_DIR', PWG_LOCAL_DIR . 'scheduler/'); |
---|
| 25 | define('SCHEDULER_VERSION', 'auto'); |
---|
| 26 | |
---|
| 27 | // +-----------------------------------------------------------------------+ |
---|
| 28 | // | Add event handlers | |
---|
| 29 | // +-----------------------------------------------------------------------+ |
---|
| 30 | |
---|
| 31 | // init the plugin |
---|
| 32 | add_event_handler('init', 'scheduler_init'); |
---|
| 33 | |
---|
| 34 | // publish scheduled photos |
---|
| 35 | add_event_handler('init', 'scheduler_init_publish'); |
---|
| 36 | |
---|
| 37 | if (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 |
---|
| 50 | include_once(SCHEDULER_PATH . 'include/functions.inc.php'); |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * plugin initialization |
---|
| 54 | * - check for upgrades |
---|
| 55 | * - unserialize configuration |
---|
| 56 | * - load language |
---|
| 57 | */ |
---|
| 58 | function 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 = ' |
---|
| 77 | UPDATE '. PLUGINS_TABLE .' |
---|
| 78 | SET version = "'. SCHEDULER_VERSION .'" |
---|
| 79 | WHERE 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 |
---|
[19382] | 95 | // $conf['scheduler'] = unserialize($conf['scheduler']); |
---|
[19232] | 96 | } |
---|
| 97 | |
---|
| 98 | function scheduler_init_publish() |
---|
| 99 | { |
---|
| 100 | $query = ' |
---|
| 101 | SELECT * |
---|
| 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 = ' |
---|
| 136 | DELETE |
---|
| 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 | ?> |
---|