redjumpsuit

Topic: Notify when ads are expiring

Hi all, I know some of you are wanting to notify your job posters when their ads are about to expire (if you are offering a premium service of some sort) so I thought I'd share this simple mod with all of you.

On /_includes folder, on class.Maintenance.php, comment out the block

Code:

    // deactivate jobs older than 30 days
    /**
    public function DeactivateJobs()
    {
         global $db;
         $sql = 'UPDATE jobs SET is_active = 0 WHERE DATEDIFF(NOW(), created_on) > 30 AND is_active = 1';
         $db->Execute($sql);
    } 
  **/

then add below it:

Code:

 // deactivate jobs older than 30 days + notification
    public function DeactivateJobs()
  {      
      $postMan = new Postman();

      global $db;
      
      $notifmin = 27; //the day when you want notification to be sent prior to the ad expiring
      $notifmax = 30; //the day the ad actually expires
      $dayscount = $notifmax - $notifmin;
        
      $sql = 'SELECT * FROM jobs WHERE DATEDIFF(NOW(), created_on) >= .' $notifmin '. AND is_active = 1';
                
      $row = $db->Execute($sql);
      foreach ( $row as $rows )
          {
          
          $ademail = trim(strtolower($rows['poster_email']))
          $adname = trim($rows['title']);
          $adcomp = trim($rows['company']);
          $adurl = BASE_URL . "job/" . $rows['id'] . "/";
          
          $sqlu = "UPDATE jobs SET is_active = 0 WHERE DATEDIFF(NOW(), created_on) > ". $notifmax ." AND is_active = 1 AND id = " . $rows['id']) ;
          $db->Execute($sqlu);
          $postMan->MailExpiringAd($ademail, $adname, $adcomp, $adurl, $dayscount);
          
          }
  }

on /_includes, on class.Postman.php

add:

Code:

// mail client when ad is expiring
    public function MailExpiringAd($email, $name, $comp, $url, $days)
    {
        $msg = EMAIL_HEADER . "\n\nWe'd like to inform you that your ad <". $name .">: \n\n". $url ."\n\nunder the Company <". $comp ."> is expiring in ". $days ." days.";
        $msg .= "\n\nThank you for using our service!";
        $msg .= "\n\n---\n" . EMAIL_FOOTER ;

        $subject = "Your ad in " . SITE_NAME . " is expiring in ". $days ." days.";

        if (mail($email, $subject, $msg, "From: " . SITE_NAME . " <" . NOTIFY_EMAIL .">"))
        {
            return true;
        }
        else
        {
            return false;
        }   
    }

on cron_maintenance.php

add:

Code:

require_once '_includes/class.Postman.php';

and make sure that this line exists

Code:

    // deactivate jobs older than 30 days + notification
    $janitor->DeactivateJobs();

and lastly, you must set-up a cron job to run the cron_maintenance.php file or else the script wont work wink

as usual, for other questions, you can email me at myredjumpsuit [at] gmail [dot] com

will host your job board for you at minimal cost! read more: http://mim.io/acb992

Tokyoj

Re: Notify when ads are expiring

redjumpsuit...looks good. Your code is set to 30 days posting period with notification on the 27th day. I'll insert it tonight and test it...but I can't wait 30 days...so will edit the code to use a 2-day posting period (instead of 30)... and 1 day before expiration.

So _includes folder, on class.Maintenance.php becomes, correct?

<? PHP

public function DeactivateJobs()
      {     
      $postMan = new Postman();
      global $db;
      $notifmin = 1; //the day when you want notification to be sent prior to the ad expiring
      $notifmax = 2; //the day the ad actually expires
      $dayscount = $notifmax - $notifmin;
      $sql = 'SELECT * FROM jobs WHERE DATEDIFF(NOW(), created_on) >= .' $notifmin '. AND is_active = 1';
      $row = $db->Execute($sql);
      foreach ( $row as $rows )
          {
          $ademail = trim(strtolower($rows['poster_email']))
          $adname = trim($rows['title']);
          $adcomp = trim($rows['company']);
          $adurl = BASE_URL . "job/" . $rows['id'] . "/";
          $sqlu = "UPDATE jobs SET is_active = 0 WHERE DATEDIFF(NOW(), created_on) > ". $notifmax ." AND is_active = 1 AND id = " . $rows['id']) ;
          $db->Execute($sqlu);
          $postMan->MailExpiringAd($ademail, $adname, $adcomp, $adurl, $dayscount);
          }
  }
}
?>

postman.php and the other files do not need adjusting.

---------------
Tokyoj
"You can't build a reputation on what you're going to do." (Henry Ford)
"Insanity is doing the same thing over and over, expecting different results." (Albert Einstein)

redjumpsuit

Re: Notify when ads are expiring

hi tokyoj, that is correct. i wrote it that way so that it can easily be configured smile

redjumpsuit is all about making people's lives a little bit better wink

will host your job board for you at minimal cost! read more: http://mim.io/acb992

evertsemeijn

Re: Notify when ads are expiring

Might be a very stupid question (I'm sorry), but how do I set up the cron job? I don't have a crontab on my server. Is there any php-way?

Member of Jobberbase Development Team - Templates/Usability :: Looking for installation and/or custom design? :: Beautiful Wordpress themes

redjumpsuit

Re: Notify when ads are expiring

hi, the only real purpose of the cron job is to automate running scripts for you. the php way is to just actually run/access the php file. if your cron_maintenance.php is on your root folder, then you can just access it like http://<yourjobberbasesite>/cron_maintenance.php

would be good though to name your cron_maintenance.php file into something unique to you (ike cron_maintenance_xxxxxx.php) where x represents values (like a code or pin) that only you would know, so that not just anybody could run your cron_maintenance.php file

i hope this answers your question.

RedJumpsuit

Last edited by redjumpsuit (2009-01-24 13:06:36)

will host your job board for you at minimal cost! read more: http://mim.io/acb992

evertsemeijn

Re: Notify when ads are expiring

Great tip!! Didn't know that about the renaming. And suppose a blank screen means no errors, right?

Member of Jobberbase Development Team - Templates/Usability :: Looking for installation and/or custom design? :: Beautiful Wordpress themes

redjumpsuit

Re: Notify when ads are expiring

yup no errors.

will host your job board for you at minimal cost! read more: http://mim.io/acb992

evertsemeijn

Re: Notify when ads are expiring

Is there anything I could do to show output when I run the cronjob? I get a white screen (supposedly that's correct @redjumpsuit), but the jobs are not deactivated.  Is there any way to let the cronjob show a 'Yeah, I did my job' message when it ran correctly and 'WRONG (error: what's the error)' when something goes wrong?

Member of Jobberbase Development Team - Templates/Usability :: Looking for installation and/or custom design? :: Beautiful Wordpress themes

redjumpsuit

Re: Notify when ads are expiring

hi, you can echo the function and it should show you an array of the jobs that have been deactivated.

Code:

// deactivate jobs older than 30 days + notification
echo $janitor->DeactivateJobs();
will host your job board for you at minimal cost! read more: http://mim.io/acb992

evertsemeijn

Re: Notify when ads are expiring

Thank! Gonna try it out this weekend. Do you think it will help when I wear my jumpsuit too? wink

Member of Jobberbase Development Team - Templates/Usability :: Looking for installation and/or custom design? :: Beautiful Wordpress themes

PopaWoody

Re: Notify when ads are expiring

I tried to implement this With error reporting off on our live site, I assumed it was running. I checked late at night with error reporting on. I get an error in class.Maintenance.php that stated unexpected T_Variable in on line 36 ....the sql statement...select * from.....I took the $notifmin & $notifmax vars out and hard coded the values. Only to end up with the same error on another line

       $adname = trim($rows['title']);
   
Any ideas?

jobshouts.com A Jobberbased Derivative wink

redjumpsuit

Re: Notify when ads are expiring

hi,

thanks for your post. i just checked the code and notice there was a missing ";" at the end of the line

Code:

  $ademail = trim(strtolower($rows['poster_email']))

which is just before the line you were having errors at. simply add the ";" at the end of the line, like this:

Code:

  $ademail = trim(strtolower($rows['poster_email']));
will host your job board for you at minimal cost! read more: http://mim.io/acb992

PopaWoody

Re: Notify when ads are expiring

doh I can't believe i did not notice that the first time. must have been because it was late. I also just spotted another syntax issue. There is a close brace at the end of this statement

$sqlu = "UPDATE jobs SET is_active = 0 WHERE DATEDIFF(NOW(), created_on) > 30 AND is_active = 1 AND id = " . $rows['id']) ;

should read

$sqlu = "UPDATE jobs SET is_active = 0 WHERE DATEDIFF(NOW(), created_on) > 30 AND is_active = 1 AND id = " . $rows['id'] ;

This error is now prevalent

Warning: Invalid argument supplied for foreach() in /home/content/33/3945733/html/_includes/class.Maintenance.php on line 39

line 39 is

   foreach ( $row as $rows )

Last edited by PopaWoody (2009-02-25 15:23:35)

jobshouts.com A Jobberbased Derivative wink

Chronos

Re: Notify when ads are expiring

Could you post the entire block of code to see it in context?

Member of Jobberbase Development Team - Implementation and Coding

Visit my Blog: ChronoScripts (JobberBase scripts, support and freelance)
JobBoards: Telefonisch Werk and Top Bijbaan

redjumpsuit

Re: Notify when ads are expiring

i have this on production and is working fine, please update your code to this:

Code:


public function DeactivateJobs()
      {      

      $postMan = new Postman();

      global $db;

      $notifmin = 27; //the day when you want notification to be sent prior to the ad expiring
      $notifmax = 30; //the day the ad actually expires
      $dayscount = $notifmax - $notifmin;

      $sql = 'SELECT * FROM jobs WHERE DATEDIFF(NOW(), created_on) >= '. $notifmin .' AND is_active = 1';
      $row = $db->QueryArray($sql);
      print_r($row); // this will print the array of the ad that was deactivated
      foreach ( $row as $rows )
          {
          $ademail = trim(strtolower($rows['poster_email']));
          $adname = trim($rows['title']);
          $adcomp = trim($rows['company']);
          $adurl = BASE_URL . "job/" . $rows['id'] . "/";
          
          $sqlu = 'UPDATE jobs SET is_active = 0 WHERE DATEDIFF(NOW(), created_on) > '. $notifmax .' AND is_active = 1 AND id = ' . $rows['id'] ;
          $db->Execute($sqlu);
          $postMan->MailExpiringAd($ademail, $adname, $adcomp, $adurl, $dayscount);
          }
  }

will host your job board for you at minimal cost! read more: http://mim.io/acb992

evertsemeijn

Re: Notify when ads are expiring

Have the cron running tonight so lets hope it works...

Member of Jobberbase Development Team - Templates/Usability :: Looking for installation and/or custom design? :: Beautiful Wordpress themes

redjumpsuit

Re: Notify when ads are expiring

Please take note also of EMAIL_HEADER AND EMAIL_FOOTER on /_includes/class.Postman.php, you need constants like these to your /config.php, like this:

define('EMAIL_HEADER','Welcome to RedJumpsuit's Site!');
define('EMAIL_FOOTER','http://www.redjump.co.cc');

will host your job board for you at minimal cost! read more: http://mim.io/acb992

evertsemeijn

Re: Notify when ads are expiring

Corrected:

Code:
define('EMAIL_HEADER','Welcome to RedJumpsuit\'s Site!'); 
define('EMAIL_FOOTER','http://www.redjump.co.cc');
Member of Jobberbase Development Team - Templates/Usability :: Looking for installation and/or custom design? :: Beautiful Wordpress themes

redjumpsuit

Re: Notify when ads are expiring

Thanks for the edit! wink

will host your job board for you at minimal cost! read more: http://mim.io/acb992