public function newAction() { if ($this->view->identity === null) { throw new Www_Exception_Auth(); } if (!array_key_exists('applicationid', $_GET)) { $this->_redirect($this->view->url(array('id' => $this->view->identity->id), 'user')); } $applicationId = (int) $_GET['applicationid']; $applicationDb = new Shared_Db_Table_Application(); $application = $applicationDb->fetchRow(array('id = ?' => $applicationId)); if ($application === null) { throw new Www_Exception_NotFound(); } $this->view->application = $application; // form $form = new Www_Form_TemplateNew($application->id); if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { $values = (object) $form->getValues(); $templateModel = new Shared_Model_Template(); $templateModel->create($application->id, $values->name, $values->body, $values->privacy, $values->web_hooks); $this->_redirect($this->view->url(array('id' => $application->id), 'application')); } } $this->view->form = $form; }
public function viewAction() { if ($this->view->identity === null) { throw new Www_Exception_Auth(); } $id = $this->_getParam('id'); $userDb = new Shared_Db_Table_User(); $user = $userDb->fetchRow(array('id = ?' => $id)); if ($user === null) { throw new Www_Exception_NotFound(); } if ($user->id != $this->view->identity->id) { throw new Www_Exception_Access(); } $this->view->user = $user; // get this user's applications $applicationDb = new Shared_Db_Table_Application(); $select = $applicationDb->select()->setIntegrityCheck(false)->from('application')->joinLeft('template', 'application.id = template.application_id', array('template_count' => 'COUNT(*)'))->where('application.user_id = ?', $user->id)->group('application.id')->order('application.created DESC'); $this->view->applications = $applicationDb->fetchAll($select); }
public function viewAction() { if ($this->view->identity === null) { throw new Www_Exception_Auth(); } $id = $this->_getParam('id'); $applicationDb = new Shared_Db_Table_Application(); $application = $applicationDb->fetchRow(array('id = ?' => $id)); if ($application === null) { throw new Www_Exception_NotFound(); } if ($application->user_id != $this->view->identity->id) { throw new Www_Exception_Access(); } $this->view->application = $application; // get templates $templateDb = new Shared_Db_Table_Template(); $this->view->templates = $templateDb->fetchAll(array('application_id = ?' => $application->id), 'created DESC'); // get events $eventDb = new Shared_Db_Table_Event(); $select = $eventDb->select()->from('event')->joinLeft('template', 'event.template_id = template.id', array())->where('template.application_id = ?', $application->id)->order('event.created DESC'); $this->view->events = $eventDb->fetchAll($select); // form $form = new Www_Form_ApplicationEdit($application->id); if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { $values = (object) $form->getValues(); $applicationModel = new Shared_Model_Application(); $applicationModel->update($application->id, $values->privacy); $this->_redirect($this->view->url(array('id' => $application->id), 'application')); } } else { $form->populate(array('privacy' => $application->privacy)); } $this->view->form = $form; }
public function update($applicationId, $privacy) { $applicationDb = new Shared_Db_Table_Application(); try { $applicationDb->getAdapter()->beginTransaction(); $applicationId = $applicationDb->update(array('privacy' => (string) $privacy), $applicationDb->getAdapter()->quoteInto('id = ?', $applicationId)); $applicationDb->getAdapter()->commit(); } catch (Exception $e) { $applicationDb->getAdapter()->rollBack(); throw $e; } return $this; }
public function registerAction() { if (!$this->getRequest()->isPost()) { throw new Api_Exception(Api_Result::ERROR_REQUEST_METHOD); } $rawPost = file_get_contents('php://input'); $doc = new DOMDocument(); $result = @$doc->loadXML($rawPost); if ($result === false) { throw new Api_Exception(Api_Result::ERROR_XML_PARSE); } $xpath = new DOMXPath($doc); // get version $query = '/template[@version]'; $nodes = $xpath->query($query); if (sizeof($nodes) !== 1) { throw new Api_Exception(Api_Result::ERROR_XML_INVALID); } $version = $nodes->item(0)->getAttribute('version'); $config = Zend_Registry::get('config'); if ($version != $config->api->version) { throw new Api_Exception(Api_Result::ERROR_API_VERSION); } // get application ID $query = '/template/applicationId'; $nodes = $xpath->query($query); if (sizeof($nodes) !== 1) { throw new Api_Exception(Api_Result::ERROR_XML_INVALID); } $node = $nodes->item(0); $applicationId = (int) $node->nodeValue; $applicationDb = new Shared_Db_Table_Application(); $application = $applicationDb->fetchRow(array('id = ?' => $applicationId)); if ($application === null) { throw new Api_Exception(Api_Result::ERROR_APPLICATION_NOT_FOUND); } // get template name $query = '/template/name'; $nodes = $xpath->query($query); if (sizeof($nodes) !== 1) { throw new Api_Exception(Api_Result::ERROR_XML_INVALID); } $node = $nodes->item(0); $templateName = $node->nodeValue; // get template body $query = '/template/body'; $nodes = $xpath->query($query); if (sizeof($nodes) !== 1) { throw new Api_Exception(Api_Result::ERROR_XML_INVALID); } $node = $nodes->item(0); $templateBody = $node->nodeValue; // get template privacy $query = '/template/privacy'; $nodes = $xpath->query($query); if (sizeof($nodes) !== 1) { throw new Api_Exception(Api_Result::ERROR_XML_INVALID); } $node = $nodes->item(0); $templatePrivacy = $node->nodeValue; // get web hooks $query = '/template/webHooks'; $nodes = $xpath->query($query); if (sizeof($nodes) !== 1) { throw new Api_Exception(Api_Result::ERROR_XML_INVALID); } $node = $nodes->item(0); $templateWebhooks = $node->nodeValue; // create the template $templateModel = new Shared_Model_Template(); try { $templateId = $templateModel->create($applicationId, $templateName, $templateBody, $templatePrivacy, $templateWebhooks); } catch (Exception $e) { throw new Api_Exception(Api_Result::ERROR_DATABASE); } $this->getResponse()->setBody(Api_Result::xml(Api_Result::SUCCESS, $templateId)); $this->getResponse()->sendResponse(); die; }