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
<?php require_once("OpenClose.class.php"); ?>
<? /** * 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(); ?>
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.
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", )); ?>
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"); ?>
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.
The constructor. empties the variables. Set up the secroty token
add opening hours of a specific day of the week
check if the status should be open according to the pre-set opening hours (not the manual setting)
set current status to closed
check if status is CLOSED
check if status is OPEN
set current status to open
output of the current day's opening hours
render the admin page form
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", )
alias of @see getStatus()
toogle current status from open to closed or from closed to open
toggle the status according to the pre-set opening hours. This overrides the manual setting !
constant for the status 'CLOSED'
here you can change the filename, where the status is saved
constant for the status 'OPEN'
text shown when there are no opening hours for the current day
View it on a page:
http://praca.webax.sk/open-close/index.php
Change the status as an admin: