Created
October 31, 2018 12:07
-
-
Save wdebusschere/1729db9b92af2fca9e46142c608445ec to your computer and use it in GitHub Desktop.
Dynamic url parameters in a datasource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Usecase example: You want to unset the price filter if the $url-price is 0 | |
--> | |
<?php | |
class datasourceds1 extends SectionDatasource | |
{ | |
public $dsParamROOTELEMENT = 'ds1'; | |
public $dsParamORDER = '{$url-order:desc}'; | |
public $dsParamPAGINATERESULTS = 'yes'; | |
public $dsParamLIMIT = '20'; | |
public $dsParamSTARTPAGE = '{$url-page}'; | |
public $dsParamREDIRECTONEMPTY = 'no'; | |
public $dsParamREDIRECTONFORBIDDEN = 'no'; | |
public $dsParamREDIRECTONREQUIRED = 'no'; | |
public $dsParamSORT = '{$url-sort}'; | |
public $dsParamHTMLENCODE = 'no'; | |
public $dsParamASSOCIATEDENTRYCOUNTS = 'no'; | |
public $dsParamFILTERS = array( | |
'323' => '{$url-price}', //323 = id of the price field -> Entity Diagram Extension | |
); | |
public $dsParamINCLUDEDELEMENTS = array( | |
'system:pagination', | |
'system:date', | |
'field1', | |
'field2', | |
'price' | |
); | |
public function __construct($env = null, $process_params = true) | |
{ | |
if (strpos($_SERVER["REQUEST_URI"], "symphony")== false) //Hack not to have an error in symphony | |
{ | |
//var_dump(Symphony::Engine()->Page()->_param);die; | |
if ( Symphony::Engine()->Page()->_param['url-price']== '0') | |
{ | |
unset($this->dsParamFILTERS['323']); | |
} | |
} | |
parent::__construct($env, $process_params); | |
$this->_dependencies = array(); | |
} | |
public function about() | |
{ | |
return array( | |
'name' => 'ds1', | |
'author' => array( | |
'name' => 'Wannes Debusschere', | |
'website' => 'http://localhost:8080/xxx', | |
'email' => '[email protected]'), | |
'version' => 'Symphony 2.7.7', | |
'release-date' => '2018-06-27T13:29:35+00:00' | |
); | |
} | |
public function getSource() | |
{ | |
return '36'; | |
} | |
public function allowEditorToParse() | |
{ | |
return false; | |
} | |
public function execute(array &$param_pool = null) | |
{ | |
$result = new XMLElement($this->dsParamROOTELEMENT); | |
try{ | |
$result = parent::execute($param_pool); | |
} catch (FrontendPageNotFoundException $e) { | |
// Work around. This ensures the 404 page is displayed and | |
// is not picked up by the default catch() statement below | |
FrontendPageNotFoundExceptionHandler::render($e); | |
} catch (Exception $e) { | |
$result->appendChild(new XMLElement('error', $e->getMessage() . ' on ' . $e->getLine() . ' of file ' . $e->getFile())); | |
return $result; | |
} | |
if ($this->_force_empty_result) { | |
$result = $this->emptyXMLSet(); | |
} | |
if ($this->_negate_result) { | |
$result = $this->negateXMLSet(); | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment