Skip to content

Instantly share code, notes, and snippets.

@ijy
Created January 12, 2014 22:10
Show Gist options
  • Save ijy/8391326 to your computer and use it in GitHub Desktop.
Save ijy/8391326 to your computer and use it in GitHub Desktop.
A bare-bones example of a custom datasource for use with Symphony 2.3.
<?php
/**
* Custom Datasource Skeleton
*
* A bare-bones example of a custom datasource for use with Symphony 2.3
*
* @author: Ian Young
* @email: i.young@me.com
* @date: 12.01.2014
* @version: 1.0
*/
// Dependencies
require_once(TOOLKIT . '/class.datasource.php');
Class datasourceName extends SectionDatasource
{
/**
* Datasource Params
* Matching those provided in the visual editor.
*/
public $dsParamROOTELEMENT = ''; // Name your custom XML node (no spaces)
public $dsParamORDER = 'desc'; // 'asc' or 'desc'
public $dsParamPAGINATERESULTS = 'yes'; // 'yes' or 'no'
public $dsParamLIMIT = '20'; // Results per page if paginating
public $dsParamSTARTPAGE = '1'; // Page number to start from if paginating
public $dsParamREQUIREDPARAM = ''; // Will only return results if this url-param is included
public $dsParamPARAMOUTPUT = array(); // Filter by page params
public $dsParamREDIRECTONEMPTY = 'no'; // Redirect location if no results
public $dsParamSORT = 'system:id'; // Field to sort by (defaults to 'system:id')
public $dsParamASSOCIATEDENTRYCOUNTS = 'no'; // Include a count of entries in associated entries?
public $dsParamINCLUDEDELEMENTS = array(); // Fields to include in the XML output
// public $dsParamCACHE = '86400'; // For Cacheable Datasource extension
/**
* Examples
*/
/**
* An associative array setting field ID and url-params (with a default value of '0')
*/
// public $dsParamFILTERS = array(
// '01' => '{$url-param1:0}',
// );
/**
* An array of fields within the Section (the one we're querying) to return in the output XML
*/
// public $dsParamINCLUDEDELEMENTS = array(
// 'field1',
// 'field2',
// 'field3: formatted',
// );
/**
* Constructor
*
* @param array $env Environment variables from the Frontend class which includes any params set by Symphony or Events or by other Datasources.
* @param boolean $process_params If set to true, `Datasource::processParameters` will be called. By default this is true.
*/
public function __construct($env = NULL, $process_params = true)
{
parent::__construct($env, $process_params);
// Accessor function to return this Datasource's dependencies
// If 'chaining' datasources this is where you list the others involved.
$this->_dependencies = array();
}
/**
* Information to be displayed in the admin panel
*
* @return array Returns an associative array of information about a datasource.
*/
public function about()
{
return array(
'name' => 'Custom Datasource Name',
'author' => array(
'name' => 'Ian Young',
'website' => '',
'email' => ''),
'version' => 'Symphony 2.3.3',
'release-date' => '2014-01-01T0:00:00+00:00'
);
}
/**
* This function is required in order to identify what section this Datasource is for.
*
* It is used in the datasource editor. It must remain intact.
* Other datasources may return a string here defining their datasource type when they do not query a section.
*/
public function getSource()
{
return '1';
}
/**
* Admin Panel Editing.
*
* For custom datasources you usually want to set this to false.
*
* @return boolean True if the Datasource can be edited, false otherwise. Defaults to false.
*/
public function allowEditorToParse()
{
return false;
}
/**
* The main method of the datasource.
*
* The function argument "param_pool" gives you access to the parameter pool so that you can add
* parameters for use later in displaying a web page.
*
* You can insert additional content (XML) using the methods of the XMLElement class.
*
* @param array $param_pool The current parameter pool that this Datasource can use when filtering and finding Entries or data.
* @return array $result Returned XML elements, attributes, and parameters.
*/
public function execute(array &$param_pool = null)
{
// Set the root element (defined above)
$result = new XMLElement($this->dsParamROOTELEMENT);
// Custom logic starts here...
/**
* Example custom query, iteration, & output to XML
*/
// $query = "SELECT field1, field2, field3
// FROM table_name
// ORDER BY 'field2'";
//
// $items = Symphony::Database()->fetch($query);
// Loop through the result and build the XML
// foreach($items as $item) {
// $id = array('id' => $row['id']); // Add ID as an attribute to 'node'
// $node = new XMLElement('node', false, $id); // Create the 'node' node (group) with above attributes
// $element1 = new XMLElement('element1', $item['field1']);
// $element2 = new XMLElement('element2', $item['field2']);
// $element3 = new XMLElement('element3', $item['field3']);
// // Add the child nodes
// $node->appendChildArray(array(
// $element1,
// $element2,
// $element3
// ));
// // Output to XML
// $result->appendChild($node);
// }
// End custom logic
/**
* If no results
*
* When there are no entries found by the Datasource, this parameter will
* be set to true, which will inject the default Symphony 'No records found'
* message into the datasource's result.
*/
if($this->_force_empty_result) $result = $this->emptyXMLSet();
// Output XML to the page
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment