4images Forum & Community

4images Modifications / Modifikationen => Mods & Plugins (Requests & Discussions) => Topic started by: Sin Nombre on August 03, 2008, 06:09:30 PM

Title: time released uploads ("Delayed Activation")
Post by: Sin Nombre on August 03, 2008, 06:09:30 PM
i maintain a flash game gallery and i often upload many games at once
but i want them to be released one each day or another time interval.
i was wondering if it was even remotely possible to enable a delayed publish system.

for example lets say i upload 7 games on Monday September 1st.
i want the 1st game to be activated on monday 9/1
the 2nd game activated tuesday 9/2.
3rd game activated wednesday 9/3.
and so on.

i have some idea on how this would be implemented but i am not an expert php coder and the core of 4image is still a bit of a mystery to me.
i was hoping someone could help me with this and that this could be of some use to other people as well.

if it were to work i think it would be an optional field when uploading an individual image possibly under "User" and over "Activate" or somewhere on that form page.

the sooner the better please. thank you in advanced.
Title: Re: time released uploads
Post by: budduke on August 05, 2008, 01:28:34 AM
It sounds like you and I are looking for the same thing.
I am wanting to upload a years worth of monthly calendars and just have them to become active at the beginning of thier month.

I had to put that project on the back burner because of other priorities but with all these talented programmers out there you would think it would not be so hard to do and I feel it would be a fantistic MOD addition to the 4images package as a whole.

When I was thinking about doing it, I was going to make an extra field in the database with a "Date to be activated" and then imbed some kind of script that would do a quick scan of the database once a day (only scanning when the first person of the day is opening the website to save on server power which would need another variable in the settings area maybe to see if the scan was made yet for that day? I think the script should be called from maybe in the header somewhere?) and if it saw something that needed to be activated then it would activate the image/game/media at that time. But that is about as far as I got on the project.

I can not believe that we are the only two looking for something like this.

I hope you get some answers and I hope that maybe I lit some lightbulbs to lead these great programmers in a path that may work and maybe they can do it much easier then I could.

I hope you get an answer soon because I am getting my calendars almost ready to post at my site and right now I am just uploading them manually at the beginning of each month.

Title: Re: time released uploads
Post by: Sin Nombre on August 09, 2008, 06:16:03 PM
its good to know there's someone else out there with the same problem.
i hope theres a coder trying to figure this out because im sure we're not the only ones.
i have some abstract clue to how to resolve it.
im guessing it would link the "activate" table (1 or 0)
to a new table where the data of the release date would be stored from the upload form.
i have a hunch that the date() function should be used...i think.
im only guessing from a few php and mysql tutorials i've read a while ago
but im not sure how to code it with or without 4img core coding.
Title: Re: time released uploads
Post by: budduke on August 17, 2008, 06:17:55 PM
UPDATED 08-24-2008!!! PLEASE READ!
This modified post includes all discussions up to reply#20

With the help of V@no, this appears to work pretty good...
This MOD give you the option to enter a delayed activation date/time for activating an image later automatically.
This only works in the admin ACP area. The end users to your site can not use this MOD.
Not sure what mods you are using but I modifield the main add/edit area and also
I am using the "[MOD] Batch Copy/Move/Edit Images v3.7.3 " to make it easier to change multiple items.
If you are using different mods, then inside of them just select 'not activated' and then make your delayed activation settings after they are uploaded using the edit 'not activated' items.


There are 4 files that will be changed,
admin/images.php
includes/functions.php
includes/page_header.php (This MOD is called from this page for activating images)
lang/english/admin.php
Backup these files before you start changing them!


lets start with the long one
in the admin/images.php file

look for
Code: [Select]
$image_ids = implode(",",$selectimages);insert after
Code: [Select]
// MOD delayed activation
$delayed_activate = (trim($HTTP_POST_VARS['delayed_activate']) != "") ? trim($HTTP_POST_VARS['delayed_activate']) : 0;
$image_active = (isset($HTTP_POST_VARS['delayed_activate']) && !empty($HTTP_POST_VARS['delayed_activate'])) ? 0 : $HTTP_POST_VARS['image_active'];
$HTTP_POST_VARS['image_active']= $image_active;
$HTTP_POST_VARS['delayed_activate']= $delayed_activate;
$my_loop= explode(",",$image_ids);
foreach ($my_loop as $var){
if (!$delayed_activate){
delay_activate($var, "remove");
}
else {
$delayed_activate=delay_activate($var,$delayed_activate);
}
}
// END MOD delayed activation

look for
Code: [Select]
$list .= (($row['image_name']) ? "<a href=\"../details.php?image_id=".$row['image_id']."\" target=\"_blank\">".$row['image_name']."</a>" : "<b class=\"marktext\">".$lang['lang_error']."</b>")." (ID: ".$row['image_id']."), ";
insert after
Code: [Select]
// MOD delayed activation
  $image_row[$row['image_id']]['delayed_activate']= delay_activate($row['image_id']);
// END MOD delayed activation

look for
Code: [Select]
show_radio_row($lang['field_free'], "image_active", $image_row[$image_ids[0]]['image_active']);
insertafter
Code: [Select]
// MOD delayed activation
    show_date_input_row($lang['field_delayed'].$lang['date_format'].$lang['delay_desc'], "delayed_activate", $image_row[$image_ids[0]]['delayed_activate'], $textinput_size);
// END MOD delayed activation

There are 2 instances of this
look for this entry under if ($action == "updateimage") area
Code: [Select]
$image_active = intval($HTTP_POST_VARS['image_active']);
insert after this location only
Code: [Select]
// MOD delayed activation
  $delayed_activate = (trim($HTTP_POST_VARS['delayed_activate']) != "") ? trim($HTTP_POST_VARS['delayed_activate']) : 0;
// END MOD delayed activation

look for
Code: [Select]
if (empty($error)) {
    $additional_sql = "";
    if (!empty($additional_image_fields)) {
      $table_fields = $site_db->get_table_fields(IMAGES_TABLE);
      foreach ($additional_image_fields as $key => $val) {
        if (isset($HTTP_POST_VARS[$key]) && isset($table_fields[$key])) {
          $additional_sql .= ", $key = '".un_htmlspecialchars(trim($HTTP_POST_VARS[$key]))."'";
        }
      }
    }
insert after
Code: [Select]
// MOD delayed activation
if ($delayed_activate){
//delay_activate($image_id,$delayed_activate);
$delayed_activate=delay_activate($image_id,$delayed_activate);
$image_active = (isset($HTTP_POST_VARS['delayed_activate']) && !empty($HTTP_POST_VARS['delayed_activate'])) ? 0 : $HTTP_POST_VARS['image_active'];
$HTTP_POST_VARS['image_active']= $image_active;
$HTTP_POST_VARS['delayed_activate']= $delayed_activate;
}
else {
delay_activate($image_id,"remove");
}
// END MOD delayed activation

look for
Code: [Select]
if ($action == $lang['save'] || $action == "save") {
  $editname = isset($HTTP_POST_VARS['editname']) ? intval(trim($HTTP_POST_VARS['editname'])) : 0;
  $editdescription = isset($HTTP_POST_VARS['editdescription']) ? intval(trim($HTTP_POST_VARS['editdescription'])) : 0;
  $editkeywords = isset($HTTP_POST_VARS['editkeywords']) ? intval(trim($HTTP_POST_VARS['editkeywords'])) : 0;
  $editdate = isset($HTTP_POST_VARS['editdate']) ? intval(trim($HTTP_POST_VARS['editdate'])) : 0;
insert after
Code: [Select]
// MOD delayed activation
  $editdelay = isset($HTTP_POST_VARS['editdelay']) ? intval(trim($HTTP_POST_VARS['editdelay'])) : 0;
  $delayed_activate = array();
  $image_active = array();
// END MOD delayed activation

look for
Code: [Select]
else {
   $image_date[] = 0;
  }
insert after
Code: [Select]
// MOD delayed activation
  if (isset($HTTP_POST_VARS['delayed_activate'])) {
    $delayed_activate = $HTTP_POST_VARS['delayed_activate'];
  }
  else {
   $delayed_activate[] = 0;
  }
// END  MOD delayed activation

look for
Code: [Select]
$set = (($editname && !empty($image_name[$key])) ? " image_name = '".trim(addslashes($image_name[$key]))."'" : "").(($editdescription) ? ", image_description = '".trim(addslashes($image_description[$key]))."'" : "").(($editkeywords) ? ", image_keywords = '".trim(addslashes($image_keywords[$key]))."'" : "").(($editdate) ? ", image_date = UNIX_TIMESTAMP('".$image_date[$key]."')" : "");
replace with
Code: [Select]
// MOD delayed activation
if ($editdelay && !empty($delayed_activate[$key])){
$delayed_activate[$key] = delay_activate($val,$delayed_activate[$key]);
$image_active[$key]=0;
}
else if ($editdelay) {
delay_activate($val,"remove");
$image_active[$key]=1;
}
// END MOD delayed activation
    $set = (($editname && !empty($image_name[$key])) ? " image_name = '".$image_name[$key]."'" : "").(($editdescription) ? ", image_description = '".$image_description[$key]."'" : "").(($editkeywords) ? ", image_keywords = '".trim(addslashes($image_keywords[$key]))."'" : "").(($editdate) ? ", image_date = UNIX_TIMESTAMP('".$image_date[$key]."')" : "");
// MOD delayed activation
if (!$image_active[$key]){
$set = $set.", image_active = 0";
}
if ($set){
// END MOD delayed activation

look for
Code: [Select]
    $sql = "UPDATE ".IMAGES_TABLE."
            SET".trim($set, ",")."
            WHERE image_id = $val";
//echo $sql."<br>";
    $result = $site_db->query($sql);
insert after
Code: [Select]
// MOD delayed activation
}
// END MOD delayed activation

look for
Code: [Select]
$image_row = $site_db->query_firstrow($sql);
insert after
Code: [Select]
// MOD delayed activation
  $image_row['delayed_activate'] = delay_activate($image_id);
// END MOD delayed activation

look for
Code: [Select]
show_input_row($title, "image_date", $image_row['image_date'], $textinput_size);
Replace with
Code: [Select]
// MOD delayed activation extra
  show_date_input_row($title, "image_date", $image_row['image_date'], $textinput_size);
  //show_input_row($title, "image_date", $image_row['image_date'], $textinput_size);
  show_date_input_row($lang['field_delayed'].$lang['date_format'].$lang['delay_desc'], "delayed_activate", $image_row['delayed_activate'], $textinput_size);
// END MOD delayed activation

look for
Code: [Select]
$remote_file = trim($HTTP_POST_VARS['remote_file_'.$i]);
    $remote_thumb_file = trim($HTTP_POST_VARS['remote_thumb_file_'.$i]);
    $image_download_url = trim($HTTP_POST_VARS['image_download_url_'.$i]);
insert after
Code: [Select]
// MOD delayed activation
$delayed_activate = trim($HTTP_POST_VARS['delayed_activate_'.$i]);
// END MOD delayed activation

look for
Code: [Select]
$image_active = trim($HTTP_POST_VARS['image_active_'.$i]);
Replace with
Code: [Select]
// MOD delayed activation
        //$image_active = trim($HTTP_POST_VARS['image_active_'.$i]);
$delayed_activate = (trim($HTTP_POST_VARS['delayed_activate_'.$i]) != "") ? trim($HTTP_POST_VARS['delayed_activate_'.$i]) : 0;

$image_active = (trim($HTTP_POST_VARS['delayed_activate_'.$i]) != "") ? 0 : trim($HTTP_POST_VARS['image_active_'.$i]);
$HTTP_POST_VARS['image_active_'.$i] = $image_active;
// END MOD delayed activation

look for
Code: [Select]
$result = $site_db->query($sql);
        $image_id = $site_db->get_insert_id();
insert after
Code: [Select]
// MOD delayed activation
if ($delayed_activate){
$delayed_activate = delay_activate($image_id,$delayed_activate);
}
// END MOD delayed activation

look for
Code: [Select]
show_radio_row($lang['field_free'], "image_active_".$i, 1);
Insert after
Code: [Select]
// MOD delayed activation
    show_date_input_row($lang['field_delayed'].$lang['date_format'].$lang['delay_desc'], "delayed_activate_".$i, "", $textinput_size);
// END MOD delayed activation

look for
Code: [Select]
show_radio_row("Date", "editdate", 0);
insert after
Code: [Select]
// MOD delayed activation
  show_radio_row("Delayed Activation","editdelay",0);
// END MOD delayed activation

look for
Code: [Select]
$nextpage = isset($HTTP_POST_VARS['nextpage']) ? intval(trim($HTTP_POST_VARS['nextpage'])) : 0;
insert Before
Code: [Select]
// MOD delayed activation
  $editdelay = isset($HTTP_POST_VARS['editdelay']) ? intval(trim($HTTP_POST_VARS['editdelay'])) : 0;
// END MOD delayed activation

look for
Code: [Select]
if (!$editname && !$editdescription && !$editkeywords && !$editdate) {
replace with
Code: [Select]
// MOD delayed activation
    //if (!$editname && !$editdescription && !$editkeywords && !$editdate &&) {
    if (!$editname && !$editdescription && !$editkeywords && !$editdate && !$editdelay) {
// END MOD delayed activation

look for
Code: [Select]
while ($image_row = $site_db->fetch_array($result)) {
insert after
Code: [Select]
// MOD delayed activation
$image_row['delayed_activate'] = delay_activate($image_row['image_id']);
// END MOD delayed activation

look for 2 times in file
Code: [Select]
if ($editname || $editdescription || $editkeywords || $editdate) {
replace with both places
Code: [Select]
// MOD delayed activation
  //if ($editname || $editdescription || $editkeywords || $editdate  {
      if ($editname || $editdescription || $editkeywords || $editdate || $editdelay) {
// END MOD delayed activation  

look for
Code: [Select]
else {
        echo format_date($config['date_format'], $image_row['image_date']);
      }
insert after
Code: [Select]
// MOD delayed activation
  if ($editdelay) {
echo "<BR><BR>".$lang['field_delayed']."<BR>";
    echo "<input name=\"delayed_activate[$i]\" value=\"".$image_row['delayed_activate']."\" /><br />yyyy-mm-dd hh:mm:ss";
  }
  else if ($image_row['delayed_activate']){
echo "<BR><BR>".$lang['delayed_on'].$image_row['delayed_activate'];
  }
// END MOD delayed activation

look for 2 times in file
Code: [Select]
show_hidden_input("editdate", $editdate);
insert after both places
Code: [Select]
// MOD delayed activation
show_hidden_input("editdelay", $editdelay);
// END MOD delayed activation


in the includes/functions.php file

look for
Code: [Select]
?>insert before
Code: [Select]
// MOD delayed activation
function delay_activate($image_id = 0, $date = 0)
{
  global $site_db, $config;
  $check_every = (60*1); //check once per 1 minute
  // $check_every = (1 * 60 * 60);// once a hour
  // $check_every = (1 * 24 * 60 * 60);// once a day
  // $check_every = (7 * 24 * 60 * 60);// once a week
  // $check_every = (14 * 24 * 60 * 60);// once every 2 weeks
  // $check_every = (28 * 24 * 60 * 60);// once a month
 
  $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
    {
if ($date == "remove"){ // if you deleted the date stamp for delayed activation this removes it from the list
$config['delayed_activate'][$image_id]= "";
$update = true;
}
else {
      $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
  $return = $config['delayed_activate'][$image_id];
  }
    }
    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 && $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 if ($val){
        $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);
  }

  if ($return){
 return date("Y-m-d H:i:s",$return); //returns the date in  proper display format
 }
}
// END MOD delayed activation

in the lang/english/admin.php file

look for
Code: [Select]
?>insert before
Code: [Select]
// MOD delayed activation
$lang['field_delayed'] = "Delayed activation";
$lang['delayed_on'] = "Delayed activation is on.<BR>The date set for this is<BR>";
$lang['delay_desc']= "<br /><span class=\"smalltext\">Leave blank to not use Delayed activation.<span />";
// END MOD delayed activation

I placed the call for the function
in the includes/page_header.php file (per V@no advice)

look for
Code: [Select]
//-----------------------------------------------------
//--- Register Global Vars ----------------------------
//-----------------------------------------------------
$total_images = 0;
$total_categories = 0;
$auth_cat_sql['auth_viewcat']['IN'] = 0;
$auth_cat_sql['auth_viewcat']['NOTIN'] = 0;
insert after
Code: [Select]
// MOD delayed activation
delay_activate();
// END MOD delayed activation

this will call the function everytime someone clicks on the page_header.php file
the function works by looking at the next date to check images, if not yet then it immediately return from the function
otherwise it scans the array for anything that is set for delayed activation that needs to be activated.
Near the beginning of the function you will see the different intervals you can have it checking for activating images.
just add // to the original line and remove the // from the line you want to use.
I had this running every minute and saw no slowdown on my site I even had it putting new images up every minute. pretty cool watching them pop up so fast with having to do anything.


hope this works for you...


Title: Re: time released uploads
Post by: Sin Nombre on August 17, 2008, 08:13:50 PM
wow thank you so much that looks like alot of code. i appreciate the time and effort put in, i'll try it out today. and i'll update this post later to tell you if it worked for me.
thanks again.

i haven't put in the batch edit mod but i will do that first. i've run quite a few mods already and attempted a few that didn't work out too well and i think it might be effecting the speed of the website. it takes a little over 10 seconds to fully load my site...
Title: Re: time released uploads
Post by: sigma. on August 17, 2008, 09:40:56 PM
This is exactly what I need. It would save me allot of time to upload everything at once and set each image to publish one day after each other instead of doing it manually everyday. I will also give this a try and let you know.

I would of thought that with the new version of 4images 1.7.6, since there is a calendar tool beside the date and time for each image, that it would do just this. Unfortunately If you put your image to a future date it has no affect. Which is pointless.

Thanks for this!
Title: Re: time released uploads
Post by: Sin Nombre on August 21, 2008, 04:38:23 AM
im kind of having 2nd thoughts about implementing more mods than i already have as it will slow things down... i'll try this and see if it slows things down too much if so i'll take it off and post it as an update.

Update:
i just started and i can't seem to find "the ($action == "saveedit") area"
im using adobe dreamweaver to edit and its got a decent search.

ok i had to get the batch mod for saveedit area.

but the last step. i can't find "$site_area = "0";"
so i just put the code at the top i'll have 2 test it later as its getting really late. so...2 b continued.
Title: Re: time released uploads
Post by: budduke on August 21, 2008, 12:22:36 PM
that is the nice thing about this mod, it call the routine everytime but if there is nothing for it to do it imediately exits the routine, so I have not noticed any slowdown at my site.
I have visited your site a couple of times and I see what you are complaining about.
Have you loaded the 4images_ms local server on your computer and uploaded your site to it?
Does it give you the same symtoms?
I use that little program all the time for my testing so I don't have to crash my main site doing these mods.

I have heard people say that google ads slow the load time down, but I am not an expert on that because I do not use that service.

If you do not see the add/save area then you probably do not have the batch copy/edit mod file. the main reason for most of those changes is to give you the button to actually select the delayed activation when you are editing files.
as long as you found most of them I think you should be ok.

the index.php file "$site_area"
in my file as long as you place it after this area you should be fine (yes, it is at the top of the file)
Code: [Select]
define('GET_CACHES', 1);
define('ROOT_PATH', './');
define('GET_USER_ONLINE', 1);
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
$user_access = get_permission();
$site_area = "0";
Title: Re: time released uploads
Post by: Sin Nombre on August 21, 2008, 04:24:46 PM
i just tried it and set the date for today an hour from now and then another one at a day from now they both activated and came up on the front page. maybe it was my copy and pasting last night i was tired. or maybe i didn't press no for activation above that...
Title: Re: time released uploads
Post by: budduke on August 21, 2008, 10:01:00 PM
upload something the normal way without a date attached to it and see what time stamp it gets.
I found out my server provider is (what looks like) one day ahead on my site.
I was having the same issue till I posted something normally and saw it had the correct time, just one day ahead.
so now I am posting in the future...
and you also have to remember that it will post everything for that entire 24hr day that it is looking at.
I may have written my formulas wrong, will look at it again this weekend.
I posted 5 items one day apart and it seems to be working...
let me know if it still is not working for you...
Title: Re: time released uploads
Post by: Sin Nombre on August 22, 2008, 05:54:47 PM
ok i checked the code again and i copied it verbatim.
i uploaded an image yesterday to be activated the 23rd
the server is located in europe so it's 6 hours ahead
so theoretically the new game should appear at 6pm EST. today.
i did click the radio button activate to no as well.
i set that to activate and the game appeared on the front page so i changed it to activate: no.

hopefully it's like a light switch and the delayed activation is like the trigger that flips it to activate.
the event to the trigger is the time specified on the textfield.
i'll reply after 6pm EST to tell you if it does or doesn't activate.
Title: Re: time released uploads
Post by: budduke on August 22, 2008, 06:21:37 PM
oops, forgot to add that info to my original posting.

Yes, you have to set the image to "not activated"
and then set the "delayed Activation" to "on". because if the image is already active there is no reason to activate it later.
and you are correct. when the date arrives that the image is set for then the delayed activation will turn the "activate" switch "on".

hope it works!
Title: Re: time released uploads
Post by: Sin Nombre on August 22, 2008, 07:34:36 PM
ohhhh it all makes sense now i hope it does too then i could upload a month or 2 worth and just forget about it till its running low which i can set on my google calendar.
thanks again. im looking into an easy way to make an automated referal link system maybe i'll find a website that's already ahead of me and its free. if only i knew python so i could use google apps to make it somehow. but anyways thanks
Title: Re: time released uploads
Post by: V@no on August 23, 2008, 02:43:26 AM
I'm sorry, I didn't look thoroughly through the code, and can't really tell how efficient it is, but IMHO its a little over complicated then it could be...

In theory all it needs is:
1) new field in the database that would store the date of the activation in the image entry (yes, you got that part done).
2) add info about that field into db_field_definitions.php, it should be set as "text" type and will be used the same way as image_date field, it will store the date of activation.
3) in member.php and image.php "catch" and convert text into time stamp the POST of delayed_activate before it saved along with the rest custom fields:
i.e. above     if (!empty($additional_image_fields)) {
      $table_fields = $site_db->get_table_fields(IMAGES_TABLE);

insert:
    if (isset($HTTP_POST_VARS['delayed_activate'])) $HTTP_POST_VARS['delayed_activate'] = UnixTime($HTTP_POST_VARS['delayed_activate']); //save it as time stamp


Also, there needed a few other changes that would save images as "not activated" if delayed_activate is set:

above
        $sql = "INSERT INTO ".IMAGES_TABLE."


insert:
        $image_active = (isset($HTTP_POST_VARS['delayed_activate']) && !empty($HTTP_POST_VARS['delayed_activate'])) ? 0 : 1;

(well, in admin/images.php same thing (or similar) has to be done when saving edited image info)

4) in functions.php need new function (taken from php.net (http://us2.php.net/manual/en/function.time.php#77649), not tested):
function UnixTime($mysql_timestamp){
    if (preg_match('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
        || preg_match('/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) {
            $unix_time = mktime($pieces[4], $pieces[5], $pieces[6], $pieces[2], $pieces[3], $pieces[1]);
    } elseif (preg_match('/\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp)
        || preg_match('/\d{2}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp)
        || preg_match('/\d{4}\-\d{2}\-\d{2}/', $mysql_timestamp)
        || preg_match('/\d{2}\-\d{2}\-\d{2}/', $mysql_timestamp)) {
            $unix_time = strtotime($mysql_timestamp);
    } elseif (preg_match('/(\d{4})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)
        || preg_match('/(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) {
            $unix_time = mktime(0, 0, 0, $pieces[2], $pieces[3], $pieces[1]);
    }
  return $unix_time;
}

4) execute the routine that checks if any images needed activation by comparing curent time (via time() timestamp) and timestamp in delayed_active field - that's probably the hardest part, because that part is what "can" hit on performance .

P.S.
This is only a theory, on practice it might be more complicated then it looks...

P.P.S.
never insert anything at the beginning of an existing line unless its absolutely necessary, otherwise its hard find the needed line for future mods. (i.e. your line has changes in the middle:
Code: [Select]
    $sql = "SELECT image_id, image_name, image_keywords, image_description, image_allow_comments, image_active, delayed_activate, FROM_UNIXTIME(image_date) AS image_date, image_media_file, image_thumb_file
Title: Re: time released uploads
Post by: Sin Nombre on August 23, 2008, 04:07:39 AM
it worked! i just came back from the movies and i checked my rss feed and the game activated!
Title: Re: time released uploads
Post by: V@no 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'])) ? 0 : 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();

Title: Re: time released uploads
Post by: sigma. 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?

Title: Re: time released uploads
Post by: budduke 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.
Title: Re: time released uploads
Post by: sigma. 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!
Title: Re: time released uploads
Post by: V@no 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).
Title: Re: time released uploads
Post by: budduke 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!
Title: Re: time released uploads
Post by: V@no 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)
Title: Re: time released uploads
Post by: budduke 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.
Title: Re: time released uploads
Post by: Sin Nombre 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.
Title: Re: time released uploads ("Delayed Activation")
Post by: sigma. 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.
Title: Re: time released uploads ("Delayed Activation")
Post by: V@no on August 31, 2008, 05:43:04 PM
You'll need either
http://www.4homepages.de/forum/index.php?topic=20501.0

or

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

not sure which one..
Title: Re: time released uploads ("Delayed Activation")
Post by: budduke 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 (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.
Title: Re: time released uploads ("Delayed Activation")
Post by: sigma. 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!
Title: Re: time released uploads ("Delayed Activation")
Post by: budduke 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 (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.
Title: Re: time released uploads ("Delayed Activation")
Post by: kar76 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
Title: Re: time released uploads ("Delayed Activation")
Post by: budduke on October 02, 2013, 12:07:27 AM
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

The reason I made the mod was because I upload a years worth of calendars and they get activated on the 1st of each of thier months so I do not have to remember to do it.
Your request looks doable but right now I am in Halloween mode. I put up a big halloween display every year. I will look at it when I get a chance.
Also, note on this mod there is a small bug. When you upload an image and want to activate it 2 weeks from now, it works, but the date on the image is todays date so it is not marked as new.
That will also be something I will change but now, I just go in and change the date on the image to the date I want it activated and then it is marked as new when it gets activated.