/components/com_fabrik/models/table.php
PHP | 3994 lines | 2886 code | 400 blank | 708 comment | 677 complexity | b38126dd4abc91b2851f41221bc1ba95 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * @package Joomla
- * @subpackage Fabrik
- * @copyright Copyright (C) 2005 Rob Clayburn. All rights reserved.
- * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
- */
-
- // Check to ensure this file is included in Joomla!
- defined('_JEXEC') or die();
-
- require_once( COM_FABRIK_FRONTEND.DS.'helpers'.DS.'json.php' );
- require_once( COM_FABRIK_FRONTEND.DS.'helpers'.DS.'pagination.php' );
- require_once( COM_FABRIK_FRONTEND.DS.'helpers'.DS.'string.php' );
-
-
- class FabrikModelTable extends JModel {
-
- /** @var object the tables connection object */
- var $_oConn = null;
-
- /** @var object table Table */
- var $_table = null;
-
- /** @var array filters applied to table */
- var $_aFilter = null;
-
- /** @var array prefilters applied to the table */
- var $_aPrefilters = array();
-
- /** @var object table's database connection object (loaded from connection object) */
- var $_oConnDB = null;
-
- /** @var object table's form model */
- var $_oForm = null;
-
- /** @var array joins */
- var $_aJoins = null;
-
- /** @var array column calculations */
- var $_aRunCalculations = array();
-
- /** @var string table output format - set to rss to collect correct element data within function gettData()*/
- var $_outPutFormat = 'html';
-
- var $_isMambot = false;
-
- var $_admin = false;
-
- /** @var object to contain access rights **/
- var $_access = null;
-
- /** @var int the id of the last inserted record (or if updated the last record updated) **/
- var $_lastInsertId = null;
-
- /** @var bol determine if we apply prefilters (0 = yes, 1 = no )*/
- var $_togglePreFilters = 0;
-
- /** @var array store data to create joined records from */
- var $_joinsToProcess = array();
-
- /** @var array database fields */
- var $_dbFields = null;
-
- /** @var bol force reload table calculations **/
- var $_reloadCalculations = false;
-
- /** @var array data contains request data **/
- var $_aData = null;
-
- /** 'var array data to render the table with **/
- var $_tableData = null;
-
- /** @var string method to use when submitting form data // post or ajax*/
- var $_postMethod = 'post';
-
- /** @var int package id */
- var $_packageId = null;
-
- /** @var object plugin manager */
- var $_pluginManager = null;
-
- /** @var int id of table to load */
- var $_id = null;
-
- /** @var string join sql **/
- var $_joinsSQL = null;
-
- /** @var bol is the object inside a package? */
- //var $_inPackage = false;
-
- /** @var bol when getting the tableData this decides if only the elements published to the
- table view are loaded. Primarily used by visualization plugins to get all table data regardless
- of whether its published to the table view */
- var $_onlyTableData = true;
-
- var $_formGroupElementData = array();
-
- var $_joinsToThisKey = null;
-
- var $_real_filter_action = null;
-
- var $_aAsFields = null;
-
- /** array merged request and session data used to potentially filter the table **/
- var $_request = null;
-
- var $_aRow = null;
-
- /** array rows to delete **/
- vaR $_rowsToDelete = null;
-
- /**
- * Constructor
- *
- * @since 1.5
- */
-
- function __construct()
- {
- parent::__construct();
- $usersConfig = &JComponentHelper::getParams( 'com_fabrik' );
- $id = JRequest::getInt( 'tableid', $usersConfig->get( 'tableid' ) );
- $this->setId($id);
- $this->_access = new stdClass();
- }
-
- /**
- * main query to build table
- *
- */
-
- function render()
- {
- global $mainframe, $_PROFILER;
- $document =& JFactory::getDocument( );
- if (is_null( $this->_id ) || $this->_id == '0') {
- return JError::raiseError( 500, JText::_('Table id not set - can not render'));
- }
- $params =& $this->getParams();
- $session =& JFactory::getSession();
- $this->_outPutFormat = JRequest::getVar( 'format', 'html' );
- $pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
- $pluginManager->loadPlugInGroup( 'table' );
- if (!$pluginManager->runPlugins( 'onStartRender', $this, 'table' )) {
- return;
- }
-
- $table =& $this->getTable();
-
- //cant set time limit in safe mode so suppress warning
- @set_time_limit ( 800 );
- $this->_request = $this->getRequestData();
- $context = 'com_fabrik.table.'. $this->_id.'.list.';
- $limitLength = $mainframe->getUserStateFromRequest( $context.'limitLength', 'limit', $table->rows_per_page );
- $limitStart = $mainframe->getUserStateFromRequest($context.'limitstart', 'limitstart', 0, 'int');
- if ($this->_outPutFormat == 'feed') {
- $limitLength = JRequest::getVar( 'limit', $params->get( 'rsslimit',150 ) );
- $maxLimit = $params->get( 'rsslimitmax', 2500 );
- if ($limitLength > $maxLimit) {
- $limitLength = $maxLimit;
- }
- }
- $total = $this->getTotalRecords( );
- $this->_pageNav =& $this->_getPagination( $total, $limitStart, $limitLength );
- if ($limitLength == 0) {
- $pageNav->limit = 0;
- }
- $this->_tableData = $this->getData();
- $this->getCalculations( );
- $table->hit();
- }
-
- /**
- * this merges session data for the fromForm with any request data
- * allowing us to filter data results from both search forms and filters
- *
- * @return array
- */
-
- function getRequestData()
- {
- global $_SESSION;
- //session_destroy();
- if (isset($this->_request)) {
- return $this->_request;
- }
-
- $aData = JRequest::get( 'request' );
- $formModel =& $this->getForm();
- $table =& $this->getTable();
- if (is_array( $_SESSION )) {
-
- if (array_key_exists( "fabrik", $_SESSION )) {
-
- if (array_key_exists( 'fromForm', $_SESSION["fabrik"] )) {
- $fromForm = $_SESSION["fabrik"]['fromForm'];
- //only merge if the fromForm is not the same as the current table's form id
- if ($_SESSION["fabrik"]['fromForm'] != $formModel->_id) {
- $this->_oFromForm =& JModel::getInstance( 'Form', 'FabrikModel' );
-
- $this->_oFromForm->setId( $fromForm );
- $fromFormTable = $this->_oFromForm->getForm();
-
- $fromFormParams = $this->_oFromForm->getParams();
-
- //fudge the sesson so that any search form data's keys are the same as the current
- //tables
- if (array_key_exists( $fromForm, $_SESSION['fabrik'] )) {
- foreach ($_SESSION['fabrik'][$fromForm] as $k1=>$v) {
- //only unset if not done previously
- if (strstr( $k1, "___" )) {
- $k2 = $table->db_table_name . "___" . array_pop(explode("___", $k1));
- } else {
- $k2 = $table->db_table_name . "___" . $k1;
- }
-
- if (array_key_exists( $k2, $aData )) {
- //override search form session info with any posted filters
- $v = $aData[$k2];
- }
- $_SESSION['fabrik'][$fromForm][$k2] = $v;
- }
- }
- if (array_key_exists( $fromForm, $_SESSION["fabrik"])) {
- $aData = array_merge( $aData, $_SESSION["fabrik"][$fromForm] );
- }
- }
- //$$$rob since after 1.0.5.2 DONT unset as page nav no longer picks up the filter
- //unset( $_SESSION["fabrik"]['fromForm'] );
- }
- }
- }
- return $aData;
- }
-
- /**
- * get the table's data
- *
- * @return array of objects (rows)
- */
-
- function getData()
- {
- $fabrikDb =& $this->getDb();
- $query = $this->_buildQuery();
- $fabrikDb->setQuery( $query, $this->_pageNav->limitstart, $this->_pageNav->limit );
- if (JRequest::getBool( 'fabrikdebug', 0 ) == 1) {
- echo "<pre>"; echo $fabrikDb->getQuery();echo "</pre>";
- }
- $data = $fabrikDb->loadObjectList( );
- if ($data === false) {
- JError::raiseNotice(500, 'getData: ' . $fabrikDb->getErrorMsg( ) );
- }
- //append the cursor & total to the data
- if ($this->_outPutFormat == 'html' || $this->_outPutFormat == 'raw') {
- for ($i=0; $i<count($data); $i++) {
- $data[$i]->_cursor = $i + $this->_pageNav->limitstart;
- $data[$i]->_total = $this->_pageNav->total;
- }
- }
- $this->formatData( $data );
- return $data;
- }
-
- /**
- * run the table data through element filters
- *
- * @param array $data
- */
-
- function formatData( &$data )
- {
- jimport('joomla.filesystem.file');
- $form =& $this->getForm();
- $table =& $this->getTable();
- $pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
- $method = 'renderTableData_' . $this->_outPutFormat;
- $this->_aLinkElements = array();
- foreach ($form->_groups as $groupModel) {
- if (is_array( $groupModel->_aElements )) {
- foreach ($groupModel->_aElements as $elementModel) {
- $e =& $elementModel->getElement();
- $elementModel->setContext( $groupModel, $form, $this );
- $params =& $elementModel->getParams( );
- $col = $elementModel->getFullName( false, true, false );
-
- //check if there is a custom out put handler for the tables format
- // currently supports "renderTableData_csv", "renderTableData_rss", "renderTableData_html", "renderTableData_json"
- if (!empty( $data ) && array_key_exists( $col, $data[0] )) {
- if (method_exists( $elementModel, $method )) {
- for ($i=0; $i<count( $data ); $i++) {
- $thisRow = $data[$i];
- $coldata = $data->$col;
- $d = $elementModel->$method( $coldata, $col, $thisRow );
- $data[$i]->$col = $this->_addLink( $d, $elementModel, $thisRow );
- }
-
- } else {
- $ec = count( $data );
- for ( $i=0; $i< $ec; $i++ ) {
- $thisRow = $data[$i];
- $coldata = $thisRow->$col;
- $d = $elementModel->renderTableData( $coldata, $thisRow );
- $data[$i]->$col = $this->_addLink( $d, $elementModel, $thisRow );
- $rawCol = $col . "_raw";
- if (!array_key_exists( $rawCol, $thisRow)){
- $data[$i]->$rawCol = $elementModel->renderRawTableData( $coldata, $thisRow );
- }
- }
-
- // run a final function for each fo the elements (basically here to
- // avoid you doing extraneous sql calls in renderTableData
- // ie create sql query in rendertabledata, then run it in mergeTableData
- // currently used for advanced table joins only
- $elementModel->mergeTableData( $data, $this );
- }
-
- //see if we replace data with icons
- /*
- if ($params->get('icon_folder') != -1 && $params->get('icon_folder') != '') {
- jimport('joomla.filesystem.file');
- for ($i=0; $i<count( $data ); $i++) {
- $thisRow = $data[$i];
- $coldata = $thisRow->$col;
- $data[$i]->$col = $elementModel->replaceWithIcons( $coldata );
- }
- }
- */
- }
- }
- }
- }
- $this->_aGroupInfo = array();
- $groupTitle = array();
-
- //check if the data has a group by applied to it
- if ($table->group_by != '') {
- $groupedData = array();
- $thisGroupedData = array();
- $groupBy = $table->group_by;
- //see if we can use a raw value instead
- if (!empty( $data ) && array_key_exists( $groupBy . "_raw", $data[0] )) {
- $groupBy = $groupBy . "_raw";
- }
- $groupTitle = null;
- $aGroupTitles = array();
- $groupId = 0;
- for ( $i=0; $i<count( $data ); $i++ ) {
- if (!in_array( $data[$i]->$groupBy , $aGroupTitles )) {
- $aGroupTitles[] = $data[$i]->$groupBy;
- $groupedData[$data[$i]->$groupBy] = array();
- }
- $data[$i]->_groupId = $data[$i]->$groupBy;
- $gKey = $data[$i]->$groupBy;
- // if the group_by was added in in getAsFields remove it from the returned data set (to avoid mess in package view)
- if ($this->_group_by_added ){
- unset($data[$i]->$groupBy );
- }
- if ($this->_temp_db_key_addded ){
- $k = $table->db_primary_key;;
- }
- $groupedData[$gKey][] = $data[$i];
-
- }
- $data = $groupedData;
- } else {
- for( $i=0; $i<count( $data ); $i++ ){
- if ($this->_temp_db_key_addded ){
- $k = $table->db_primary_key;;
- }
- }
- //make sure that the none grouped data is in the same format
- $data = array( $data );
- }
-
- if ($this->_outPutFormat != 'pdf' && $this->_outPutFormat != 'csv' && $this->_outPutFormat != 'feed') {
- $this->addSelectBoxAndLinks( $data );
- if (JRequest::getVar( 'fabrikdebug' )) {
- echo "<pre>";print_r($data);echo"</pre>";
- }
- }
- }
-
- /**
- * add the select box and various links into the data array
- * @param array table row objects
- */
-
- function addSelectBoxAndLinks( &$data )
- {
- global $Itemid;
- $table =& $this->getTable();
- $db =& JFactory::getDBO();
- $params =& $this->getParams();
- $nextview = ($this->canEdit()) ? "form" : "details";
- $tmpKey = '__pk_val';
-
- $aExisitngLinkedTables = $params->get('linkedtable', '', '_default', 'array');
- $aExisitngLinkedForms = $params->get('linkedform', '', '_default', 'array');
-
- $linkedform_linktype = $params->get( 'linkedform_linktype', '', '_default', 'array' );
- $linkedtable_linktype = $params->get( 'linkedtable_linktype', '', '_default', 'array' );
- $aExistingTableHeaders = $params->get( 'linkedtableheader', '', '_default', 'array' );
- $aExistingFormHeaders = $params->get( 'linkedformheader', '', '_default', 'array' );
-
- //get a list of fabrik tables and ids for view table and form links
-
- $action = ($this->_admin) ? "task" : "view";
-
- $sql = "SELECT id, label FROM #__fabrik_tables";
- $db->setQuery( $sql );
- $aTableNames = $db->loadObjectList( 'label' );
- $cx = count($data);
- $viewLinkAdded = false;
- //for ($x=0; $x<$cx; $x++) { //if grouped data then the key is not numeric
- foreach ($data as $key=>$group) {
- //$group =& $data[$key]; //Messed up in php 5.1 group positioning in data became ambiguous
- $cg = count( $group );
- for( $i=0; $i < $cg; $i++ ){
- $row =& $data[$key][$i];
- $pKeyVal = (array_key_exists( $tmpKey, $row )) ? $row->$tmpKey : '';
- $row->fabrik_delete = ($this->canDelete()) ? '<input type="checkbox" id="id_'.$row->_cursor .'" name="ids['.$row->_cursor.']" value="' . $pKeyVal . '" />' : '';
- //add in some default links if no element choosen to be a link
- if (empty( $this->_aLinkElements ) and ($this->canView() || $this->canEdit())){
-
-
- $link = ( $this->_outPutFormat == 'json' ) ? "#" : JRoute::_("index.php?option=com_fabrik&c=form&$action=$nextview&Itemid=$Itemid&fabrik=" . $table->form_id . "&rowid=$pKeyVal&tableid=" .$this->_id . "&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
- if ($this->canEdit()) {
- $row->fabrik_edit = "<a class='fabrik___rowlink' href='$link'>" . JText::_('Edit') . "</a>";
- } else {
- if($this->canViewDetails()){
- $viewLinkAdded = true;
- $row->fabrik_edit = "<a class='fabrik___rowlink' href='$link'>" . JText::_('View') . "</a>";
- } else {
- $row->fabrik_edit = '';
- }
- }
- }
- //@TODO: test if all of these are necessary
- if ($this->canViewDetails()) {
- $link = JRoute::_( "index.php?option=com_fabrik&c=form&$action=form&Itemid=$Itemid&fabrik=" . $table->form_id . "&rowid=$pKeyVal&tableid=" .$this->_id . "&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
- $row->fabrik_view = "<a class='fabrik___rowlink' href='$link'>" . JText::_('View') . "</a>";
- }
-
- if ($this->canViewDetails( ) && $params->get( 'detaillink' ) == '1' && !$viewLinkAdded ) {
- $row->__details_link = $this->viewDetailsLink( $pKeyVal );
- }
- // create columns containing links which point to tables associated with this table
- $joinsToThisKey = $this->getJoinsToThisKey( );
- $f = 0;
- foreach ($joinsToThisKey as $element) {
- $linkedTable = array_key_exists($f, $aExisitngLinkedTables) ? $aExisitngLinkedTables[$f] : false;
- $popUpLink = array_key_exists($f, $linkedtable_linktype) ? $linkedtable_linktype[$f] : false;
- if ($linkedTable != '0') {
- if ($element->tablelabel == $table->label) { //if the link points to the same table
- $thiskey = $table->db_table_name.'.'.$this->_oTable->_tbl_key;
- $key = $element->element_name;
- $x = $element->element_name;
- $val = $row->$thiskey;
- } else {
- $linkKey = $element->db_table_name . "___" . $element->name;
- $key = $linkKey . "_table_heading";
- $val = $pKeyVal;
- }
- $element->table_id = ( array_key_exists( $element->tablelabel, $aTableNames)) ? $aTableNames[$element->tablelabel]->id : '';
-
- if ($popUpLink != '0') {
- //pop up window link
- $url = JRoute::_( "index.php?option=com_fabrik&tmpl=component&Itemid=$Itemid&view=table&tableid=$element->table_id&$linkKey" . "[value]=$val&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
- FabrikHelperHTML::mocha( 'a.popupwin' );
- $group[$i]->$key = '<a rel="{\'maximizable\':true}" href="'. $url.'" class="popupwin">'. JText::_('View') .'</a>';
- } else {
- $url = JRoute::_( "index.php?option=com_fabrik&Itemid=$Itemid&$action=table&tableid=$element->table_id&$linkKey" . "[value]=$val&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
- $group[$i]->$key = "<a href=\"$url\">" . JText::_('View'). "</a>";
- }
- }
- $f ++;
- }
-
- $linksToForms = $this->getLinksToThisKey( );
- $f = 0;
- //create columns containing links which point to forms assosciated with this table
- foreach ( $linksToForms as $element ) {
- if ($element != '') {
- $linkedForm = array_key_exists($f, $aExisitngLinkedForms) ? $aExisitngLinkedForms[$f] : false;
- $popUpLink = array_key_exists($f, $linkedform_linktype) ? $linkedform_linktype[$f] : false;
- $linkKey = $element->db_table_name . "___" . $element->name;
- $key = $linkKey . "_form_heading";
- if ($linkedForm != '0') {
- if ($popUpLink != '0') {
- $url = JRoute::_("index.php?option=com_fabrik&tmpl=component&Itemid=$Itemid&$action=form&tableid=$element->table_id&fabrik=$element->form_id&$linkKey" . "[value]=$val");
- FabrikHelperHTML::mocha( 'a.popupwin' );
- $group[$i]->$key = '<a rel="{\'maximizable\':true}" href="'. $url.'" class="popupwin">'. JText::_('Add') .'</a>';
- } else {
- $url = JRoute::_("index.php?option=com_fabrik&Itemid=$Itemid&$action=form&tableid=$element->table_id&fabrik=$element->form_id&$linkKey" . "[value]=$val");
- $group[$i]->$key = "<a href=\"$url\">" . JText::_('Add') . "</a>";
- }
- }
- }
- $f ++;
- }
- }
- }
- }
-
- /**
- * add a custom link to the element data
- *
- * @param string element $data
- * @param object element
- * @param object of all row data
- */
-
- function _addLink( $data, &$elementModel, &$row )
- {
- global $Itemid;
- if ($this->_outPutFormat == 'csv') {
- return $data;
- }
-
- $nextview = ($this->canEdit()) ? "form" : "details";
- $params =& $elementModel->getParams();
- $element =& $elementModel->getElement();
- $table =& $this->getTable();
-
- if ($element->link_to_detail == '1' && ( $this->canEdit() || $this->canView())) {
- $this->_aLinkElements = $element->name;
- if ($this->_postMethod == 'post') {
- $primaryKeyVal = $this->getKeyIndetifier( $row );
- if ($this->_admin) {
- $link = JRoute::_( "index.php?option=com_fabrik&c=form&task=$nextview&fabrik=" . $table->form_id . "$primaryKeyVal&fabrik_cursor=" . @$row->_cursor . "&fabrik_total=" . @$row->_total . "&tableid=" .$this->_id );
- } else {
- $link = JRoute::_( "index.php?option=com_fabrik&c=form&view=$nextview&Itemid=$Itemid&fabrik=" . $table->form_id . "$primaryKeyVal&fabrik_cursor=" . @$row->_cursor . "&fabrik_total=" . @$row->_total . "&tableid=" .$this->_id );
- }
- } else {
- $link = '#';
- }
- //try to remove any previously entered links
- $data = preg_replace( '/<a(.*?)>|<\/a>/', '', $data );
- $data = "<a class='fabrik___rowlink' href='$link'>$data</a>";
- } else {
- $customLink = $params->get( 'custom_link' );
- if ($customLink != '') {
- //$w = new FabrikWorker();
- //$customLink = $w->parseMessageForPlaceHolder($customLink, JArrayHelper::fromObject( $row ));
- $customLink = $this->parseMessageForRowHolder($customLink, JArrayHelper::fromObject( $row ));
- //try to remove any previously entered links
- $data = preg_replace( '/<a(.*?)>|<\/a>/', '', $data );
- $data = "<a class='fabrik___rowlink' href='$customLink'>$data</a>";
- }
- }
- return $data;
- }
-
- /**
- * get query to make records
- * @return string sql
- */
-
- function _buildQuery()
- {
- $query = $this->_buildQuerySelect();
- $query .= $this->_buildQueryJoin();
- $query .= $this->_buildQueryWhere();
- $query .= $this->_buildQueryOrder();
- return $query;
- }
-
- function _buildQuerySelect()
- {
- $form =& $this->getForm();
- $table =& $this->getTable();
- $form->getGroupsHiarachy( $this->_onlyTableData, true );
- $this->_getAsFields();
- $fields = (empty( $this->_aFields )) ? '' : implode( ", \n ", $this->_aFields ) . "\n ";
- if (trim( $table->db_primary_key ) != '' && ( $this->_outPutFormat == 'html' || $this->_outPutFormat == 'feed' ) ) {
- if ($this->isView()) {
- $strPKey = ''; //view dont have primary key!
- } else {
- $fields .= ", ";
- $strPKey = $table->db_primary_key . " AS __pk_val\n";
- }
- $query = 'SELECT DISTINCT ' . $fields . $strPKey;
- } else {
- $query = 'SELECT DISTINCT ' . trim($fields, ", \n") . "\n";
- }
- $query .= " FROM `$table->db_table_name` \n" ;
- return $query;
- }
-
- /**
- * get the part of the sql statement that orders the table data
- * @return string ordering part of sql statement
- */
-
- function _buildQueryOrder()
- {
- global $mainframe;
- $params = $this->getParams();
- if ($this->_outPutFormat == 'feed' )
- {
- $dateCol = $params->get( 'feed_date', '' );
- if( $dateCol != '' ){
- $this->order_dir = 'DESC';
- $this->order_by = $dateCol;
- return "\n ORDER BY `$dateCol` DESC";
- }
- }
- $session =& JFactory::getSession();
- $table =& $this->getTable();
-
- $postOrderBy = JRequest::getVar( 'orderby', '', 'post' );
- $postOrderBy = str_replace( ".", "___", $postOrderBy );
-
- $postOrderDir = JRequest::getVar( 'orderdir', '', 'post' );
- $arOrderVals = array('asc', 'desc', '-');
- if (in_array( $postOrderDir, $arOrderVals )) {
- $context = 'com_fabrik.table.'. $this->_id.'.order.'.$postOrderBy;
- $session->set( $context, $postOrderDir );
- }
- //build the order by statement from the session
- $strOrder = '';
- foreach ($this->_aAsFields as $field) {
- $field = str_replace('`', '', $field);
- $context = 'com_fabrik.table.'. $this->_id.'.order.'.$field;
- $dir = $session->get( $context );
- if ($dir != '' && $dir != '-' && trim( $dir ) != 'Array' ) {
- $field = str_replace( "___", ".", $field );
- if (!strstr( $field, '.' )) {
- $field = $table->db_table_name.'.'. $field ;
- }
- //ensure its quoted
-
- $field = explode( ".", $field );
- $field = "`" . $field[0] . "`.`" . $field[1] . "`";
- $strOrder == '' ? $strOrder = "\n ORDER BY " : $strOrder .= ',';
- $strOrder .= " $field $dir";
- }
- }
-
- //if nothing found in session use default ordering
- if ($strOrder == '' ) {
- if ($table->order_by != '') {
- $table->order_dir != ''? $dir = $table->order_dir : $dir = 'desc';
- $field = str_replace( "___", ".", $table->order_by );
- $field = explode( ".", $field );
- if(count($field) == 2 && $field[1] != '') {
- $field = "`" . $field[0] . "`.`" . $field[1] . "`";
- $strOrder = "\n ORDER BY $field $dir";
- }
- }
- }
-
- // apply group ordering
- $groupOrderBy = $params->get( 'group_by_order' );
- if ($groupOrderBy != '' && in_array( $groupOrderBy, $this->_aAsFields )) {
- $groupOrderDir = $this->_params->get( 'group_by_order_dir' );
- $strOrder == '' ? $strOrder = "\n ORDER BY " : $strOrder .= ',';
- $strOrder .= " `$groupOrderBy` $groupOrderDir ";
- }
- return $strOrder;
- }
-
- /**
- * get the part of the sql query that creates the joins
- * used when building the table's data
- *
- * @return string join sql
- */
-
- function _buildQueryJoin()
- {
- if (isset( $this->_joinSQL )) {
- return $this->_joinsSQL;
- }
- $sql = '';
- $joins =& $this->getJoins();
-
- $tableGroups = array();
- foreach ($joins as $join) {
- $sql .= strtoupper($join->join_type) ." JOIN `$join->table_join`" ;
-
- if ($join->table_join_alias == '') {
- /*$sql .= " ON `$join->table_join`.`$join->table_join_key` = " .
- "`$join->join_from_table`.`$join->table_key` \n";*/
- $sql .= " ON `$join->table_join`.`$join->table_join_key` = " .
- "`$join->keytable`.`$join->table_key` \n";
- } else {
- /*$sql .= " AS `" . $join->table_join_alias .
- "` ON `" . $join->table_join_alias . "`.`$join->table_join_key` = " .
- "`$join->join_from_table`.`$join->table_key` \n ";*/
-
- $sql .= " AS `" . $join->table_join_alias .
- "` ON `" . $join->table_join_alias . "`.`$join->table_join_key` = " .
- "`$join->keytable`.`$join->table_key` \n ";
-
- }
- }
- return $sql;
- }
-
- /**
- * get the part of the sql query that relates to the where statement
- *
- * @param bol $incFilters
- * @return string where query
- */
-
- function _buildQueryWhere( $incFilters = true )
- {
- if (isset( $this->_whereSQL )) {
- return $this->_whereSQL[$incFilters];
- }
- $aFilters =& $this->getFilterArray();
- $params =& $this->getParams();
- if (isset( $params ) && $this->_togglePreFilters == 0) {
- $aPrefilters = $this->getPrefilterArray( );
- } else {
- $aPrefilters = array();
- }
- $aOrSQL = array( );
- $aFoundOrGroupings = array( );
- $i = 0;
-
- $aSQLBits = array();
- if (is_array( $aFilters )) {
- /* stores sql for or statements */
- $c = 0;
- foreach ($aFilters as $key=>$val) {
- /*work through or columns first to build sql ( col = val or col = val...)
- * then remove them from the rest of the array
- */
- if ( isset( $val['aOrCols'] ) ) {
-
- $aOrColumns = $val['aOrCols'] ;
- $filterVal = isset( $val['value'] ) ? $val['value'] : '';
- /* check if we've already performed it elsewhere */
- $done = false;
- foreach ($aFoundOrGroupings as $aFoundOrGroupSet) {
- $a = filter_unused( $aFoundOrGroupSet, $aOrColumns );
- if (empty( $a )){
- $done = true;
- }
- }
- if (!$done) {
- /*
- * ok we havent processed this group -
- * lets add it to the groups the we have processed
- */
- $aFoundOrGroupings[] = $aOrColumns;
- /*
- *now lets build that query string!
- */
- $orSql ='(';
- foreach ($aOrColumns as $col) {
- $col = FabrikWorker::getDbSafeName( $col );
- $orSql .= "`$col` = '$filterVal' OR " ;
- }
- $orSql = substr($orSql, 0, strlen($orSql)-3) . ')';
- $aOrSQL[] = $orSql;
- }
- /* ok, even if its been done before we still need to remove it from the filter array */
- unset ( $aFilters[$key] );
- $c++;
- }
- }
- foreach ($aFilters as $key=>$val) {
- $filterType = isset( $val['type'] ) ? $val['type']: 'dropdown';
- $filterVal = isset( $val['value'] ) ? $val['value'] : '';
- $filterExactMatch = isset( $val['match'] ) ? $val['match'] : '';
- $fullWordsOnly = isset( $val['full_words_only'] )? $val['full_words_only'] : '0';
-
- if (array_key_exists( $key, $aPrefilters )){
- if (array_key_exists( 'sqlCond' , $aPrefilters[$key] )) {
- $sqlCond = "( " .$val['sqlCond'] . " AND " . $aPrefilters[$key]['sqlCond'] . " )";
- } else {
- //$$$rob used for prefilter and table - "Tables with database join elements linking to this table " pointing to same prefilter opt
- $sqlCond = "( " .$val['sqlCond'];
- foreach ($aPrefilters[$key] as $tmpC) {
- $sqlCond .= " AND " . $tmpC['sqlCond'];
- }
- $sqlCond .= " )";
- }
- unset( $aPrefilters[$key] );
- } else {
- $sqlCond = $val['sqlCond'];
- }
-
- if ( $filterVal != "" ) {
- $aSQLBits[] = " AND ";
- $aSQLBits[] = $sqlCond;
- $i ++;
- }
- }
- }
-
- //add in any prefiltres not duplicated by filters
- //put them at the beginning of query as well
-
- $aSQLBits2 = array();
- foreach ($aPrefilters as $key=>$ar) {
- if ($key !== '') {
- $aSQLBits2[] = $ar['concat'];
- $aSQLBits2[] = $ar['sqlCond'];
- }
- }
- // $$$rob work out the where statment minus any filters (so only include prefilters)
- // this is needed to ensure that the filter drop downs contain the correct info.
-
- $sqlNoFilter = '';
- if (!empty( $aSQLBits2 )) {
- $aSQLBits2[0] = "WHERE";
- $sqlNoFilter .= implode( ' ', $aSQLBits2 );
- if (count( $aOrSQL ) > 0) {
- if (empty( $aSQLBits2 )) {
- $sqlNoFilter .= " WHERE " . implode( ' AND ', $aOrSQL );
- } else {
- $sqlNoFilter .= ' AND ' . implode( ' AND ', $aOrSQL );
- }
- }
- }
- //apply advanced filter query
- $advancedFilter = JRequest::getVar('advancedFilterContainer', array('value' => ''), 'default', 'none', 2);
-
- $sql = '';
- $aSQLBits = array_merge( $aSQLBits2, $aSQLBits );
-
- if (!empty( $aSQLBits )) {
- $aSQLBits[0] = "WHERE";
- $sql .= implode( ' ', $aSQLBits );
- if (count( $aOrSQL ) > 0) {
- if (empty( $aSQLBits )) {
- $sql .= " WHERE " . implode( ' AND ', $aOrSQL );
- } else {
- $sql .= ' AND ' . implode( ' AND ', $aOrSQL );
- }
- }
- }
- if ($advancedFilter['value'] != '') {
- $sql .= empty( $sql ) ? " WHERE " : " AND ";
- $sql .= trim( trim( $advancedFilter['value'], "AND" ), "OR" );
- }
- $this->_whereSQL = array( '0'=>$sqlNoFilter, '1'=>$sql );
- return $this->_whereSQL[$incFilters];
- }
-
- /**
- * get the part of the table sql statement that selects which fields to load
- * (both this_>_aASFields and this->_aFields)
- *
- * @return array field names to select in getelement data sql query
- */
-
- function &_getAsFields()
- {
- if (!is_null( $this->_aAsFields )) {
- return $this->_aAsFields;
- }
- $form =& $this->getForm();
- $table =& $this->getTable();
- $aJoinObjs =& $this->getJoins();
- $this->_aAsFields = array();
- $this->_aFields = array();
- $this->_temp_db_key_addded = false;
-
- foreach ($form->_groups as $groupModel) {
- $table_name = $table->db_table_name;
- $group =& $groupModel->getGroup();
- if ($group->is_join) {
- foreach ($aJoinObjs as $join) {
- //also ignore any joins that are elements
- if (array_key_exists( 'group_id', $join ) && $join->group_id == $group->id && $join->element_id == 0 ) {
- $table_name = $join->table_join;
- }
- }
- }
- foreach ($groupModel->_aElements as $elementModel) {
- if (!$this->_onlyTableData || $elementModel->inTableFields( $this )) {
- $method = "getAsField_" . $this->_outPutFormat;
- if (!method_exists( $elementModel, $method )) {
- $method = "getAsField_html";
- }
- $elementModel->$method( $this->_aFields, $this->_aAsFields, $table_name );
- }
- }
- }
- //temporaraily add in the db key so that the edit links work, must remove it before final return
- // of getData();
-
- if (!$this->isView()) {
- if (!$this->_temp_db_key_addded && $table->db_primary_key != '') {
- $str = str_replace( '___', '.', $table->db_primary_key ) . " AS " . str_replace( '.', '___', $table->db_primary_key );
- //if we are quoting the priamry key in the db then we need to remove these quotes
- $str = str_replace( '`___`', '___', $str );
- $this->_aFields[] = $str;
- $this->_aAsFields[] = $table->db_primary_key;
- }
- }
- //for raw data in packages
-
- if ($this->_outPutFormat == 'raw') {
- $str = str_replace( '___', '.', $table->db_primary_key ) . " AS __pk_val";
- $str = str_replace( '`___`', '___', $str );
- $this->_aFields[] = $str;
- }
- //end
-
- $this->_group_by_added = false;
- //if the group by element isnt in the fields add it (otherwise group by wont work)
- if (!in_array( $table->group_by, $this->_aAsFields ) && trim( $table->group_by ) != '') {
- $this->_aFields[] = str_replace( '___', '.', $table->group_by ) . " AS `$table->group_by`";
- $this->_aAsFields[] = $table->group_by;
- $this->_group_by_added = true;
- }
- return $this->_aAsFields;
- }
-
- /**
- * checks if the params object has been created and if not creates and returns it
- * @return object params
- */
-
- function &getParams()
- {
- $table =& $this->getTable();
- if (!isset( $this->_params )) {
- $this->_params = &new fabrikParams( $table->attribs, JPATH_SITE . '/administrator/components/com_fabrik/models/table.xml', 'component' );
- }
- return $this->_params;
- }
-
-
- /**
- * Method to set the table id
- *
- * @access public
- * @param int table ID number
- */
-
- function setId( $id )
- {
- $this->_id = $id;
- }
-
- /**
- * sets the instances admin state
- * @param bol admin state
- */
-
- function setAdmin( $bol )
- {
- $this->_admin = $bol;
- }
-
- /**
- * get the table object for the models _id
- *
- * @return object table
- */
-
- function &getTable()
- {
- if (is_null( $this->_table )) {
- JTable::addIncludePath( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fabrik'.DS.'tables' );
- $row = JTable::getInstance( 'table', 'Table' );
- $row->load( $this->_id );
- $this->_table =& $row;
- }
- return $this->_table;
- }
-
- /**
- * load the database associated with the table
- *@return object database
- */
-
- function &getDb()
- {
- if (!isset( $this->_oConnDB )) {
- $cnn =& $this->getConnection();
- if (!is_object( $cnn )) {
- return JError::raiseError( 500, JText::_( 'Fabrik was unable to load the database object for this table' ) );
- }
- $this->_oConnDB =& $cnn->getDb( );
- }
- return $this->_oConnDB;
- }
-
- /**
- * function get the tables connection object
- * sets $this->_oConn to the tables connection
- * @return object connection
- */
-
- function &getConnection( )
- {
- $config =& JFactory::getConfig();
- if (!isset( $this->_oConn )) {
- $table =& $this->getTable();
- $connectionModel =& JModel::getInstance( 'connection', 'FabrikModel' );
- $connId = ( is_null( $table->connection_id ) ) ? JRequest::getVar( 'connection_id', null ) : $table->connection_id;
- $connectionModel->setId( $connId );
- if ($connId == '' || is_null( $connId ) || $connId == '-1' ){ //-1 for creating new table
- $connectionModel->loadDefaultConnection();
- $connectionModel->setId( $connectionModel->_connection->id );
- }
- $connection =& $connectionModel->getConnection( );
- // if its the default connection then load from the
- // config file
- if ($connectionModel->isDefault() ){
- $connection->host = $config->getValue('config.host');
- $connection->user = $config->getValue('config.user');
- $connection->password = $config->getValue('config.password');
- $connection->database = $config->getValue('config.db');
- }
- $this->_oConn =& $connectionModel;
- }
- return $this->_oConn;
- }
-
- /**
- * is the table published
- *
- * @return bol published state
- */
-
- function canPublish()
- {
- $table =& $this->getTable();
-
- global $mainframe;
- $db =& JFactory::getDBO();
- if (method_exists( $db, 'getNullDate' )) {
- $nullDate = $db->getNullDate( );
- } else {
- $nullDate = $this->getNullDate( );
- }
- $publishup =& JFactory::getDate($table->publish_up, $mainframe->getCfg('offset'));
- $publishup = $publishup->toUnix();
-
- $publishdown =& JFactory::getDate($table->publish_down, $mainframe->getCfg('offset'));
- $publishdown = $publishdown->toUnix();
-
- $jnow =& JFactory::getDate();
- $now = $jnow->toUnix();
- if ($table->state == '1') {
- if ($now >= $publishup || $table->publish_up == '' || $table->publish_up == $nullDate) {
- if ($now <= $publishdown || $table->publish_down == '' || $table->publish_down == $nullDate) {
- return true;
- }
- }
- }
- return false;
-
- return $table->state;
- }
-
- /**
- *
- */
-
- function canEmpty()
- {
- $user =& JFactory::getUser();
- $acl =& JFactory::getACL();
- $table =& $this->getTable();
- $params =& $this->getParams();
- $a = $params->get( 'allow_drop', 0 );
- if ($a == '29'|| $a == ''|| $a == 0) {
- $this->_access->allow_drop = true;
- } else {
- if (!is_object( $this->_access ) || !array_key_exists( 'allow_drop', $this->_access )) {
- $groupNames =& FabrikWorker::getACLGroups( $a );
- foreach ($groupNames as $name) {
- FabrikWorker::setACL( 'action', 'allow_drop', 'fabrik', $name, 'components', null );
- }
- if ($acl->acl_check( 'action', 'allow_drop', 'fabrik', $user->get(' usertype' ), 'components', null )) {
- $this->_access->allow_drop = true;
- } else {
- $this->_access->allow_drop = false;
- }
- }
- }
- return $this->_access->allow_drop;
- }
-
- /**
- * check if the user can view the detailed records
- *
- * @return bol
- */
-
- function canViewDetails()
- {
- $user =& JFactory::getUser();
- $acl =& JFactory::getACL();
- $table =& $this->getTable();
- $params =& $this->getParams();
- $a = $params->get( 'allow_view_details', 0 );
- if ($a == '29'|| $a == ''|| $a == 0) {
- $this->_access->viewdetails = true;
- } else {
- if (!is_object( $this->_access ) || !array_key_exists( 'viewdetails', $this->_access )) {
- $groupNames =& FabrikWorker::getACLGroups( $a );
- foreach ($groupNames as $name) {
- FabrikWorker::setACL( 'action', 'viewdetails', 'fabrik', $name, 'components', null );
- }
- if ($acl->acl_check( 'action', 'viewdetails', 'fabrik', $user->get(' usertype' ), 'components', null )) {
- $this->_access->viewdetails = true;
- } else {
- $this->_access->viewdetails = false;
- }
- }
- }
- return $this->_access->viewdetails;
- }
-
- /**
- * checks user access for editing records
- *
- * @return bol access allowed
- */
-
- function canEdit()
- {
- $user =& JFactory::getUser();
- $acl =& JFactory::getACL();
- $table =& $this->getTable();
- $params =& $this->getParams();
- $a = $params->get( 'allow_edit_details', 25 );
- if ($a == '29'|| $a == ''|| $a == 0) {
- $this->_access->edit = true;
- } else {
- if(!is_object($this->_access) || !array_key_exists('edit', $this->_access)){
- $groupNames = FabrikWorker::getACLGroups( $a );
- foreach ($groupNames as $name) {
- FabrikWorker::setACL( 'action', 'edit', 'fabrik', $name, 'components', null );
- }
- if ($acl->acl_check( 'action', 'edit', 'fabrik', $user->get( 'usertype' ), 'components', null )) {
- $this->_access->edit = true;
- } else {
- $this->_access->edit = false;
- }
- }
- }
- return $this->_access->edit;
- }
-
- /**
- * checks user access for deleting records
- *
- * @return bol access allowed
- */
-
- function canDelete()
- {
- $user = &JFactory::getUser();
- $acl =& JFactory::getACL();
- $table =& $this->getTable();
- $params =& $this->getParams();
- $a = $params->get( 'allow_delete', 25 );
- if ($a == '29'|| $a == '' || $a == 0 ){
- $this->_access->delete = true;
- } else {
- if (!is_object( $this->_access ) || !array_key_exists( 'delete', $this->_access )) {
- $groupNames = FabrikWorker::getACLGroups( $a );
- foreach ($groupNames as $name) {
- FabrikWorker::setACL( 'action', 'delete', 'fabrik', $name, 'components', null );
- }
- if ($acl->acl_check( 'action', 'delete', 'fabrik', $user->get( 'usertype' ), 'components', null )) {
- $this->_access->delete = true;
- } else {
- $this->_access->delete = false;
- }
- }
- }
- return $this->_access->delete;
- }
-
- /**
- * checks user access for adding records
- *
- * @return bol access allowed
- */
-
- function canAdd()
- {
- $user =& JFactory::getUser();
- $acl =& JFactory::getACL();
- $params =& $this->getParams();
- $table =& $this->getTable();
- $a = $params->get( 'allow_add', 25 );
- if ($a == '29' || $a == ''|| $a == 0){
- $this->_access->add = true;
- } else {
- if (!is_object( $this->_access ) || !array_key_exists( 'add', $this->_access )) {
- $groupNames = FabrikWorker::getACLGroups( $a );
- foreach ($groupNames as $name) {
- FabrikWorker::setACL( 'action', 'add', 'fabrik', $name, 'components', null );
- }
- if ($acl->acl_check( 'action', 'add', 'fabrik', $user->get( 'usertype' ), 'components', null )) {
- $this->_access->add = true;
- } else {
- $this->_access->add = false;
- }
- }
- }
- return $this->_access->add;
- }
-
- /**
- * check use can view the table
- * @return bol can view or not
- */
-
- function canView( ){
- $user =& JFactory::getUser();
- $acl =& JFactory::getACL();
- $table =& $this->getTable();
- if ($table->access == '29'|| $table->access == '' || $table->access == 0) {
- $this->_access->view = true;
- } else {
- if (!is_object($this->_access) || !array_key_exists('view', $this->_access)) {
- $groupNames = FabrikWorker::getACLGroups( $table->access );
- foreach ($groupNames as $name) {
- FabrikWorker::setACL( 'action', 'view', 'fabrik', $name, 'components', null );
- }
- if ($acl->acl_check( 'action', 'view', 'fabrik', $user->get('usertype'), 'components', null )) {
- $this->_access->view = true;
- } else {
- $this->_access->view = false;
- }
- }
- }
- return $this->_access->view;
- }
-
- /**
- * load the table from the form_id value
- * @param int $formId
- * @return object table row
- */
-
- function loadFromFormId( $formId ){
- JTable::addIncludePath( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fabrik'.DS.'table' );
- $row = JTable::getInstance( 'table', 'Table' );
- $origKey = $row->_tbl_key;
- $row->_tbl_key = "form_id";
- $row->load( $formId );
- $this->_table = $row;
- $row->_tbl_key = $origKey;
- $this->setId( $row->id );
- return $row;
- }
-
- /**
- * alias to loadJoins - should be used instead
- * @return array join objects (table rows - not table objects or models)
- */
-
- function &getJoins()
- {
- if (!isset( $this->_aJoins )) {
- $form =& $this->getForm();
- $form->getGroupsHiarachy( false );
- $ids = $form->getElementIds();
- $table =& $this->getTable();
- $db =& JFactory::getDBO();
- $sql = "SELECT * FROM #__fabrik_joins WHERE table_id = '$this->_id'";
- if (!empty( $ids )) {
- $sql .= " OR element_id IN ( " . implode(", ", $ids) .")";
- }
- //maybe we will have to order by element_id asc to ensure that table joins are loaded
- //before element joins (if an element join is in a table join then its 'join_from_table' key needs to be updated
- $sql .= " ORDER BY id";
- $db->setQuery( $sql );
- $this->_aJoins = $db->loadObjectList( );
- if ($db->getErrorNum()) {
- JError::raiseError( 500, $db->stderr());
- }
-
- $aliases = array();
- $tableGroups = array();
-
- foreach ($this->_aJoins as $join) {
- //if their 'table_join' has been used then update with an alais
- if (in_array( $join->table_join, $aliases )) {
- $base = $join->table_join;
- $a = $base;
- $c = 0;
- while (in_array( $a, $aliases )) {
- $a = "{$base}_{$c}";
- $c ++;
- }
- $join->table_join_alias = $a;
- } else {
- $join->table_join_alias = $join->table_join;
- }
- $aliases[] = $join->table_join;
-
- //if they are element joins add in this tables name as the calling joining table.
- if ($join->join_from_table == '') {
- $join->join_from_table = $table->db_table_name;
- }
-
- // test case:
- /*
- * you have a talbe that joins to a 2nd table
- * in that 2nd table there is a database join element
- * that 2nd elements key needs to point to the 2nd tables name and not the first
- *
- * e.g. when you want to create a n-n relationship
- *
- * events -> (table join) events_artists -> (element join) artist
- */
-
- $join->keytable = $join->join_from_table;
- if (!array_key_exists( $join->group_id, $tableGroups )) {
- $tableGroups[$join->group_id] = $join->table_join_alias;
- } else {
- if ($join->element_id != 0) {
- $join->keytable = $tableGroups[$join->group_id];
- }
- }
- }
- }
- return $this->_aJoins;
- }
-
-
- /**
- * gets the field names for the given table
- * @param string table name
- * @return array table fields
- */
-
- function getDBFields( $tbl = null )
- {
- if (is_null( $tbl )) {
- $table =& $this->getTable();
- $tbl = $table->db_table_name ;
- }
- if (!strstr( $tbl, '`' )) {
- $tbl = "`$tbl`";
- }
- if (!isset( $this->_dbFields[$tbl] ) ){
- $db =& $this->getDb();
- $db->setQuery( "DESCRIBE $tbl" );
- $this->_dbFields[$tbl] = $db->loadObjectList( );
- }
- return $this->_dbFields[$tbl];
- }
-
- /**
- * add or update a database column via sql
- * @param object element plugin
- * @param bol is new
- * @param string origional field name
- */
-
- function alterStructure( &$elementModel, $new, $origColName=null )
- {
- $db =& JFactory::getDBO();
- $element =& $elementModel->getElement();
- $pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
- $basePlugIn =& $pluginManager->getPlugIn( $element->plugin, 'element' );
- $fbConfig = JComponentHelper::getParams( 'com_fabrik' );
- $fabrikDb =$this->getDb();
- $table =& $this->getTable();
- $tableName = $table->db_table_name;
- $objtype = $basePlugIn->getFieldDescription();
- $dbdescriptions = $this->getDBFields( $tableName );
- if (!$fbConfig->get( 'fbConf_alter_existing_db_cols' )) {
- foreach ($dbdescriptions as $f) {
- if ($f->Field == $origColName) {
- $objtype = $f->Type;
- }
- }
- }
-
- if (!is_null( $objtype )) {
- foreach ($dbdescriptions as $dbdescription) {
- $fieldname = $dbdescription->Field;
- $exitingfields[] = strtolower($fieldname);
- }
- $lastfield = $fieldname;
- if (in_array( strtolower($element->name), $exitingfields )) {
- return ;
- }
-
- FabrikString::safeColName( $element->name );
- FabrikString::safeColName( $tableName );
- FabrikString::safeColName( $lastfield );
-
- if (( $new && !in_array( strtolower($element->name), $exitingfields) ) || !in_array( strtolower($origColName), $exitingfields )) {
- $sql = "ALTER TABLE $tableName ADD COLUMN $element->name $objtype AFTER $lastfield";
- } else {
- if ($origColName == null) {
- $origColName = $element->name;
- }
- FabrikString::safeColName( $origColName );
- $sql = "ALTER TABLE $tableName CHANGE $origColName " . $element->name ;
- $sql .= " $objtype";
- }
- $fabrikDb->setQuery( $sql );
- if (!$fabrikDb->query( )) {
- return JError::raiseError( 500, 'alter structure: ' . $fabrikDb->getErrorMsg( ));
- }
- $this->createCacheQuery();
- }
- return true;
- }
-
- /**
- * if not loaded this loads in the table's form object
- * also binds a reference of the table to the form.
- * @return object form model with form table loaded
- */
-
- function &getForm()
- {
- if (is_null( $this->_oForm )) {
- $this->_oForm =& JModel::getInstance( 'Form', 'FabrikModel' );
- $table =& $this->getTable();
- $this->_oForm->setId( $table->form_id );
- $this->_oForm->getForm();
- $this->_oForm->setTableModel( $this );
- //ensure joins are loaded
- }
- return $this->_oForm;
- }
-
- /**
- * tests if the table is in fact a view
- * @returns true if table is a view
- */
-
- function isView()
- {
- $params =& $this->getParams();
- $isview = $params->get('isview', null);
-
- if (is_null($isview)) {
- $db =& JFactory::getDBO();
- $table =& $this->getTable();
- $cn = $this->getConnection();
- $c = $cn->getConnection();
- $dbname = $c->database;
- $sql = "show table status like '$table->db_table_name'";
- $sql = " select table_name, table_type, engine from INFORMATION_SCHEMA.tables ".
- "where table_name = '$table->db_table_name' and table_type = 'view' and table_schema = '$dbname'";
- $db->setQuery( $sql );
- $row = $db->loadObjectList();
- $isview = empty($row) ? false : true;
- $intisview = $isview ? 1 : 0;
- //store and save param for following tests
- $params->set('isview', $isview);
- $table->attribs .= "\nisview=$intisview\n";
- $table->store();
- }
- return $isview;
- }
-
- /** DEPRECIATED SHOULD NOT BE NEEDED NOW
- function _loadJoinOnce( $join )
- {
- return $join->table_join;
- }
- **/
-
- /**
- * filter array is created in $this->_loadFilterArray
- *@return array filters
- */
-
- function &getFilterArray( )
- {
- if (isset( $this->_aFilter )) {
- return $this->_aFilter;
- }
-
- $user = &JFactory::getUser();
- $request = $this->getRequestData();
- $this->_aFilter = array();
-
- $this->_aPrefilters =& $this->getPrefilterArray();
- $form =& $this->getForm();
-
- $filterCondSQL = '';
- $aPostFilters = array();
- $useFullName = JRequest::getBool( 'fullName', 1 );
-
- if (is_null( $form->_groups )) {
- $form->getGroupsHiarachy();
- }
- foreach ($form->_groups as $groupModel) {
- $group =& $groupModel->getGroup();
- foreach ($groupModel->_aElements as $elementModel) {
- $element =& $elementModel->getElement();
- $thisData = $this->_aData;
- if ($group->is_join) {
- if (@array_key_exists( 'join', $request ) && @is_array( $request['join'][$group->join_id] )) {
- $thisData = $this->_aData['join'][$group->join_id];
- }
- $key = $elementModel->getFilterFullName( false, true, false );
- } else {
- $key = $elementModel->getFilterFullName( true, true, false );
- }
- $dbKey = str_replace( "___", ".", $key );
- $dbKey = FabrikWorker::getDbSafeName( $dbKey );
- if (array_key_exists( $key, $request )) {
- $safeKey = FabrikWorker::getDbSafeName( $dbKey );
- $arr = $elementModel->getFilterConditionSQL( $request[$key], $this->_aFilter, $safeKey, $key );
- $arr['no-filter-setup'] = ($element->filter_type == '') ? 1 : 0;
- $arr['ellabel'] = $element->label;
- if (!empty( $arr['sqlCond'] )) {
-
- $sqlCond = "( " . $arr['sqlCond'] ;
- …
Large files files are truncated, but you can click here to view the full file