OpenClose PHP class


OpenClose is a simply class to show the open/closed status (opening hours) of a store/shop

This class is a simple open/closed sign plugin, which can be used on any webpage showing the current status of a shop/store... The admin can change the status from open to close or vice versa by simply kliking a button od the admin page. Additionally opening hours can be set, and the script can automatically change the status accoring to these hours.No Database is required, the status is saved in a file on the server.

Features:

Examples:

Example 1: Rendering the admin view

This example shows, how to render the admin view


  // Example 1 - rendering the admin form  
     // initialize the object
     $status = new OpenClose('someCustomToken');
     // render the admin form
     $status->renderAdminForm();


Example 2: Showing the status on the website

In this example we simply show a message accorgding to the current status

  
  // Example 2 -  usage on the page
      // initialize the object       
      $status = new OpenClose();
      if($status->isOpen())
         echo "Our store is open";
      else
         echo "Our store is closed";

Example 3: Setting the opening hours

Here we set up some opening hours for Monday and Tuesday and afterwards the script checks if the store is open/closed and changes the status automatically.

  // Example 3 - setting opening hours and checking them
    $status = new OpenClose();
    $status->setOpenHours(array(
       "Mon"=>"11:00-14:00",
       "Tue"=>"09:00-14:00;23:00-23:15",
    ));
    $status->toggleOpeningHours();

 

To start usinf this clas only a few steps are needed

1. Include the OpenClose PHP class in your code

<?php
  require_once("OpenClose.class.php");
?>

2. Set up an admin page

<?
   /**
    *   set up the token (so that no one else can change the status)
    *   this string you have to use later to change the status
    *   if you save your file as "admin.php", you can call it similar to this:
    *   http://www.example.com/admin.php?token=security
    */
    $token = 'security'; // use this in the admin URL
    // initialize the object
    $status = new OpenClose($token);
    
    // render the admin form
    $status->renderAdminForm();
?>

3. Include the status in your own page

Here you have to repeat step 1, as this is a different page

<?
   require_once("OpenClose.class.php");
   // create an object instance
   $status = new OpenClose();
   
   // check for open/close
   if($status->isOpen()) {
      echo "It's open";
   } else {
      echo "It's closed";
   }
?>

For more advanced examples refer to the Advanced features.

 

 

 

 

 

 

 

 

 

 

 

 

 

1. Setting opening hours

It is possible to set the opening hours, which can be later used for output, or for automatically changing the status.

Some rules have to be followed:

- weekdays are 3 letters abbreviations, with an Uppercase first letter (Mon, Tue, Wed, Thu, Fri, Sat, Sun)
- time range format is: hh:mm-hh:mm (24-hour format)
- separate more times in one day with a semi-colon ;

 

<?php
    require_once("OpenClose.class.php");

    $status = new OpenClose();
    // this way we can add hours for a specific day of the week
    $status->addWeekDay("Sun","11:00-16:30;17:00-19:30");
    // this way we can add hours for more days at once
    $status->setOpenHours(array(
       "Mon"=>"11:00-14:00",
       "Tue"=>"09:00-14:00;23:00-23:15",
    ));


?>

2. Output of the opening hours

If we have set up some opening hours, we can show them on the website for the current day. We can customize the output format and the separator - if more time spans are active on this day

<?
    // separate individual times with a new line,and show times in 12-hour format
    echo $status->openToday("<br />","g:ia");
    //  separate individual times with comma, and show times in 24-hour format
    echo $status->openToday(", ","H:i");
?>

3. Automatically change the status according to opening hours

We can make the script check for opening hours and automatically change the status. This will also override the manaul setting! This check will happen on every page load.

<?
    // checks the opening hours and changes the status when needed
    $status->toggleOpeningHours();
?>

For a complete reference of all functions, check out the class reference Class reference.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Here you can find a detailed documentation of all the settings and features of this class.

 

Class Constant Summary
 CLOSED = 'closed'
 FILENAME = "status.txt"
 OPEN = 'open'
 TEXT_CLOSED_TODAY = 'Closed today'
Method Summary
OpenClose __construct ([mixed $token = "somePrivateToken"])
void addWeekDay (mixed $weekday, mixed $times)
void close ()
bool isClosed ()
bool isOpen ()
void open ()
opening openToday ([mixed $separator = "; "], [mixed $format = "H:i"])
void renderAdminForm ()
void setOpenHours (mixed $settings, [bool $replaceAll = false])
void status ()
void toggle ()
Methods
Constructor __construct (line 77)

The constructor. empties the variables. Set up the secroty token

  • access: public
OpenClose __construct ([mixed $token = "somePrivateToken"])
  • mixed $token: - your security token to be used to change the status online
addWeekDay (line 184)

add opening hours of a specific day of the week

  • access: public
void addWeekDay (mixed $weekday, mixed $times)
  • mixed $weekday
  • mixed $times
checkOpeningHours (line 241)

check if the status should be open according to the pre-set opening hours (not the manual setting)

  • return: self::OPEN or self::CLOSED, or boolean false, if no opening hours are set
  • access: public
mixed checkOpeningHours ()
close (line 128)

set current status to closed

  • access: public
void close ()
isClosed (line 154)

check if status is CLOSED

  • access: public
bool isClosed ()
isOpen (line 146)

check if status is OPEN

  • access: public
bool isOpen ()
open (line 119)

set current status to open

  • access: public
void open ()
openToday (line 218)

output of the current day's opening hours

  • return: hours of the current day
  • access: public
opening openToday ([mixed $separator = "; "], [mixed $format = "H:i"])
  • mixed $separator
  • mixed $format
renderAdminForm (line 162)

render the admin page form

  • access: public
void renderAdminForm ()
setOpenHours (line 201)

set open hours at once with an array of days and times

weekdays are 3 letters abbreviations, with an Uppercase first letter (Mon, Tue, Wed, Thu, Fri, Sat, Sun) time range format is: hh:mm-hh:mm separate more times in one day with a semi-colon ; array( 'Mon' => "9:00-18:00", 'Tue' => "9:00-12:00;15:00-18:00", )

  • access: public
void setOpenHours (mixed $settings, [bool $replaceAll = false])
  • mixed $settings
  • bool $replaceAll: - if true, all opening hours are replaced with new ones, otherwise hours are appended
status (line 137)

alias of @see getStatus()

  • access: public
void status ()
toggle (line 109)

toogle current status from open to closed or from closed to open

  • access: public
void toggle ()
toggleOpeningHours (line 265)

toggle the status according to the pre-set opening hours. This overrides the manual setting !

  • access: public
void toggleOpeningHours ()
Class Constants
CLOSED = 'closed' (line 62)

constant for the status 'CLOSED'

FILENAME = "status.txt" (line 70)

here you can change the filename, where the status is saved

OPEN = 'open' (line 58)

constant for the status 'OPEN'

TEXT_CLOSED_TODAY = 'Closed today' (line 66)

text shown when there are no opening hours for the current day

If you want to see the converter in action, check out the live demo:

View it on a page:

http://praca.webax.sk/open-close/index.php

Change the status as an admin:

http://praca.webax.sk/open-close/admin.php