public function viewAction() { if ($this->view->identity === null) { throw new Www_Exception_Auth(); } $id = $this->_getParam('id'); $templateDb = new Shared_Db_Table_Template(); $template = $templateDb->fetchRow(array('id = ?' => $id)); if ($template === null) { throw new Www_Exception_NotFound(); } $application = $template->findParentRow('Shared_Db_Table_Application'); if ($application->user_id != $this->view->identity->id) { throw new Www_Exception_Access(); } $this->view->application = $application; $this->view->template = $template; $form = new Www_Form_TemplateEdit($template->id); if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { $values = (object) $form->getValues(); $templateModel = new Shared_Model_Template(); $templateModel->update($template->id, $values->privacy, $values->web_hooks); $this->_redirect($this->view->url(array('id' => $template->id), 'template')); } } else { $form->populate(array('privacy' => $template->privacy, 'web_hooks' => $template->web_hooks)); } $this->view->form = $form; }
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; }