* @package WB * @subpackage Table */ WBClass::load('WBDatasource_XReference'); /** * Public Holidays * * @version 0.2.0 * @package WB * @subpackage db */ class WBDatasource_Holiday extends WBStdClass { /** * Table access * @var WBDatasource_Table */ private $table; /** * Current Date String * @var array */ private $date = ''; /** * Current List of Holidays * @var array */ private $list = array(); const TABLE_HOLIDAY = 'holiday'; /** * Fuzzy * Use fuzzy date for select * @var int */ private $fuzzy = 1; /** * Constructor * * Parameter: * * @param array $parameter */ public function __construct($parameter = array()) { $this->table = WBClass::create('WBDatasource_Table', $parameter); } /** * Unload Holiday * */ private function flush() { $this->date = ''; $this->list = array(); } /** * Save Public Holiday * * @param array $data * @return string holiday id */ public function save($data) { $old = $this->getByDate($data['holiday']); if (empty($old)) { return $this->table->save(self::TABLE_HOLIDAY, '__new', $data); } return $old[0]['id']; } /** * Get Holiday Data List * * @param string * @return array */ private function getByDate($date) { $this->loadByDate($date); return $this->list; } /** * Load Holiday Data * * @param string * @return int */ public function loadByDate($date) { if ($this->date == $date) { return count($this->list); } $this->flush(); $this->date = $date; $clause = array(); $clause[] = array( 'field' => 'holiday', 'value' => $date ); if (1 > $this->fuzzy) { $this->list = $this->table->get(self::TABLE_HOLIDAY, null, null, $clause); return $this->list; } $clause[0]['relation'] = 'ge'; $date = strtotime($date) + 24 * 3600 * $this->fuzzy; $date = date('Y-m-d 00:00:00', $date); $clause[] = array( 'field' => 'holiday', 'relation' => 'le', 'value' => $date ); $this->list = $this->table->get(self::TABLE_HOLIDAY, null, null, $clause); return count($this->list); } /** * Check Date for Holiday * * @param string date * @return bool true of date is a holiday */ public function isHoliday($date = null) { if (empty($date)) { $date = time(); } else { $date = strtotime($date); } $date = gmdate('Y-m-d', $date); $cnt = $this->loadByDate($date); if (0 < $cnt) { return true; } return false; } /** * Get Date * * @return string */ public function getDate() { return $this->date; } /** * Get Title * * @return string */ public function getTitle() { return $this->getColumn('title'); } /** * Get Blurb * * @return string */ public function getBlurb() { return $this->getColumn('blurb'); } /** * Get Column String * * @return string */ private function getColumn($column = 'title') { if (empty($this->list)) { return ''; } return trim($this->list[0][$column]); } }