private function search(OrmManager $orm, JsonApiQuery $documentQuery)
 {
     $term = $documentQuery->getFilter('term');
     if ($term) {
         $term = '%' . $term . '%';
     }
     $geoLocationModel = $orm->getGeoLocationModel();
     $geoLocationLocalizedModel = $orm->getGeoLocationLocalizedModel();
     // query localized table
     if ($term) {
         $query = $geoLocationLocalizedModel->createQuery();
         $query->setFields('{entry}, {name}');
         $query->setLimit(100);
         $query->addCondition('{locale} = %1%', $orm->getLocale());
         $query->addCondition('{name} LIKE %1%', $term);
         $geoLocationLocalizedResult = $query->query('entry');
     }
     // query main table
     $query = $geoLocationModel->createQuery();
     $query->setFields('{id}, {path}, {code}');
     $query->setLimit(100);
     if ($term) {
         if (!$geoLocationLocalizedResult) {
             $query->addCondition('{code} LIKE %1%', $term);
         } else {
             $query->addCondition('{id} IN %1%', array_keys($geoLocationLocalizedResult));
         }
     }
     $type = $documentQuery->getFilter('type');
     if ($type) {
         if (is_array($type)) {
             $query->addCondition('{type} IN %1%', $type);
         } elseif (strpos($type, ',')) {
             $query->addCondition('{type} IN %1%', explode(',', $type));
         } else {
             $query->addCondition('{type} = %1%', $type);
         }
     }
     $path = $documentQuery->getFilter('path');
     if ($path) {
         if (strpos($path, '~') == false) {
             $path = '~' . $path . '~';
         }
         $query->addCondition('{path} LIKE %1%', '%' . $path . '%');
     }
     $geoLocationResult = $query->query();
     // foreach ($geoLocationResult as $index => $geoLocation) {
     // $result[$index] = $index;
     // }
     // merge result
     return $geoLocationResult;
 }
 /**
  * Gets the entry
  * @param string $locale Code of the locale
  * @return mixed|null Instance of the entry if found, null otherwise
  */
 public function getEntry($locale = null)
 {
     if (isset($this->entries[$locale])) {
         return $this->entries[$locale];
     }
     $model = $this->getEntryModel();
     $id = $this->getEntryId();
     if (!$model || !$id) {
         return null;
     }
     $model = $this->orm->getModel($model);
     $this->entries[$locale] = $model->getById($id, $locale);
     return $this->entries[$locale];
 }
Beispiel #3
0
 /**
  * Gets the resource adapter for the provided type
  * @param string $type Name of the resource type
  * @return JsonApiResourceAdapter
  * @throws \ride\library\http\jsonapi\exception\JsonApiException when no
  * resource adapter is set for the provided type
  */
 public function getResourceAdapter($type)
 {
     if (isset($this->resourceAdapters[$type])) {
         return $this->resourceAdapters[$type];
     }
     $modelName = $this->getModelName($type);
     if (!$modelName) {
         throw new JsonApiException('Could not get resource adapter: no adapter set for type ' . $type);
     }
     $model = $this->orm->getModel($modelName);
     $meta = $model->getMeta();
     $filterStrategies = array();
     $filterStrategyNames = $meta->getOption('json.api.filters', 'query,exact,match,expression');
     $filterStrategyNames = explode(',', $filterStrategyNames);
     foreach ($filterStrategyNames as $filterStrategyName) {
         $filterStrategyName = trim($filterStrategyName);
         $filterStrategies[$filterStrategyName] = $this->dependencyInjector->get('ride\\web\\rest\\jsonapi\\filter\\FilterStrategy', $filterStrategyName);
     }
     $resourceAdapterArguments = array('web' => $this->web, 'model' => $model, 'type' => $type);
     $resourceAdapter = $meta->getOption('json.api.adapter', 'entry');
     $resourceAdapter = $this->dependencyInjector->get('ride\\web\\rest\\jsonapi\\EntryJsonApiResourceAdapter', $resourceAdapter, $resourceAdapterArguments, true);
     $resourceAdapter->setFilterStrategies($filterStrategies);
     $this->setResourceAdapter($type, $resourceAdapter);
     return $resourceAdapter;
 }
 public function likertCompatibleAction(OrmManager $orm, $likert1, $likert2)
 {
     $likertModel = $orm->getSurveyLikertModel();
     $likert1 = $likertModel->getById($likert1);
     if (!$likert1) {
         $this->response->setNotFound();
         return;
     }
     $likert2 = $likertModel->getById($likert2);
     if (!$likert2) {
         $this->response->setNotFound();
         return;
     }
     $result = $likert1->isCompatible($likert2);
     $this->document->setLink('self', $this->request->getUrl());
     $this->document->setMeta('compatible', $result);
 }
 public function contentAction(OrmManager $orm, Cms $cms, ContentService $contentService, ImageUrlGenerator $imageUrlGenerator, $model, $id, $locale)
 {
     $model = $orm->getModel($model);
     $entry = $model->getById($id);
     if (!$entry) {
         $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
         return;
     }
     $site = $cms->getCurrentSite($this->request->getBaseUrl(), $locale);
     $content = $contentService->getContentForEntry($model, $entry, $site->getId(), $locale);
     if ($content->image) {
         $transformation = $this->request->getQueryParameter('transformation', 'crop');
         $options = array('width' => $this->request->getQueryParameter('width', 100), 'height' => $this->request->getQueryParameter('height', 100));
         $content->image = $imageUrlGenerator->generateUrl($content->image, $transformation, $options);
     }
     unset($content->data);
     $this->setJsonView($content);
 }
 /**
  * Action to display the widget
  * @return null
  */
 public function indexAction(OrmManager $orm, ContentService $contentService, I18n $i18n, ReflectionHelper $reflectionHelper, $id = null)
 {
     $contentProperties = $this->getContentProperties();
     $id = $contentProperties->getEntryId();
     if ($id === null) {
         return;
     }
     $modelName = $contentProperties->getModelName();
     if (!$modelName) {
         return;
     }
     $contentProperties->setIdField(ModelTable::PRIMARY_KEY);
     $this->entryFormatter = $orm->getEntryFormatter();
     $this->model = $orm->getModel($modelName);
     $query = $this->getModelQuery($contentProperties, $this->locale, $id);
     $content = $this->getResult($contentProperties, $contentService, $query);
     if ($content && $content->data instanceof LocalizedEntry && !$content->data->isLocalized() && !$contentProperties->getIncludeUnlocalized()) {
         $content = null;
     }
     if (!$content) {
         return;
     }
     $this->setContext('orm.entry.' . $this->id, $content);
     if ($contentProperties->getBreadcrumb()) {
         $url = $this->request->getBaseScript() . $this->properties->getNode()->getRoute($this->locale) . '/' . $id;
         $this->addBreadcrumb($url, $content->title);
     }
     if ($contentProperties->getTitle()) {
         $this->setPageTitle($content->title);
     }
     $this->setView($contentProperties, $content);
     if ($this->properties->getWidgetProperty('region')) {
         $this->setIsRegion(true);
     }
     if ($this->properties->getWidgetProperty('section')) {
         $this->setIsSection(true);
     }
     if ($this->properties->getWidgetProperty('block')) {
         $this->setIsBlock(true);
     }
 }
 public function prepareContentMenu(Event $event, OrmManager $ormManager)
 {
     $locale = $event->getArgument('locale');
     $menu = $event->getArgument('menu');
     $models = $ormManager->getModels();
     foreach ($models as $model) {
         $meta = $model->getMeta();
         $option = $meta->getOption('scaffold.expose');
         if (!$option) {
             continue;
         }
         $menuItem = new MenuItem();
         $title = $meta->getOption('scaffold.title');
         if ($title) {
             $menuItem->setTranslation($title);
         } else {
             $menuItem->setLabel($meta->getName());
         }
         $menuItem->setRoute('system.orm.scaffold.index', array('locale' => $locale, 'model' => $meta->getName()));
         $menu->addMenuItem($menuItem);
     }
 }
 /**
  * Gets an existing text from the data source
  * @param \ride\library\widget\WidgetProperties $widgetProperties Instance
  * of the widget properties
  * @param string $locale Code of the current locale
  * @param string $textId Identifier of the text
  * @param boolean $isNew Flag to see if this text will be a new text
  * @return \ride\web\cms\text\Text Instance of the text
  */
 public function getExistingText(WidgetProperties $widgetProperties, $locale, $textId, $isNew)
 {
     $textModel = $this->orm->getTextModel();
     $text = null;
     if ($textId) {
         $query = $textModel->createQuery($locale);
         $query->setIncludeUnlocalized(true);
         $query->addCondition('{id} = %1%', $textId);
         $text = $query->queryFirst();
     }
     if (!$text) {
         $text = $textModel->createEntry();
         $text->setLocale($locale);
     }
     return $text;
 }
 /**
  * Gets the content objects for the provided entries
  * @param \ride\library\orm\model\Model $model
  * @param array $entries
  * @param string $siteId
  * @param string $locale
  * @param string $idContentMapper
  * @param string $titleFormat
  * @param string $teaserFormat
  * @param string $imageFormat
  * @param string $dateFormat
  * @return array Array with Content objects for the provided entries
  * @see \ride\library\cms\content\Content
  */
 public function getContentForEntries(Model $model, array $result, $siteId, $locale, $idContentMapper = null, $titleFormat = null, $teaserFormat = null, $imageFormat = null, $dateFormat = null)
 {
     $modelName = $model->getName();
     $modelTable = $model->getMeta()->getModelTable();
     $entryFormatter = $this->orm->getEntryFormatter();
     if (!$titleFormat) {
         $titleFormat = $modelTable->getFormat(EntryFormatter::FORMAT_TITLE, false);
         if ($titleFormat == null) {
             $titleFormat = $model->getName() . ' #{id}';
         }
     }
     if (!$teaserFormat && $modelTable->hasFormat(EntryFormatter::FORMAT_TEASER)) {
         $teaserFormat = $modelTable->getFormat(EntryFormatter::FORMAT_TEASER);
     }
     if (!$imageFormat && $modelTable->hasFormat(EntryFormatter::FORMAT_IMAGE)) {
         $imageFormat = $modelTable->getFormat(EntryFormatter::FORMAT_IMAGE);
     }
     if (!$dateFormat && $modelTable->hasFormat(EntryFormatter::FORMAT_DATE)) {
         $dateFormat = $modelTable->getFormat(EntryFormatter::FORMAT_DATE);
     }
     try {
         $mapper = $this->getContentMapper($modelName, $idContentMapper);
     } catch (Exception $e) {
         $mapper = new OrmContentMapper($this->nodeModel, $model, $entryFormatter);
     }
     foreach ($result as $index => $entry) {
         $title = $entryFormatter->formatEntry($entry, $titleFormat);
         $url = $mapper->getUrl($siteId, $locale, $entry);
         $teaser = null;
         if ($teaserFormat) {
             $teaser = $entryFormatter->formatEntry($entry, $teaserFormat);
         }
         $image = null;
         if ($imageFormat) {
             $image = $entryFormatter->formatEntry($entry, $imageFormat);
         }
         $date = null;
         if ($dateFormat) {
             $date = $entryFormatter->formatEntry($entry, $dateFormat);
         }
         $content = new Content($modelName, $title, $url, $teaser, $image, $date, $entry);
         $result[$index] = $content;
     }
     return $result;
 }
 /**
  * Action to delete an item
  * @param \ride\library\orm\OrmManager $orm
  * @param string $locale Code of the locale
  * @param string $asset Id of an asset
  * @return null
  */
 public function assetDeleteAction(OrmManager $orm, $locale, $asset)
 {
     $assetModel = $orm->getAssetModel();
     $asset = $assetModel->getById($asset, $locale);
     if (!$asset) {
         $this->response->setNotFound();
         return;
     }
     $referer = $this->getAssetReferer($asset, $locale);
     if ($this->request->isPost()) {
         $assetModel->delete($asset);
         $this->addSuccess('success.data.deleted', array('data' => $asset->getName()));
         $this->response->setRedirect($referer);
         return;
     }
     $embed = $this->request->getQueryParameter('embed', false);
     $this->setTemplateView('assets/delete', array('name' => $asset->getName(), 'embed' => $embed, 'referer' => $referer));
 }
 /**
  * Gets a previous slug from the entry log
  * @param \ride\library\orm\OrmManager $orm
  * @param \ride\library\reflection\ReflectionHelper $reflectionHelper
  * @param \ride\web\cms\orm\ContentProperties $contentProperties
  * @param string $locale Code of the locale
  * @param string $id Requested slug
  * @return string|null Current slug of the entry, null if no change found
  */
 protected function getIdFromLog(OrmManager $orm, ReflectionHelper $reflectionHelper, ContentProperties $contentProperties, $locale, $id)
 {
     $entryLogChangeModel = $orm->getEntryLogChangeModel();
     $meta = $this->model->getMeta();
     $isLocalized = $meta->isLocalized();
     if ($isLocalized) {
         $model = $orm->getModel($meta->getLocalizedModelName());
     } else {
         $model = $this->model;
     }
     $idField = $contentProperties->getIdField();
     // look in the log for the requested id
     $query = $entryLogChangeModel->createQuery($locale);
     $query->addCondition('{entryLog.model} = %1%', $model->getName());
     $query->addCondition('{fieldName} = %1%', $idField);
     $query->addCondition('{oldValue} = %1%', $id);
     $query->addOrderBy('{id} DESC');
     $entryLogChange = $query->queryFirst();
     if (!$entryLogChange) {
         // no history of the provided id
         return null;
     }
     $entryLog = $entryLogChange->getEntryLog();
     $entryId = $entryLog->getEntry();
     // get the original entry
     $entry = $model->getById($entryId, $this->locale);
     if (!$entry) {
         return null;
     }
     // retrieve and return the id value from the entry
     return $reflectionHelper->getProperty($entry, $idField);
 }
 /**
  * Constructs a new component
  * @param \ride\library\orm\OrmManager $orm
  * @return null
  */
 public function __construct(OrmManager $orm)
 {
     $this->assetModel = $orm->getAssetModel();
 }
 /**
  * Action to display the widget
  * @return null
  */
 public function indexAction(OrmManager $orm, ContentService $contentService)
 {
     $contentProperties = $this->getContentProperties();
     $modelName = $contentProperties->getModelName();
     if (!$modelName) {
         if ($this->properties->isAutoCache()) {
             $this->properties->setCache(true);
         }
         return;
     }
     // handle search
     $searchForm = null;
     if ($contentProperties->hasSearch()) {
         $searchForm = $this->createFormBuilder();
         $searchForm->setAction('search' . $this->id);
         $searchForm->addRow('query', 'string', array('label' => $this->getTranslator()->translate('label.search'), 'default' => $this->request->getQueryParameter('query')));
         $searchForm = $searchForm->build();
         if ($searchForm->isSubmitted()) {
             // redirect the search
             $data = $searchForm->getData();
             $queryParameters = $this->request->getQueryParameters();
             $queryParameters['query'] = $data['query'];
             foreach ($queryParameters as $queryParameter => $queryValue) {
                 if ($queryParameter === self::PARAM_PAGE) {
                     $queryValue = 1;
                 }
                 $queryParameters[$queryParameter] = $queryParameter . '=' . urlencode($queryValue);
             }
             $url = $this->request->getUrl(true);
             $url .= '?' . implode('&', $queryParameters);
             $this->response->setRedirect($url);
             return;
         }
         $searchForm = $searchForm->getView();
     }
     // process parameters
     $action = $contentProperties->getNoParametersAction();
     $parameters = $contentProperties->getParameters();
     $arguments = func_get_args();
     array_shift($arguments);
     // remove $orm
     array_shift($arguments);
     // remove $contentService
     if ($parameters) {
         if (is_array($parameters)) {
             if (count($arguments) != count($parameters) * 2) {
                 if ($action != ContentProperties::NONE_IGNORE) {
                     $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
                 }
                 return;
             }
             $arguments = $this->parseArguments($arguments);
         } else {
             if (count($arguments) != $parameters) {
                 if ($action != ContentProperties::NONE_IGNORE) {
                     $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
                 }
                 return;
             }
             foreach ($arguments as $index => $argument) {
                 $arguments[$index] = urldecode($argument);
             }
         }
     } elseif ($arguments) {
         if ($action == ContentProperties::NONE_404 || $action == ContentProperties::NONE_IGNORE) {
             if ($action != ContentProperties::NONE_IGNORE) {
                 $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
             }
             return;
         }
     } else {
         $arguments = array();
     }
     // process pagination parameters
     $page = 1;
     $pages = 1;
     if ($contentProperties->willShowPagination()) {
         $page = $this->request->getQueryParameter(self::PARAM_PAGE);
         if (!is_numeric($page) || $page <= 0) {
             $page = 1;
         }
     }
     // create the query
     $this->entryFormatter = $orm->getEntryFormatter();
     $this->model = $orm->getModel($modelName);
     $query = $this->getModelQuery($contentProperties, $this->locale, $page, $arguments, $isFiltered);
     // apply pagination
     $numRows = -1;
     if ($contentProperties->willShowPagination()) {
         $numRows = max(0, $query->count() - $contentProperties->getPaginationOffset());
         $pages = ceil($numRows / $contentProperties->getPaginationRows());
         if ($contentProperties->useAjaxForPagination() && $this->request->isXmlHttpRequest()) {
             $this->setIsContent(true);
         }
     }
     // fetch the result
     $result = $this->getResult($contentProperties, $contentService, $query);
     if ($numRows == -1) {
         $numRows = count($result);
     }
     if (!$contentProperties->hasEmptyResultView() && !$isFiltered && !$result) {
         // no view requested when no result
         return;
     }
     if ($this->properties->getWidgetProperty('content')) {
         $this->setIsContent(true);
     }
     // set the view
     $this->setView($contentProperties, $result, $searchForm, $numRows, $pages, $page, $arguments);
 }
 /**
  * Gets the options for the model field
  * @param \ride\library\orm\OrmManager $orm Instance of the ORM manager
  * @return array Array with the model names which are flagged to be a node
  */
 protected function getModelOptions(OrmManager $orm)
 {
     $translator = $this->getTranslator();
     $options = array('' => '---');
     $models = $orm->getModels(true);
     foreach ($models as $modelName => $model) {
         $meta = $model->getMeta();
         if (!$meta->getOption('cms.node')) {
             continue;
         }
         $title = $meta->getOption('scaffold.title');
         if ($title) {
             $title = $translator->translate($title);
         } else {
             $title = $modelName;
         }
         $options[$modelName] = $title;
     }
     return $options;
 }