Author Topic: time released uploads ("Delayed Activation")  (Read 35585 times)

0 Members and 1 Guest are viewing this topic.

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: time released uploads
« Reply #15 on: August 23, 2008, 05:30:23 AM »
While I was wring my previous reply and going over your code, I realized that maybe your method is more efficient on large galleries with lots of images, because MySQL is a bottleneck if it has to scan through thousands of entries...
So, storing list of images into settings field would probably be the fastest way, unless you are planning on delay thousands of images at once.

Let me give you a few more hints on how to optimize your method.

1) you need only one new field into 4images_settings database table (no need new fields in 4images_images table)
2) you'll need new input fields in upload and edit image forms, lets leave them as you already done: delayed_activate. In that input field you can enter the date when the image will be activated (the same as the image date input field)
3) as in my code above check if anything entered into delayed_activate input form and if so, set image_active to 0
(i.e. $image_active = (isset($HTTP_POST_VARS['delayed_activate']) && !empty($HTTP_POST_VARS['delayed_activate'])) ? 1;


4) you'll need a new function in functions.php:
//MOD delayed activation
function delay_activate($image_id 0$date 0)
{
  global 
$site_db$config;
  
$check_every 60*1//check no more then once per 1 minute
  
  
$current_time time();
  
$sql_data "";
  
$update false;
  
$image_list "";
  
$return "";
  
  if (!isset(
$config['delayed_activate'])) //this block can be removed if delayed_activate field already added into settings table
  
{
    
$sql "INSERT INTO " SETTINGS_TABLE "
              (setting_name , setting_value)
            VALUES
              ('delayed_activate', '')"
;
    
$site_db->query($sql);
    
$config['delayed_activate'] = "";
  }

  if (!
is_array($config['delayed_activate'])) //this is first time this function called
  
{
    
$delayed_activate explode(","$config['delayed_activate']); //images separated by comma
    
$config['delayed_activate'] = array();
    foreach(
$delayed_activate as $val)
    {
      
$data explode(" "$val); //image id separated from activate delay timestamp by a space
      
if (isset($data[1]))
        
$config['delayed_activate'][$data[0]] = $data[1];
    }
    if (!isset(
$config['delayed_activate'][0]))
      
$config['delayed_activate'][0] = $current_time//store last time we checked delayed dates
  
}

  if (
$image_id//add or update existing image with new activation date
  
{
    if (
$date//add/update
    
{
      
$config['delayed_activate'][$image_id] = (is_numeric($date)) ? $date strtotime($date); //add new image id into the list
      
$update true//we need update settings database
    
}
    else 
//return delayed date for requested $image_id
    
{
      if (isset(
$config['delayed_activate'][$image_id]))
        
$return $config['delayed_activate'][$image_id];
    }
  }

  if (
$update || $config['delayed_activate'][0] <= $current_time $check_every//do we need update settings or is time for next update?
  
{
    
$config['delayed_activate'][0] = $current_time//store current time as update time
    
foreach($config['delayed_activate'] as $key => $val//loop through all delayed image list
    
{
      if (
$key && $val <= $current_time//if $key = 0 - its update timestamp and not image id, otherwise check if ac
      
{
        
$image_list .= "," $key//list of images to activate
      
}
      else
      {
        
$sql_data .= "," $key " " $val//save image id and activation delay date separated by space
      
}
    }
    
$image_list trim($image_list",");
    
$sql_data trim($sql_data",");
  }

  if (!empty(
$image_list)) //activate images
  
{
    
$sql "UPDATE " IMAGES_TABLE "
            SET image_active = 1
            WHERE image_id IN (" 
$image_list ")";
    
$site_db->query($sql);
  }

  if (!empty(
$sql_data)) //save list of images and time of last check
  
{
    
$sql "UPDATE " SETTINGS_TABLE ."
            SET setting_value = '" 
$sql_data "'
            WHERE setting_name = 'delayed_activate'"
;
    
$site_db->query($sql);
  }
// END function delay_activate
//END MOD delayed activation


This function is "all-in-one":
If its called with one paramter delay_activate($image_id) it will return unix timestamp of $image_id image, if no delay was stored for that id, it will return empty string, plus it will do checking and image activation if needed.
If its called with two parameters delay_activate($image_id, $date) it will add or update delay time of the $image_id, plus it will do checking and image activation if needed. In parameter $date you can either send unix timestamp or unmodified entry from delay_activate input field, it will convert it into unix timestamp.
If its called without any paramaters delay_activate() it will do only checking if its time for "delayed" images get activated and will activate needed images

By default it will do the check and database update no more then once a minute (set in $check_every variable), therefor its quiet safe add in page_header.php:delay_activate();

« Last Edit: August 23, 2008, 06:42:15 AM by V@no »
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline sigma.

  • Full Member
  • ***
  • Posts: 148
  • cydonian.com/potd
    • View Profile
    • sigma's gallery
Re: time released uploads
« Reply #16 on: August 23, 2008, 03:55:39 PM »
Before I go ahead and implement this MOD, is there anyway one of you can rewrite this code or clean it up? I'm sure the initial code is good and will work but I like the idea of keeping it simplified like V@no says since I have lots of MODs installed already.

V@no, do you think the original code is good to go? or should I hold off?


Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: time released uploads
« Reply #17 on: August 23, 2008, 04:33:15 PM »
To Sin Nombre,
Glad it worked! I checked your site also and saw the new game arrive on time!

To V@no,
I love it when a real programmer steps in and makes it look easy...
I am a hacker, not really thinking of the future, just want to get a problem of mine fixed or working.
The only thing I did not like was the idea that it is running this check every minute. not sure what it would do on server power.
In my case, I will only have 12 wallpapers, one for each month, to check on, but I think Sin Nombre had about 6 months worth of games that wanted to be activated this way.
I like your way much better though. I may still try to keep with the variable for checking intervals, which could just be hardcoded into the function that the site owner would decide which one to use.

If I understand it correclty, one field in the settings database with the ID&date seperated by commas and your function would handle adding, actiivating, and deleting images as needed from the list?
I would call your function everytime I needed to check on the image and how it is set instead of looking at another database field in the image database?
Is it better to place the call in the header.php instead of the index.php (just curiosity stepping in)

PS: sorry about putting my field in the middle of the SQL fields, I was searching for every instance of image_date and pasting my field right after it not thinking about future stuff.

Sin Nombre,
Let me redo my local testing site and see how all this works and I will modify my original post with the correct way to go about this. give me some time, it is HOT HOT HOT up here in Ohio this weekend and no AC in my computer room, just a big fan blowing on me.
Buddy Duke
www.budduke.com

Offline sigma.

  • Full Member
  • ***
  • Posts: 148
  • cydonian.com/potd
    • View Profile
    • sigma's gallery
Re: time released uploads
« Reply #18 on: August 23, 2008, 05:46:34 PM »
Thanks for the work you put into this budduke. I will definitely add this mod when you update the code. Very useful mod.
Stay Cool!

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: time released uploads
« Reply #19 on: August 23, 2008, 06:33:01 PM »
The only thing I did not like was the idea that it is running this check every minute. not sure what it would do on server power.
Well, actually, since there is no any database calls during the checking (image list automatically stored into $config by 4images), this will not hit on the performance, especially with so few images as you planning on, but in any case the interval can be changed:
$check_every 60*60*24*1;//once in 24 hours


If I understand it correclty, one field in the settings database with the ID&date seperated by commas and your function would handle adding, actiivating, and deleting images as needed from the list?
Correct, also there is always one entry that stores the time of last check even if no images were delayed.


I would call your function everytime I needed to check on the image and how it is set instead of looking at another database field in the image database?
Yes. From this point the "hardest" thing to do is to add new input field in upload form and in edit image form (if you are not planning non-admins use this feature, then it only requires modifications in admin/images.php)


Is it better to place the call in the header.php instead of the index.php (just curiosity stepping in)
I would (includes/page_header.php to be more specific).
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: time released uploads
« Reply #20 on: August 25, 2008, 01:47:41 AM »
I have updated my original post with a version that runs much better thanks to V@no!
If you have already changed your files from the original, you will need to start fresh or remove all the changes from your file before putting that mod in its place.
If you modified your database with the new fields from the old mod then you will need to delete them through your
PHPadmin program.

the settings database had these fields in it that will need to be deleted.
delayed_activate
delayed_activate_time

The images database had this field in it that will need to be deleted,
delayed_activate

and then everything should work fine...

Thanks again to V@no for stepping in and helping a beginner out!
Buddy Duke
www.budduke.com

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: time released uploads
« Reply #21 on: August 25, 2008, 02:38:33 AM »
Very well.
Just in case, I've attached to your post the original instructions for those who installed it and need uninstall. (found cached page on live.com)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: time released uploads
« Reply #22 on: August 25, 2008, 12:14:27 PM »
Thanks for posting the original, I usually just look through the file for // mod delay but I forgot that I changed a couple of lines in the sql commands that did not have the remark on them.
I just did not want to confuse anyone by having both of them listed but maybe I should have just put the updated one in a reply instead.
Live and learn by our mistakes.
Buddy Duke
www.budduke.com

Offline Sin Nombre

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Timewasters
Re: time released uploads
« Reply #23 on: August 26, 2008, 08:42:23 PM »
wow thanks to budduke and v@no for the code i apreciate it and im sure im not the only one.
i feel somewhat good being the 1st post even though i didn't really contribute code-wise.
thanks again!

UPDATE:
i just replaced the code and removed what i felt was excessive javascript and i just tested my page loads using tools.pingdom.com as well as visiting the site myself and it seems to be loading faster than normal  :D
im not sure if this had anything to do with it but i think it does. either way im happy (2 birds with 1 stone)
im going to test it soon.
« Last Edit: August 26, 2008, 09:52:02 PM by Sin Nombre »

Offline sigma.

  • Full Member
  • ***
  • Posts: 148
  • cydonian.com/potd
    • View Profile
    • sigma's gallery
Re: time released uploads ("Delayed Activation")
« Reply #24 on: August 31, 2008, 03:26:30 PM »
I would like to install this mod but my admin/images.php does not match even one line shown in your code.

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: time released uploads ("Delayed Activation")
« Reply #26 on: August 31, 2008, 05:57:31 PM »
my bad again...

my mod is based that you already are using this mod...

http://www.4homepages.de/forum/index.php?topic=6759.0

I have noticed when I search for stuff I have to make sure I did not include the spaces before or after the code or it sometimes does not find the code you are searching for.
Buddy Duke
www.budduke.com

Offline sigma.

  • Full Member
  • ***
  • Posts: 148
  • cydonian.com/potd
    • View Profile
    • sigma's gallery
Re: time released uploads ("Delayed Activation")
« Reply #27 on: August 31, 2008, 11:29:38 PM »
oooh ok I didn't know. I will add that MOD and come back to this one.
Thanks!

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: time released uploads ("Delayed Activation")
« Reply #28 on: September 14, 2008, 05:59:25 PM »
I noticed that the Batch/copy mod that this mod integrates with has been updated to 4.0.3
http://www.4homepages.de/forum/index.php?topic=6759.0
attached is a modified version of that mod with the delayed activation information already placed in it.

I will try to keep track of the changes on the other mod so I can update it on this end.

It seems a couple of places changed that you have to put information into so that is why I am just posted the file itself.
Buddy Duke
www.budduke.com

Offline kar76

  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: time released uploads ("Delayed Activation")
« Reply #29 on: October 01, 2013, 10:54:14 PM »
Hello Budduke

Well this Works:)
But this is not what is in my mind.
I see that in Delayed activation field i have to put date and time manually.
I want something different like if i choose delayed activation option then it scheduled
images in minutes or days from current day and time.
Like after 5 minutes or 60 minutes or after 2 days or 5 days intervals etc etc.
Is it Possible? It will be big help
Thanks
« Last Edit: October 01, 2013, 11:49:13 PM by kar76 »