* @package WB * @subpackage base */ WBClass::load('WBDatasource' ); /** * WBEvent_Handler_Contact_Saver * * Store contact data * * @version 0.1.0 * @package Wombat * @subpackage base */ class WBDatasource_ContactRequest extends WBStdClass { /** * @var WBDatasource_Table */ private $table; /** * Current Mandator * * @var WBMandator */ private $mandator; /** * Current Namespace * @var string */ private $namespace = 'default'; /** * Current Data * @var array */ private $data = array(); /** * Current Id * @var string */ private $id = ''; /** * Constructor * * @param array */ public function __construct($parameter = array()) { if (empty($parameter['table'])) { $parameter['table'] = WBClass::create('WBDatasource_Table', $parameter); } if (empty($parameter['mandator'])) { $parameter['mandator'] = WBClass::create('WBMandator', $parameter); } $this->table = $parameter['table']; $this->mandator = $parameter['mandator']; } /** * Get Mandator * * @return WBMandator */ public function getMandator() { return $this->mandator; } /** * Load Record by Id * * @param string */ public function load($id) { if ($this->id == $id) { return; } $this->id = ''; $this->data = array(); $id = trim($id); if (empty($id)) { return; } if ($this->mandator->isEnabled()) { if (strstr($id, '-')) { $id = explode('-', $id); // verify mandatorid if (!$this->mandator->isMaster() && $id[0] != $this->mandator->getId()) { return; } $id = implode('-', $id); } else { $id = $this->mandator->getId() . '-' . $id; } } $data = $this->table->get(WBDatasource::TABLE_CONTACTREQUEST, $id); if (empty($data)) { return; } $data = $data[0]; $this->id = $data['id']; $this->namespace = $data['namespace']; $req = json_decode($data['request'], true); if (!is_array($req)) { $req = array(); } $this->data = array_merge($req, $data); } /** * Current Data OK * * Check if record is loaded * @return bool */ public function isOK() { if (empty($this->id)) { return false; } return true; } /** * Current Request Read? * * Check if currently loaded contact request is marked as read * @return bool */ public function isRead() { if (!$this->isOK()) { return false; } if ('1970-01-01 00:00:00' == $this->data['read']) { return false; } return true; } /** * Current Request Done? * * Check if currently loaded contact request is marked as done * @return bool */ public function isDone() { if (!$this->isOK()) { return false; } if ('1970-01-01 00:00:00' == $this->data['done']) { return false; } return true; } /** * Mark Request as Read * * */ public function markAsDone() { if (!$this->isOK()) { return; } // already done if ('1970-01-01 00:00:00' != $this->data['done']) { return; } $data = array( 'done' => date('Y-m-d H:i:s') ); return $this->save($data); } /** * Mark Request as Read * * */ public function markAsRead() { if (!$this->isOK()) { return; } // already done if ('1970-01-01 00:00:00' != $this->data['read']) { return; } $data = array( 'read' => date('Y-m-d H:i:s') ); return $this->save($data); } /** * Mark Request as SPAN * */ public function markAsSpam() { if (!$this->isOK()) { return; } // already done if (0 < $this->data['spam']) { return; } $data = array( 'spam' => 1 ); // also mark as read if ('1970-01-01 00:00:00' == $this->data['read']) { $data['read'] = date('Y-m-d H:i:s'); } return $this->save($data); } /** * Mark Request as HAM, not SPAM * */ public function markAsHam() { if (!$this->isOK()) { return; } // already done if (1 > $this->data['spam']) { return; } $data = array( 'spam' => 0 ); return $this->save($data); } private function save($data) { $this->table->save(WBDatasource::TABLE_CONTACTREQUEST, $this->id, $data); $this->data = array_merge($this->data, $data); } /** * Set Namespace * * @param string */ public function setNamespace($ns = '') { $this->namespace = $ns; } /** * Get Request Pager * * @param array * @param array * @return WBDatasource_Pager */ public function getPager($clause = array(), $option = array()) { $foreign = null; if (!is_array($clause)) { $clause = array(); } if (!is_array($option)) { $option = array(); } $this->mandator->inject2Clause($clause); $pager = $this->table->getPager(WBDatasource::TABLE_CONTACTREQUEST . '-pager', WBDatasource::TABLE_CONTACTREQUEST, $foreign, $clause, $option); return $pager; } /** * Add Any Contact Request * * * @param array * @return stirng */ public function add($data) { $rest = array(); $save = $this->prepareSave($data, $rest); if (empty($save)) { return ''; } $this->mandator->inject2Data($save); $save['namespace'] = $this->namespace; $save['request'] = json_encode($rest); $this->id = $this->table->save(WBDatasource::TABLE_CONTACTREQUEST, '__new', $save); $this->data = array_merge($data, $save); return $this->id; } /** * Prepare Savable Data * * Empty result if data is not sufficient * * @param array * @param array * @return array */ private function prepareSave($data, &$rest) { $save = array(); if (!empty($data['emaillocal']) && !empty($data['emaildomain']) && empty($data['email'])) { $data['email'] = sprintf('%s@%s', $data['emaillocal'], $data['emaildomain']); } $required = array( 'email', 'message' ); foreach ($required as $k) { if (empty($data[$k])) { return $save; } } // need to know creation time if (empty($data['created'])) { $data['created'] = date('Y-m-d H:i:s'); } $copy = array( $this->mandator->getIdentifier(), 'xid', 'lang', 'email', 'phone', 'message', 'created', 'forename', 'surname', $this->table->getIdentifier(WBDatasource::TABLE_USER, true), $this->table->getIdentifier(WBDatasource::TABLE_PERSONGENDER, true), 'company', 'title' ); foreach ($data as $k => $v) { if (in_array($k, $copy)) { $save[$k] = $v; continue; } $rest[$k] = $v; } return $save; } }