Blame view

administrator/components/com_jmap/tables/datasets.php 3.37 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
<?php
// namespace administrator\components\com_jmap\tables;
/**
 *
 * @package JMAP::DATASETS::administrator::components::com_jmap
 * @subpackage tables
 * @author Joomla! Extensions Store
 * @copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
// no direct access
defined ( '_JEXEC' ) or die ( 'Restricted access' );

/**
 * ORM Table for Datasets
 *
 * @package JMAP::DATASETS::administrator::components::com_jmap
 * @subpackage tables
 * @since 2.0
 */
class TableDatasets extends JTable {
	/**
	 *
	 * @var int
	 */
	public $id;
	
	/**
	 *
	 * @var string
	 */
	public $name;
	
	/**
	 *
	 * @var string
	 */
	public $description;
	
	/**
	 *
	 * @var int
	 */
	public $checked_out = 0;
	
	/**
	 *
	 * @var datetime
	 */
	public $checked_out_time = 0;
	
	/**
	 *
	 * @var string
	 */
	public $sources = '[]';
	
	/**
	 * Check Table override
	 * @override
	 *
	 * @see JTable::check()
	 */
	public function check() {
		// Title required
		if (! $this->name) {
			$this->setError ( JText::_ ( 'COM_JMAP_VALIDATION_ERROR' ) );
			return false;
		}
		
		return true;
	}
	
	/**
	 * Store Table override
	 * @override
	 *
	 * @see JTable::store()
	 */
	public function store($updateNulls = false) {
		$result = parent::store($updateNulls);
		
		// If store sucessful go on to popuplate relations table for sources/datasets
		if($result) {
			// Clear table from previous records
			$queryDelete = "DELETE" .
						   "\n FROM " . $this->_db->quoteName('#__jmap_dss_relations') .
						   "\n WHERE" .
						   "\n " . $this->_db->quoteName('datasetid') . " = " .
						   "\n " . (int)$this->id;
			if(!$this->_db->setQuery($queryDelete)->execute()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
			
			// Manage multiple tuples to be inserted using single query
			$selectedSources = json_decode($this->sources);
			if(count($selectedSources)) {
				$insertTuples = array();
				foreach ($selectedSources as $source) {
					$insertTuples[] = '(' . (int)$this->id . ',' . $source . ')';
				}
				$insertTuples = implode(',', $insertTuples);
				
				$queryMultipleInsert = "INSERT" .
									   "\n INTO " . $this->_db->quoteName('#__jmap_dss_relations') .
									   "\n (" . 
									   $this->_db->quoteName('datasetid') . "," .
									   $this->_db->quoteName('datasourceid') . ")" .
									   "\n VALUES " . $insertTuples;
				if(!$this->_db->setQuery($queryMultipleInsert)->execute()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
		
		return $result;
	}
	
	/**
	 * Delete Table override
	 * @override
	 *
	 * @see JTable::delete()
	 */
	public function delete($pk = null) {
		$result = parent::delete($pk);
		
		// If store sucessful go on to popuplate relations table for sources/datasets
		if($result) {
			// Clear table from previous records
			$queryDelete = "DELETE" .
						   "\n FROM " . $this->_db->quoteName('#__jmap_dss_relations') .
						   "\n WHERE" .
						   "\n " . $this->_db->quoteName('datasetid') . " = " .
						   "\n " . (int)$this->id;
			if(!$this->_db->setQuery($queryDelete)->execute()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		
		
		return $result;
	}
	
	/**
	 * Class constructor
	 * 
	 * @param Object& $_db
	 *        	return Object&
	 */
	public function __construct(&$_db) {
		parent::__construct ( '#__jmap_datasets', 'id', $_db );
	}
}