public function ajax_flushLog(CM_Params $params, CM_Frontend_JavascriptContainer $handler, CM_Http_Response_View_Ajax $response) { if (!$this->_getAllowedFlush($response->getRender()->getEnvironment())) { throw new CM_Exception_NotAllowed(); } $type = $params->getInt('type'); $logList = CM_Paging_Log_Abstract::factory($type); $logList->flush(); $response->reloadComponent(); }
public function ajax_getSuggestionByCoordinates(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { $lat = $params->getFloat('lat'); $lon = $params->getFloat('lon'); $location = CM_Model_Location::findByCoordinates($lat, $lon); $location = $this->_squashLocationInConstraints($location); if (!$location) { throw new CM_Exception('Cannot find a location by coordinates `' . $lat . '` / `' . $lon . '`.'); } return $this->getSuggestion($location, $response->getRender()); }
public function ajax_flushLog(CM_Params $params, CM_Frontend_JavascriptContainer $handler, CM_Http_Response_View_Ajax $response) { if (!$this->_getAllowedFlush($response->getRender()->getEnvironment())) { throw new CM_Exception_NotAllowed(); } $level = $params->has('level') ? $params->getInt('level') : null; $levelList = $level ? [$level] : null; $type = $params->has('type') ? $params->getInt('type') : null; $logList = new CM_Paging_Log($levelList, $type); $logList->flush(); $response->reloadComponent(); }
/** * @param CM_Params $params * @param CM_Http_Response_View_Ajax $response * @throws CM_Exception_Invalid * @return array */ public function loadPage(CM_Params $params, CM_Http_Response_View_Ajax $response) { $path = $params->getString('path'); $currentLayoutClass = $params->getString('currentLayout'); $request = $this->_createGetRequestWithUrl($path); $responseFactory = new CM_Http_ResponseFactory($this->getServiceManager()); $count = 0; $fragments = []; do { $fragment = CM_Util::link($request->getPath(), $request->getQuery()); $fragments[] = $fragment; $url = $this->getRender()->getSite()->getUrlBase() . $fragment; if ($count++ > 10) { throw new CM_Exception_Invalid('Page redirect loop detected (' . implode(' -> ', $fragments) . ').'); } $responsePage = $responseFactory->getResponse($request); if (!$responsePage->getSite()->equals($this->getSite())) { $redirectExternalFragment = CM_Util::link($responsePage->getRequest()->getPath(), $responsePage->getRequest()->getQuery()); return array('redirectExternal' => $responsePage->getRender()->getUrl($redirectExternalFragment)); } $responseEmbed = new CM_Http_Response_Page_Embed($responsePage->getRequest(), $responsePage->getSite(), $this->getServiceManager()); $responseEmbed->process(); $request = $responseEmbed->getRequest(); if ($redirectUrl = $responseEmbed->getRedirectUrl()) { if (!$this->_isPageOnSameSite($redirectUrl)) { return array('redirectExternal' => $redirectUrl); } } } while ($redirectUrl); foreach ($responseEmbed->getCookies() as $name => $cookieParameters) { $response->setCookie($name, $cookieParameters['value'], $cookieParameters['expire'], $cookieParameters['path']); } $page = $responseEmbed->getPage(); $this->_setStringRepresentation(get_class($page)); $frontend = $responseEmbed->getRender()->getGlobalResponse(); $html = $responseEmbed->getContent(); $js = $frontend->getJs(); $autoId = $frontend->getTreeRoot()->getValue()->getAutoId(); $frontend->clear(); $layoutRendering = null; $layoutClass = $page->getLayout($this->getRender()->getEnvironment()); if ($layoutClass !== $currentLayoutClass) { $layout = new $layoutClass(); $layoutRendering = $this->_getComponentRendering($layout); } $title = $responseEmbed->getTitle(); $menuList = array_merge($this->getSite()->getMenus($this->getRender()->getEnvironment()), $responseEmbed->getRender()->getMenuList()); $menuEntryHashList = $this->_getMenuEntryHashList($menuList, get_class($page), $page->getParams()); $jsTracking = $responseEmbed->getRender()->getServiceManager()->getTrackings()->getJs(); return ['pageRendering' => ['js' => $js, 'html' => $html, 'autoId' => $autoId], 'layoutRendering' => $layoutRendering, 'title' => $title, 'url' => $url, 'menuEntryHashList' => $menuEntryHashList, 'jsTracking' => $jsTracking]; }
public function ajax_validate(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { $data = $params->getArray('data'); $result = []; foreach ($data as $name => $userInput) { $field = $this->getField($name); $empty = $field->isEmpty($userInput); $value = null; $validationError = null; if (!$empty) { try { $value = $field->validate($response->getEnvironment(), $userInput); } catch (CM_Exception_FormFieldValidation $e) { $validationError = $e->getMessagePublic($response->getRender()); } } $result[$name] = ['value' => $value, 'empty' => $empty, 'validationError' => $validationError]; } return $result; }
/** * @param CM_Params $params * @param CM_Http_Response_View_Ajax $response * @throws CM_Exception_Invalid * @return array */ public function loadPage(CM_Params $params, CM_Http_Response_View_Ajax $response) { $request = new CM_Http_Request_Get($params->getString('path'), $this->getRequest()->getHeaders(), $this->getRequest()->getServer(), $this->getRequest()->getViewer()); $count = 0; $paths = array($request->getPath()); do { $url = $this->getRender()->getUrl(CM_Util::link($request->getPath(), $request->getQuery())); if ($count++ > 10) { throw new CM_Exception_Invalid('Page redirect loop detected (' . implode(' -> ', $paths) . ').'); } $responsePage = new CM_Http_Response_Page_Embed($request, $this->getServiceManager()); $responsePage->process(); $request = $responsePage->getRequest(); $paths[] = $request->getPath(); if ($redirectUrl = $responsePage->getRedirectUrl()) { $redirectExternal = 0 !== mb_stripos($redirectUrl, $this->getRender()->getUrl()); if ($redirectExternal) { return array('redirectExternal' => $redirectUrl); } } } while ($redirectUrl); foreach ($responsePage->getCookies() as $name => $cookieParameters) { $response->setCookie($name, $cookieParameters['value'], $cookieParameters['expire'], $cookieParameters['path']); } $page = $responsePage->getPage(); $this->_setStringRepresentation(get_class($page)); $frontend = $responsePage->getRender()->getGlobalResponse(); $html = $responsePage->getContent(); $js = $frontend->getJs(); $autoId = $frontend->getTreeRoot()->getValue()->getAutoId(); $frontend->clear(); $title = $responsePage->getTitle(); $layoutClass = get_class($page->getLayout($this->getRender()->getEnvironment())); $menuList = array_merge($this->getSite()->getMenus(), $responsePage->getRender()->getMenuList()); $menuEntryHashList = $this->_getMenuEntryHashList($menuList, get_class($page), $page->getParams()); $jsTracking = $responsePage->getRender()->getServiceManager()->getTrackings()->getJs(); return array('autoId' => $autoId, 'html' => $html, 'js' => $js, 'title' => $title, 'url' => $url, 'layoutClass' => $layoutClass, 'menuEntryHashList' => $menuEntryHashList, 'jsTracking' => $jsTracking); }
public function testNonexistentAjaxMethodException() { /** @var CM_View_Abstract|PHPUnit_Framework_MockObject_MockObject $view */ $view = $this->getMockForAbstractClass('CM_View_Abstract'); $site = CM_Site_Abstract::factory(); $request = $this->createRequestAjax($view, 'someReallyBadMethod', ['params' => 'foo']); $response = CM_Http_Response_View_Ajax::createFromRequest($request, $site, $this->getServiceManager()); $exception = $this->catchException(function () use($response) { $response->process(); }); $this->assertInstanceOf('CM_Exception_Invalid', $exception); /** @var CM_Exception_Invalid $exception */ $this->assertSame('Method not found', $exception->getMessage()); $this->assertSame(['method' => 'ajax_someReallyBadMethod'], $exception->getMetaInfo()); }
public function ajax_getSuggestions(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { return $this->_getSuggestions($params->getString('term'), $params->getArray('options'), $response->getRender()); }
public function testLoadPageTrackingError() { CM_Config::get()->CM_Http_Response_Page->exceptionsToCatch['CM_Exception_Nonexistent'] = ['errorPage' => 'CM_Page_View_Ajax_Test_Mock']; $page = new CM_Page_View_Ajax_Test_Mock(); $request = $this->createRequestAjax($page, 'loadPage', ['path' => '/iwhdfkjlsh']); $response = new CM_Http_Response_View_Ajax($request, $this->_getServiceManagerWithGA('ga123')); $response->process(); $this->assertViewResponseSuccess($response); $responseContent = CM_Params::decode($response->getContent(), true); $jsTracking = $responseContent['success']['data']['jsTracking']; $html = $responseContent['success']['data']['html']; $this->assertSame(1, substr_count($jsTracking, 'ga("send", "pageview"')); $this->assertContains('ga("send", "pageview", "\\/iwhdfkjlsh")', $jsTracking); $this->assertContains('CM_Page_View_Ajax_Test_Mock', $html); }
public function ajax_ping(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { $number = $params->getInt('number'); self::stream($response->getViewer(true), 'ping', array("number" => $number, "message" => 'pong')); }
/** * @param CM_View_Abstract $view * @param string $methodName * @param array|null $params * @param CM_Frontend_Environment|null $environment * @return CM_Http_Response_View_Ajax */ public function getResponseAjax(CM_View_Abstract $view, $methodName, array $params = null, CM_Frontend_Environment $environment = null) { $site = CM_Site_Abstract::factory(); $request = $this->createRequestAjax($view, $methodName, $params, null, null, $site); if ($environment) { $request->mockMethod('getViewer')->set(function () use($environment) { return $environment->getViewer(); }); } $response = CM_Http_Response_View_Ajax::createFromRequest($request, $site, $this->getServiceManager()); $response->process(); return $response; }
public function ajax_validate(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { $environment = $response->getRender()->getEnvironment(); $userInput = $params->get('userInput'); $this->validate($environment, $userInput); }
public function ajax_reloadComponent(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { $response->reloadComponent($params->getParamsDecoded()); }
public function ajax_loadPage(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) { return $response->loadPage($params, $response); }
public function testLoadPageTrackingError() { CM_Config::get()->CM_Http_Response_Page->exceptionsToCatch['CM_Exception_Nonexistent'] = ['errorPage' => 'CM_Page_View_Ajax_Test_Mock']; $site = CM_Site_Abstract::factory(); $page = new CM_Page_View_Ajax_Test_Mock(); $env = new CM_Frontend_Environment($site, CMTest_TH::createUser()); $params = ['path' => '/iwhdfkjlsh', 'currentLayout' => $page->getLayout($env)]; $request = $this->createRequestAjax($page, 'loadPage', $params); $response = CM_Http_Response_View_Ajax::createFromRequest($request, $site, $this->_getServiceManagerWithGA('ga123')); $response->process(); $this->assertViewResponseSuccess($response); $responseContent = CM_Params::decode($response->getContent(), true); $jsTracking = $responseContent['success']['data']['jsTracking']; $html = $responseContent['success']['data']['pageRendering']['html']; $this->assertSame(1, substr_count($jsTracking, 'ga("send", "pageview"')); $this->assertContains('ga("send", "pageview", "\\/iwhdfkjlsh")', $jsTracking); $this->assertContains('CM_Page_View_Ajax_Test_Mock', $html); }