Schedule and Datetime module for Phalcon Framework
In my projects the application logic often depends on the time. For example some forms can be avaible in specified period or some views need to change after competition. Moreover projects managers and testers woud like to change application time. This modules can do this in an easy way. For each enviroment developer can set custom datetime.
===
Copy the main folder into your project where you keep libraries or modules. For example (libs/ or app/vendor or /vendor). In \Phalcon\Loader() register new namespace 'Modules'.
in index.php set global var with your enviroment
define('APP_ENVIROMENT', getenv('AppEnviroment'));
or if your enviroment in VirtualHost is not set:
define('APP_ENVIROMENT', 'development');
add datetime configuration in your global or module config file
/**
* Config for datetime
*/
'datetime' => array(
'production' => 'normal',
'staging' => 'normal',
'testing' => 'normal',
'development' => '2014-02-01 15:45:00'
),
in your Bootstrap file or in index.php you can add datetime to your DI
$config = $this->getConfig();
$this->getDi()->set('datetime', function () use ($config) {
return new \Modules\Datetime($config->datetime);
}, true);
in any file where DI is included
public function indexAction() {
$datetime = $this->getDi()->get('datetime')->datetime();
}
===
run schedule.sql file into your MySql database.
add schedule to DI
$this->getDi()->set('schedule', function () {
return new \Modules\Schedule();
}, true);
Each of schedule point has start and end time, so it can have one of three status:
- before
- active
- after
Also each schedule point has it's own type so we can configurate many points with different types. Class \Modules\Schedule has two major methods:
-
getByType($type) - get all schedule points with the same type
-
getByDate($datetime) - get all schedule points (with all types) where start >= $datetie <= end
when we choose interesting schedule points we can move between them using methods:
-
getFirst()
-
getNext()
-
getPrevious()
-
getLast()
-
getCurrent()
-
getActive() return point which status is 'active'
Insert testing values
-- ----------------------------
-- Records of `schedule`
-- ----------------------------
BEGIN;
INSERT INTO `schedule` VALUES
('1', 'competition', 'photo-upload', '2014-07-22 00:00:00', '2014-07-22 23:59:59'),
('2', 'competition', 'moderator-accept', '2014-07-23 00:00:00', '2014-07-31 23:59:59'),
('3', 'competition', 'voting', '2014-08-01 00:00:00', '2014-08-16 23:59:59'),
('4', 'promoCode', 'show sales codes', '2014-08-01 00:00:00', '2014-08-01 20:00:00');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
Check promo code time
$schedule = $this->getDi()->get('schedule');
$showPromoCodes = $schedule->getByType('promoCode')->getFirst()->isActive();
Voting schedule validator
...
$schedule = $this->getDi()->get('schedule');
$votingSchedulePoint = $schedule->getByType('competition')->getLast();
if($votingSchedulePoint->isBefore())
$message = "You aren't able to vote before: ".$votingSchedulePoint->getStart();
if($votingSchedulePoint->isActive())
$message = "Thanks for voting";
if($votingSchedulePoint->isAfter())
$message = "Voting time is over";
...
If you want to test the application you just need to change datetime configuration for your enviroment.
I encourage you to clone repo and test it :)