* @license PHP License * @package WB */ WBClass::load('WBDatasource' , 'WBDatasource_Table'); /** * Mandator * * @version 0.9.0 * @package WB */ class WBMandator extends WBStdClass { const TRADENAME_ZERO = 'z'; const MASTER_ID = 0; const INVALID_ID = -1; const PAYMENT_INVOICE = 'invoice'; const PAYMENT_CHECKOUT = 'checkout'; const AREA_CART = 'cart'; const AREA_PARTICIPANT = 'participant'; /** * Current Global Mandator Id * @var string */ private static $mandatorId = self::INVALID_ID; /** * Current Local Mandator Id * @var string */ private $mandatorIdLocal = self::INVALID_ID; /** * Table Access * @var WBDatasource_Table */ private static $table; /** * Is Mandator Feature enabled? * * Zero or one for disabled and enabled. Initially this member variable is -1 * to indicate, that it as never set. * @var integer */ private static $enabled = -1; /** * Current Mandator Data * * @var array */ private $data; /** * Construtor * * All parameter are optional, accept parameter: * - table: inject table object * * @param array parameter list */ public function __construct($parameter = array()) { if (0 > self::$enabled) { /** @var WBConfig */ $config = WBClass::create('WBConfig'); $config->load('config'); $conf = $config->get('db/mandator', 0); if (0 < $conf) { $conf = 1; } else { $conf = 0; } self::$enabled = $conf; } if (1 > self::$enabled) { return; } if (empty(self::$table)) { if (empty($parameter['table'])) { $parameter['table'] = WBClass::create('WBDatasource_Table'); } self::$table = $parameter['table']; } if (self::MASTER_ID > self::$mandatorId) { $sess = WBClass::create('patSession'); self::$mandatorId = $sess->get('wb.user.auth.mandatorid'); if (empty(self::$mandatorId)) { self::$mandatorId = self::MASTER_ID; } } $this->mandatorIdLocal = self::$mandatorId; } /** * Is Mandator Feature Enalbed * * @return bool */ public function isEnabled() { if (1 > self::$enabled) { return false; } return true; } /** * Is Mandator Master * * @return bool */ public function isMaster() { if (1 > self::$enabled) { return false; } if (self::MASTER_ID == self::$mandatorId) { return true; } return false; } /** * Check Wheather Using Own Checkout * * Load master's checkout config * @param string payment * @param string area * @return bool true if own, false if master */ public function useOwnCheckout() { $data = $this->get(); return (0 < $data['useownpayment']); } /** * Check Wheather Using Own * * Use constants for parameter payment and area * @param string payment * @param string area * @return bool true if own, false if master */ public function useOwn($payment = self::PAYMENT_INVOICE, $area = self::AREA_CART) { if (1 > self::$enabled) { return false; } $data = $this->get(); $flag = sprintf('useown%s4%s', $payment, $area); return (0 < $data[$flag]); } /** * Get Mandator Id Of Master * * @return string */ public function getMasterId() { return self::MASTER_ID; } /** * Check if Current Mandator is Valid * * Mandator data must be loaded and enabled * * @param bool local or global mandator * @return string */ public function isValid($local = true) { $id = $this->getId($local); if (self::INVALID_ID == $id) { return false; } $data = $this->get($local); if (empty($data)) { return false; } if (empty($data['enabled'])) { return false; } return true; } /** * Get Current Mandator Id * * @param bool local or global mandator * @return string */ public function getId($local = true) { if (1 > self::$enabled) { return self::MASTER_ID; } if ($local) { return $this->mandatorIdLocal; } return self::$mandatorId; } /** * Check if Current Mandator Provides Newsletter * * Mandator data must be valid and newsletter flag must be enabled * * @param bool local or global mandator * @return bool */ public function hasNewsletter($local = true) { if (!$this->isValid($local)) { return false; } $data = $this->get(); return (0 < $data['newsletter']); } /** * Set Current Mandator Id * * @param string mandator id * @param bool local or global mandator */ public function setId($id, $local = true) { if ($id != $this->mandatorIdLocal) { $this->data = array(); } $this->mandatorIdLocal = $id; if ($local) { return; } self::$mandatorId = $id; } /** * Get Mandator Data * * Load if not there yet * @param bool local or global mandator * @return array */ public function get($local = true) { if (!empty($this->data)) { return $this->data; } $id = self::$mandatorId; if ($local) { $id = $this->mandatorIdLocal; } if (self::INVALID_ID == $id) { return array(); } if (self::MASTER_ID == $id) { return $this->getMaster(); } $this->loadById($id); return $this->data; } /** * Get Master Mandator * * @return array */ public function getMaster() { $customerid = 1; $config = WBClass::create('WBConfig'); $config->load('info'); $customerid = $config->get('general/customer', $customerid); return array( 'id' => 0, $this->getIdentifier() => 0, 'enabled' => 1, 'customer' => $config->get('general/name', 'Master'), 'tradename' => self::TRADENAME_ZERO, 'customerid' => $customerid, 'useowninvoice4cart' => 1, 'useowninvoice4participant' => 1, 'useowncheckout4cart' => 1, 'useowncheckout4participant'=> 1, 'useownpayment' => 1, 'newsletter' => 1 ); } /** * Load Mandator By Id or Tradename * * @see loadByRaw() * @param string * @param bool local or global mandator * @return bool true on success */ public function loadByIdOrTradename($thing, $local = true) { if (preg_match('/^\\d+$/', $thing)) { return $this->loadById($thing, $local); } return $this->loadByTradename($thing, $local); } /** * Load Mandator By Tradename * * @see loadByRaw() * @param string * @param bool local or global mandator * @return bool true on success */ public function loadByTradename($name, $local = true) { $name = trim(strtolower($name)); if (self::TRADENAME_ZERO == $name) { $this->setId(self::MASTER_ID, $local); return true; } if (empty($name)) { $this->data = array(); return false; } if (!empty($this->data) && $name == strtolower($this->data['tradename'])) { return true; } $clause = array(); $clause[] = array( 'field' => 'tradename', 'value' => $name ); $this->startTable(); $data = self::$table->get(WBDatasource::TABLE_MANDATOR, null, null, $clause); if (1 == count($data)) { $data = $data[0]; } return $this->loadByRaw($data); } /** * Load Mandator By Id * * @see loadByRaw() * @param string * @param bool local or global mandator * @return bool true on success */ private function loadById($id, $local = true) { if (self::MASTER_ID == $id) { $this->setId(self::MASTER_ID, $local); return true; } $data = self::$table->get(WBDatasource::TABLE_MANDATOR, $id); if (1 == count($data)) { $data = $data[0]; } return $this->loadByRaw($data); } /** * Load Product By Raw Data * * @param array * @param bool local or global mandator * @return bool true on success */ private function loadByRaw($data, $local = true) { $this->data = array(); if (empty($data)) { return false; } $this->data = $data; if ($this->isEnabled()) { if (empty($data['id'])) { $data['id'] = 0; } $this->setId($data['id'], $local); } return true; } /** * Fetch Mandator Data By Id * * @param string mandator id * @return array */ public function fetchById($id) { if (self::MASTER_ID == $id) { return $this->getMaster(); } if (0 > $id || empty($id)) { return array(); } $data = self::$table->get(WBDatasource::TABLE_MANDATOR, $id); if (1 != count($data)) { return array(); } $data = $data[0]; return $data; } /** * Get Primary Key of Table * * By default, get primary key of mandator table, if table name is specified * use this table. Just a convenient function. * * @see WBDatasource_Table::getIdentifier() * @param string table name * @param string bool * @return string */ public function getIdentifier($table = WBDatasource::TABLE_MANDATOR, $asString = false) { $this->startTable(); return self::$table->getIdentifier($table, $asString); } /** * Inject Mandator Id * * In case mandator feature is enabled, inject current id * * @see isEnabled() * @param array data * @return bool true if mandator is enabled */ public function inject2Data(&$data) { if (!$this->isEnabled()) { return false; } if (!is_array($data)) { $data = array(); } $key = $this->getIdentifier(); $id = $this->getId(); // add id, if not there if (empty($data[$key])) { $data[$key] = $id; return true; } // master is allowed to act on behalf ot others if (self::MASTER_ID == $id) { return true; } $data[$key] = $id; return true; } /** * Inject Check for Enabled Mandator * * Build clause and join to allow master mandator or mandators that are enabled * * @see isEnabled() * @param array clause list * @param array option list * @return bool true if mandator is enabled */ public function injectEnabled2Clause(&$clause, &$option = array()) { if (!$this->isEnabled()) { return false; } if (!is_array($clause)) { $clause = array(); } $c = array(); $c[] = array( 'table' => WBDatasource::TABLE_MANDATOR, 'field' => 'enabled', 'value' => 1 ); $info = self::$table->getTableInfo(WBDatasource::TABLE_MANDATOR); if (isset($info['delete']) && !empty($info['delete'])) { $c[] = array( 'table' => WBDatasource::TABLE_MANDATOR, 'field' => $info['delete'], 'value' => 0 ); $tmp = array( 'type' => 'complex', 'bond' => 'and', 'clause' => $c ); $c = array(); $c[] = $tmp; } $c[] = array( 'table' => WBDatasource::TABLE_MANDATOR, 'field' => $this->getIdentifier(), 'relation' => 'null', 'value' => 1 ); $clause[] = array( 'type' => 'complex', 'bond' => 'or', 'clause' => $c ); if (!is_array($option)) { $option = array(); } if (empty($option['join']) || !is_array($option['join'])) { $option['join'] = array(); } $option['join'][] = array( 'table' => WBDatasource::TABLE_MANDATOR, 'left' => 1, 'using' => $this->getIdentifier() ); return true; } /** * Inject Master Mandator Key * * In case mandator feature is enabled, inject clause and return true. * * @see doInject2Clause() * @see isEnabled() * @param array clause list * @param string table name * @return bool true if mandator is enabled */ public function injectMaster2Clause(&$clause, $table = null) { return $this->doInject2Clause($clause, $this->getMasterId(), $table); } /** * Inject Mandator Key * * In case mandator feature is enabled, inject clause and return true. * * @see doInject2Clause() * @see isEnabled() * @param array clause list * @param string table name * @return bool true if mandator is enabled */ public function inject2Clause(&$clause, $table = null) { return $this->doInject2Clause($clause, $this->getId(), $table); } /** * Inject Mandator Key * * In case mandator feature is enabled, inject clause and return true. * * @see isEnabled() * @param array clause list * @param string mandator id * @param string table name * @return bool true if mandator is enabled */ private function doInject2Clause(&$clause, $id, $table = null) { if (!$this->isEnabled()) { return false; } if (empty($id)) { $id = self::MASTER_ID; } $c = array( 'field' => $this->getIdentifier(), 'value' => $id ); if (!empty($table)) { $c['table'] = $table; } if (!is_array($clause)) { $clause = array(); } $clause[] = $c; return true; } /** * Get Pager 2 List all Mandators * * @param bool only enabled * @parm bool only visible * @return WBDatasource_Pager */ public function getPager($enabled = true, $visible = false) { $option = array(); $option['limit'] = 5; $clause = array(); if ($enabled) { $clause[] = array( 'field' => 'enabled', 'value' => 1 ); } if ($visible) { $clause[] = array( 'field' => 'visible', 'value' => 1 ); } $this->startTable(); $id = WBDatasource::TABLE_MANDATOR . '-list'; return self::$table->getPager($id, WBDatasource::TABLE_MANDATOR, false, $clause, $option); } /** * List All Mandators That are Visible * * Load up to 100 records from database * * @param bool enabled * @return array */ public function getVisible($enabled = true) { $option = array(); $option['limit'] = 100; $clause = array(); $clause[] = array( 'field' => 'visible', 'value' => 1 ); if ($enabled) { $clause[] = array( 'field' => 'enabled', 'value' => 1 ); } return self::$table->get(WBDatasource::TABLE_MANDATOR, null, null, $clause, $option); } /** * Get All Mandator Records * * Load up to 100 records from database * * @deprecated in favour of getPager() * @return array */ public function getAll() { $clause = array(); $option = array( 'limit' => 100 ); return self::$table->get(WBDatasource::TABLE_MANDATOR, null, null, $clause, $option); } /** * Initialize Table Access * * On demand object creation. Makes sure, that static table object * will be created only once. */ private function startTable() { if (self::$table) { return; } self::$table = WBClass::create('WBDatasource_Table'); } }