コード例 #1
0
ファイル: PageInjectWidget.php プロジェクト: esase/dream-cms
 /**
  * Call widget
  *
  * @param string $position
  * @param integer $pageId
  * @param integer $userRole
  * @param array $widgetInfo
  * @param boolean $useLayout
  * @throws \Page\Exception\PageException
  * @return string|boolean
  */
 protected function callWidget($position, $pageId, $userRole, array $widgetInfo, $useLayout = true)
 {
     // don't call any widgets
     if (true === self::$widgetRedirected) {
         return false;
     }
     // check a widget visibility
     if ($userRole != AclBaseModel::DEFAULT_ROLE_ADMIN) {
         if (!empty($widgetInfo['hidden']) && in_array($userRole, $widgetInfo['hidden'])) {
             return false;
         }
     }
     // call the widget
     $widget = $this->getView()->{$widgetInfo['widget_name']}();
     // check the widget
     if (!$widget instanceof IPageWidget) {
         throw new PageException(sprintf($widgetInfo['widget_name'] . ' must be an object implementing IPageWidget'));
     }
     // init the widget
     $widget->setPageId($pageId)->setWidgetPosition($position)->setWidgetConnectionId($widgetInfo['widget_connection_id']);
     $widgetCacheName = null;
     if ((int) $widgetInfo['widget_cache_ttl']) {
         // generate a cache name
         $widgetCacheName = CacheUtility::getCacheName($widgetInfo['widget_name'], [$widgetInfo['widget_connection_id']]);
         // check the widget data in a cache
         if (null !== ($cachedWidgetData = $this->dynamicCache->getItem($widgetCacheName))) {
             // check a local widget lifetime
             if ($cachedWidgetData['widget_expire'] >= time()) {
                 // include widget's css and js files
                 if (false !== $cachedWidgetData['widget_content'] && !$this->request->isXmlHttpRequest()) {
                     $widget->includeJsCssFiles();
                 }
                 return $cachedWidgetData['widget_content'];
             }
             // clear cache
             $this->dynamicCache->removeItem($widgetCacheName);
         }
     }
     if (false !== ($widgetContent = $widget->getContent())) {
         self::$widgetRedirected = $widget->isWidgetRedirected();
         // include widget's css and js files
         if (!$this->request->isXmlHttpRequest()) {
             $widget->includeJsCssFiles();
         }
         // add the widget's layout
         if ($useLayout) {
             if (!empty($widgetInfo['widget_layout'])) {
                 $widgetContent = $this->getView()->partial($this->layoutPath . $widgetInfo['widget_layout'], ['title' => $this->getView()->pageWidgetTitle($widgetInfo), 'content' => $widgetContent]);
             } else {
                 $widgetContent = $this->getView()->partial($this->layoutPath . 'default', ['title' => $this->getView()->pageWidgetTitle($widgetInfo), 'content' => $widgetContent]);
             }
         }
     }
     // cache the widget data
     if ($widgetCacheName) {
         $this->dynamicCache->setItem($widgetCacheName, ['widget_content' => $widgetContent, 'widget_expire' => time() + $widgetInfo['widget_cache_ttl']]);
     }
     return $widgetContent;
 }
コード例 #2
0
 /**
  * @param \Zend\Http\PhpEnvironment\Request           $request
  * @param \Phpro\SmartCrud\Service\SmartServiceResult $SmartServiceResult
  * @param \StdClass                                   $entity
  */
 public function it_should_build_a_json_model_when_request_is_xml_http_request($request, $SmartServiceResult)
 {
     $request->isXmlHttpRequest()->willReturn(true);
     $this->build($request, $SmartServiceResult, 'create')->shouldBeAnInstanceOf('\\Zend\\View\\Model\\JsonModel');
 }