/** * Execute AJAX post actions to toggle. * * @param string $action * @param \DataContainer $dc */ public function executePostActions($action, \DataContainer $dc) { if ($action !== 'hasteAjaxOperation') { return; } $id = $dc->id = \Input::post('id'); $currentValue = \Input::post('value'); $operation = \Input::post('operation'); $hasteAjaxOperationSettings = $GLOBALS['TL_DCA'][$dc->table]['list']['operations'][$operation]['haste_ajax_operation']; if (!isset($hasteAjaxOperationSettings)) { return; } // Check permissions if (!$this->checkPermission($dc->table, $hasteAjaxOperationSettings)) { \System::log(sprintf('Not enough permissions to toggle field %s::%s', $dc->table, $hasteAjaxOperationSettings['field']), __METHOD__, TL_ERROR); \Controller::redirect('contao/main.php?act=error'); } // Initialize versioning $versions = new \Versions($dc->table, $id); $versions->initialize(); // Determine next value and icon $options = $this->getOptions($hasteAjaxOperationSettings); $nextIndex = 0; foreach ($options as $k => $option) { if ($option['value'] == $currentValue) { $nextIndex = $k + 1; } } // Make sure that if $nextIndex does not exist it's the first if (!isset($options[$nextIndex])) { $nextIndex = 0; } $value = $options[$nextIndex]['value']; $value = $this->executeSaveCallback($dc, $value, $hasteAjaxOperationSettings); // Update DB \Database::getInstance()->prepare('UPDATE ' . $dc->table . ' SET ' . $hasteAjaxOperationSettings['field'] . '=? WHERE id=?')->execute($value, $id); $versions->create(); if ($GLOBALS['TL_DCA'][$dc->table]['config']['enableVersioning']) { \System::log(sprintf('A new version of record "%s.id=%s" has been created', $dc->table, $id), __METHOD__, TL_GENERAL); } $response = array('nextValue' => $options[$nextIndex]['value'], 'nextIcon' => $options[$nextIndex]['icon']); $response = new JsonResponse($response); $response->send(); }
/** * Generate ajax */ public function generateAjax() { if (!\Environment::get('isAjaxRequest')) { return; } if ($this->iso_searchAutocomplete && \Input::get('iso_autocomplete') == $this->id) { $objProducts = Product::findPublishedByCategories($this->findCategories(), array('order' => 'c.sorting')); if (null === $objProducts) { $objResponse = new JsonResponse(array()); $objResponse->send(); } $objResponse = new JsonResponse(array_values($objProducts->fetchEach($this->iso_searchAutocomplete))); $objResponse->send(); } }
public function testJsonOutput() { $objResponse = new JsonResponse(array('foo' => 'bar'), 201); $this->assertSame(201, $objResponse->getStatusCode()); $this->assertSame("HTTP/1.1 201 Created\nContent-Length: 13\nContent-Type: application/json; charset=utf-8\n\n{\"foo\":\"bar\"}", (string) $objResponse); }
/** * Return the demanded frontend module or content element parsed as html string * * Required GET data: * * action: "reload-element" * * element: "ce::id" or "mod::id" (replace 'id' with the element's id) * * page: "id" (optionally, replace 'id' with the current page's id) * * auto_item: (an optional auto_item which will be set before fetching the element) */ public function getModuleOrContentElement() { if (!\Environment::get('isAjaxRequest') || Input::get('action') != 'reload-element') { return; } global $objPage; // Set page object as it may be needed for the language e.g. if (!$objPage && (int) Input::get('page')) { $objPage = \PageModel::findWithDetails((int) Input::get('page')); } $GLOBALS['TL_LANGUAGE'] = null !== $objPage ? $objPage->language : $GLOBALS['TL_LANGUAGE']; list($strElementType, $intElementId) = trimsplit('::', Input::get('element')); $strError = ''; $return = ''; // Authenticate front end user, e.g. for insert tags if (FE_USER_LOGGED_IN) { /** @noinspection PhpUndefinedMethodInspection */ $this->import('FrontendUser', 'User'); /** @var \FrontendUser $this ->User */ $this->User->authenticate(); } // Load default language file \System::loadLanguageFile('default'); // Set a given auto_item to fetch the correct version of a module or content element if ($strAutoItem = Input::get('auto_item')) { Input::setGet('auto_item', $strAutoItem); } switch ($strElementType) { case 'mod': /** @type \Model $objModule */ $objModule = \ModuleModel::findByPk($intElementId); if (null === $objModule) { $strError = sprintf('Could not find module ID %s', $intElementId); continue; } if (!$objModule->allowAjaxReload) { $strError = sprintf('Module ID %u is not allowed to fetch', $intElementId); continue; } $return = \Controller::getFrontendModule($objModule); break; case 'ce': /** @type \Model $objContent */ $objContent = ContentModel::findByPk($intElementId); if (null === $objContent) { $strError = sprintf('Could not find content element ID %s', $intElementId); continue; } if (!$objContent->allowAjaxReload) { $strError = sprintf('Content element ID %u is not allowed to fetch', $intElementId); continue; } $return = \Controller::getContentElement($objContent); break; default: $strError = 'Could not determine whether the element is a module or content element'; break; } $arrResponse = array(); if ($strError) { $arrResponse['status'] = 'error'; $arrResponse['error'] = $strError; } else { $arrResponse['status'] = 'ok'; $arrResponse['html'] = $return; } $objResponse = new JsonResponse($arrResponse); $objResponse->send(); }
/** * Generate ajax */ public function generateAjax() { if (!\Environment::get('isAjaxRequest')) { return; } // todo: Use the current filters too... if ($this->iso_searchAutocomplete && \Input::get('iso_autocomplete') == $this->id) { include_once TL_ROOT . '/system/modules/isotope_direct/config/stopwords.php'; $arrWhere = array("c.page_id IN (" . implode(',', array_map('intval', $this->findCategories())) . ")"); $arrValues = array(); $keywords = explode(' ', \Input::get('query')); for ($i = 0; $i < count($keywords); $i++) { $strTerm = trim($keywords[$i]); if (empty($strTerm) || in_array(strtolower($strTerm), array_map('strtolower', $GLOBALS['KEYWORD_STOP_WORDS'])) || in_array(strtolower($strTerm), array_map('strtolower', $GLOBALS['KEYWORD_STOP_WORDS']))) { continue; } $arrWhere[] = Product_Model::getTable() . "." . $this->iso_searchAutocomplete . " REGEXP ?"; $arrValues[] = $strTerm; } if ($this->iso_list_where != '') { $arrWhere[] = Haste::getInstance()->call('replaceInsertTags', $this->iso_list_where); } $objProducts = Product_Model::findPublishedBy($arrWhere, $arrValues, array('order' => "c.sorting")); if (null === $objProducts) { $objResponse = new JsonResponse(array('suggestions' => array())); $objResponse->send(); } $objResponse = new JsonResponse(array('suggestions' => array_values(array_map('html_entity_decode', $objProducts->fetchEach($this->iso_searchAutocomplete))))); $objResponse->send(); } }