* @license PHP License * @package WB * @subpackage Tag */ //WBClass::load('WBDatasource_XReference'); /** * Public Holidays * * @version 0.1.0 * @package WB * @subpackage db */ class WBDatasource_Calendar_Holiday extends WBStdClass { /** * Table access * @var WBDatasource_Table */ private $table; /** * Table Micro Cache * @var WBDatasource_TableMicroCache */ private $cache; const TABLE_HOLIDAY = 'holiday'; private $clause = array(); /** * Constructor * * Parameter: * * @param array $parameter */ public function __construct( $parameter = array() ) { $this->table = WBClass::create('WBDatasource_Table'); $this->cache = WBClass::create('WBDatasource_TableMicroCache', array('name' => __CLASS__)); $this->cache->setTable(self::TABLE_HOLIDAY); } public function useCountry($country, $region = '') { $this->clause = array(); $this->cache->flushAll(__CLASS__); // any country if (empty($country)) { return; } // limit country $this->clause[] = array( 'field' => 'country', 'value' => $country ); if (empty($region)) { return; } // limit region to national and region $tmp = array(); $tmp[] = array( 'field' => 'region', 'value' => $region ); $tmp[] = array( 'field' => 'region', 'value' => '' ); $this->clause[] = array( 'type' => 'complex', 'clause' => $tmp, 'bond' => 'OR' ); } /** * 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']; } private function getByDate($date = '') { if (empty($date)) { $date = time(); } else { $date = strtotime($date); } $date = gmdate('Y-m-d', $date); $list = $this->cache->get($date); if (!empty($list)) { return $list; } $clause = $this->clause; $clause[] = array( 'field' => 'holiday', 'value' => $date ); $list = $this->table->get(self::TABLE_HOLIDAY, null, null, $clause); $this->cache->add($date, $list); return $list; } public function getTitle($date = null) { $old = $this->getByDate($date); if (!count($old)) { return ''; } return $old[0]['title']; } public function getBrief($date = null) { $old = $this->getByDate($date); if (!count($old)) { return ''; } return $old[0]['brief']; } /** * Check Date for Holiday * * @param string date * @return bool true of date is a holiday */ public function isHoliday($date = null) { $old = $this->getByDate($date); if (count($old)) { return true; } return false; } }