Current Path :
/
home
/
develito
/
www
/
components
/
com_raforms
/
models
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/develito/www/components/com_raforms/models/listviewcategory.php
<?php //-- No direct access defined('_JEXEC') or die('=;)'); jimport('joomla.application.component.modellist'); class RAformsModelListViewCategory extends JModelList { var $_data; var $_pagination = null; var $formFields; protected $_context = 'com_raforms'; function __construct() { parent::__construct(); $this->state = $this->getState(); } function setFormFields(&$formFields) { $this->formFields = &$formFields; } protected function populateState($ordering = null, $direction = null) { // Initialise variables. $app = JFactory::getApplication(); JLoader::import( 'raform', JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_raforms' . DIRECTORY_SEPARATOR . 'models' ); // List state information $limitstart = $this->getUserStateFromRequest($this->context.'.list.start', 'limitstart', '', 'int', true); $this->setState($this->context.'.list.start', $limitstart); $listlimit = $this->getUserStateFromRequest($this->context.'.list.limit', 'limit', $app->getCfg('list_limit'), 'int'); $this->setState($this->context.'.list.limit', $listlimit); // Params $this->params = $app->getParams(); // get captions $this->captions = $this->params->get('captions'); // get data for listview $listview = $this->params->get('listview'); // add a c_ for column if (is_array($listview)) { foreach ($listview as $formid => $column) { ksort($column); $position = 0; foreach ($column as $pos => $fieldname) { // for resolving formid_c_position later $this->listview[$formid]['c_' . $position] = $fieldname; $this->columnMapping[$formid][$fieldname] = 'c_' . $position; $this->filterMapping[$formid . '_f_' . $fieldname] = 'c_' . $position; $position++; } } } // get data for listfilter $this->listfilter = $this->params->get('listfilter'); /* $formid = $this->params->get('formid'); $this->formid = (empty($formid)) ? $app->getUserStateFromRequest($this->_context . '.formid', 'formid', 0) : $formid; // if we came from a category page, rip off alias from slug $this->formid = (strpos($this->formid, ':')) ? substr($this->formid, 0, strpos($this->formid, ':')) : $this->formid; $app->setUserState($this->_context . '.formid', $this->formid); */ // catid $catid = $this->params->get('catid'); $this->catid = (empty($catid)) ? $app->getUserStateFromRequest($this->_context . '.catid', 'catid', 0) : $catid; // if we came from a category page, rip off alias from slug $this->catid = (strpos($this->catid, ':')) ? substr($this->catid, 0, strpos($this->catid, ':')) : $this->catid; $app->setUserState($this->_context . '.catid', $this->catid); If ($this->catid <= 0) { JError::RaiseError('500', 'RAFORMS_NO_VALID_CATID_GIVEN'); } // load structures $this->formids = $this->getFormIds(); foreach ($this->formids as $formid) { $raform = JModelLegacy::getInstance( 'raform', 'RAformsModel' ); $raform->setId($formid); $this->formFields[$formid] = $raform->getData(); $this->catForms[$formid] = $this->formFields[$formid]->formname; $this->formsAccess[$formid] = $this->formFields[$formid]->access; unset($raform); } // remove fieldposition data as it's not needed foreach ($this->formFields as $formid => $data) { foreach ($this->formFields[$formid] as $fields) { unset($this->formFields[$formid]); $this->formFields[$formid] = $fields; } } if (is_array($this->listfilter)) { foreach ( $this->listfilter as $container => $filterData ) { foreach ( $filterData as $findex => $filterName ) { /* // Determine default ordering if (array_key_exists('listview', $field) && $field['listview'] >= 0) { if (array_key_exists('listviewordering', $field) && $field['listviewordering'] == 'true') { $ordering = $field['fieldname']; $direction = $field['listvieworderingdirection']; } } */ $filter = $this->getUserStateFromRequest($this->context.'.filter.lvc_' . $this->catid . '_' . $filterName, 'filter_lvc_' . $this->catid . '_'.$filterName, ''); $this->setState('filter.lvc_' . $this->catid . '_' . $filterName, $filter); } } } $orderCol = $this->getUserStateFromRequest($this->_context . '.lvc.' . $this->catid . '.list.ordering', 'filter_order', $this->params->get('listorder')); $orderDirn = $this->getUserStateFromRequest($this->_context . '.lvc.' . $this->catid . '.list.direction', 'filter_order_Dir', $this->params->get('listdir')); $orderCol = (empty($orderCol)) ? 'c_0' : $orderCol; $orderDirn = (empty($orderDirn)) ? 'ASC' : $orderDirn; $this->setState($this->_context . '.lvc.' . $this->catid . '.list.ordering', $orderCol); $this->setState($this->_context . '.lvc.' . $this->catid . '.list.direction', $orderDirn); parent::populateState($ordering, $direction); } protected function getFormIds() { $db = JFactory::getDBO(); $sql = $db->getQuery(true); $sql->select('id'); $sql->from('#__raforms'); $sql->where('catid = ' . (int) $this->catid); $db->setQuery($sql); $formids = $db->loadColumn(); return $formids; } public function getPagination() { // Get a storage key. $store = $this->getStoreId('getPagination'); // Try to load the data from internal storage. if (!empty($this->cache[$store])) { return $this->cache[$store]; } // Create the pagination object. jimport('joomla.html.pagination'); $limit = (int) $this->getState($this->context.'.list.limit') - (int) $this->getState('list.links'); $page = new JPagination($this->getTotal(), $this->getStart(), $limit); // Add the object to the internal cache. $this->cache[$store] = $page; return $this->cache[$store]; } public function getStart() { $store = $this->getStoreId('getstart'); // Try to load the data from internal storage. if (!empty($this->cache[$store])) { return $this->cache[$store]; } $start = $this->getState($this->context.'.list.start'); $limit = $this->getState($this->context.'.list.limit'); $total = $this->getTotal(); if ($start > $total - $limit) { $start = max(0, (int)(ceil($total / $limit) - 1) * $limit); } $start = ($limit == 0) ? 0 : $start; // Add the total to the internal cache. $this->cache[$store] = $start; return $this->cache[$store]; } protected function getEffects() { $rowEffects = array(); $fieldEffects = array(); $tableEffects = array(); $jsEffects = array(); $effects = array('rowEffects' => array(), 'fieldEffects' => array(), 'tableEffects' => array()); //, 'jsEffects' => array()); foreach ( $this->formids as $formid ) { foreach ( $this->formFields[$formid] as $field ) { if (array_key_exists('fx', $field)) { switch ($field['elementtype']) { case 'radio': case 'checkbox': if (array_key_exists('selopt', $field) && is_array($field['selopt'])) { foreach ($field['selopt'] as $id => $option) { $style = ''; if (array_key_exists($field['fieldname'] . '_' . $id, $field['fx'])) { if (array_key_exists('background-color', $field['fx'][$field['fieldname'] . '_' . $id]['fxtype'])) { $style = 'background-color: ' . $field['fx'][$field['fieldname'] . '_' . $id]['fxtype']['background-color'] . ';'; } if (array_key_exists('css', $field['fx'][$field['fieldname'] . '_' . $id]['fxtype'])) { $style = $field['fx'][$field['fieldname'] . '_' . $id]['fxtype']['css']; } if ($field['fx'][$field['fieldname'] . '_' . $id]['applyto'] == 'row') { @$rowEffects[$formid][$formid . '_' . $field['fieldname']][$id] .= $style; } if ($field['fx'][$field['fieldname'] . '_' . $id]['applyto'] == 'field') { @$fieldEffects[$formid][$formid . '_' . $field['fieldname'] . '_' . $id] .= $style; } } } } break; default : $pluginPath = JPATH_SITE . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'extension' . DIRECTORY_SEPARATOR . 'raform_' . $field['elementtype'] . DIRECTORY_SEPARATOR . 'site'; $pluginfx = $pluginPath . DIRECTORY_SEPARATOR . 'listviewfx_' . $field['elementtype'] . '.php'; if (JFile::exists($pluginfx)) { include_once($pluginfx); $rowFx = JFile::stripExt(JFile::getName($pluginfx)) . 'Row'; $fieldFx = JFile::stripExt(JFile::getName($pluginfx)) . 'Field'; $tableFx = JFile::stripExt(JFile::getName($pluginfx)) . 'Table'; $jsFx = JFile::stripExt(JFile::getName($pluginfx)) . 'Js'; if ($field['fx'][$field['fieldname']]['applyto'] == 'row' && function_exists($rowFx)) { @$rowEffects[$formid][$formid . '_' . $field['fieldname']] .= $rowFx($field); } else if ($field['fx'][$field['fieldname']]['applyto'] == 'field' && function_exists($fieldFx)) { @$fieldEffects[$formid][$formid . '_' . $field['fieldname']] .= $fieldFx($field); } else if ($field['fx'][$field['fieldname']]['applyto'] == 'table' && function_exists($tableFx)) { $field['fieldname'] = $formid . '_' . $field['fieldname']; @$tableEffects[$formid][] = $tableFx($field); } else if ($field['fx'][$field['fieldname']]['applyto'] == 'js' && function_exists($jsFx)) { $field['fieldname'] = $formid . '_' . $field['fieldname']; @$jsEffects[$formid][] = $jsFx($field); } } break; } } } } if (count($rowEffects) > 0) { $effects['rowEffects'] = $rowEffects; } if (count($fieldEffects) > 0) { $effects['fieldEffects'] = $fieldEffects; } if (count($tableEffects) > 0) { $effects['tableEffects'] = $tableEffects; } /* No longer necessary if (count($jsEffects) > 0) { $effects['jsEffects'] = $jsEffects; } */ return $effects; } function resolveEffects(&$items) { if (!property_exists($this, 'effects')) { // get effect information $this->effects = $this->getEffects(); } $dataEffects = array(); foreach ($items as $item => $fields) { $formid = $fields->formid; $id = $fields->id; foreach ($fields as $field => $value) { if ($field == 'id' || $field == 'formid') { continue; } $field = (isset($this->listview[$formid][$field])) ? $formid . '_' . $this->listview[$formid][$field] : $formid . '_' . $field; if (preg_match("/\\0/", $value)) { $svalues = preg_split("/\\0/", $value); foreach ($svalues as $skey => $svalue) { if ( isset($this->effects['rowEffects'][$formid]) && array_key_exists($field, $this->effects['rowEffects'][$formid]) && array_key_exists($svalue, $this->effects['rowEffects'][$formid]) ) { if (!isset($dataEffects[$item])) { $dataEffects[$item] = new stdClass(); } @$dataEffects[$item]->rowEffect .= $this->effects['rowEffects'][$formid][$field][$svalue]; } if ( isset($this->effects['fieldEffects'][$formid]) && array_key_exists($field . '_' . $svalue, $this->effects['fieldEffects'][$formid]) ) { if (!isset($dataEffects[$item])) { $dataEffects[$item] = new stdClass(); } @$dataEffects[$item]->fieldEffect[$field] .= $this->effects['fieldEffects'][$field . '_' . $svalue]; } } unset($svalue); unset($svalues); } else { if ( isset($this->effects['rowEffects'][$formid]) && array_key_exists($field, $this->effects['rowEffects'][$formid]) && array_key_exists($value, $this->effects['rowEffects'][$formid][$field]) ) { if (!isset($dataEffects[$item])) { $dataEffects[$item] = new stdClass(); } @$dataEffects[$item]->rowEffect .= $this->effects['rowEffects'][$formid][$field][$value]; } if (isset($this->effects['fieldEffects'][$formid]) && array_key_exists($field . '_' . $value, $this->effects['fieldEffects'][$formid]))// && !@array_key_exists($value, $dataEffects[$item]->fieldEffect[$field])) { if (!isset($dataEffects[$item])) { $dataEffects[$item] = new stdClass(); } @$dataEffects[$item]->fieldEffect[$field] .= $this->effects['fieldEffects'][$formid][$field . '_' . $value]; } } } } return $dataEffects; } protected function getListQuery() { $app = JFactory::getApplication(); $db = JFactory::getDBO(); $user = JFactory::getUser(); //$this->formid = $this->params->get('formid'); $listview = array(); $listviewcrop = array(); $datetimefields = array(); $own_field = array(); $foreign_field = array(); $query = array(); $sql = array(); /* print "<pre>"; print_r($this->listview); print "</pre>"; */ if (property_exists($this, 'listview')) { foreach ( $this->listview as $formid => $listviewData) { $table = $this->getTable('formresultlist'); $table->initialize($db, $formid); // first, gather data in $listview foreach ( $listviewData as $formidColumn => $fname ) { if (empty($fname)) { continue; } // determine if field is stored in separate table switch ($this->formFields[$formid][$fname]['elementtype']) { case 'select': case 'checkbox': case 'radio': $foreign_field[$formid][$fname] = 1; break; default: $own_field[$formid][$fname] = 1; break; } if (array_key_exists($fname, $this->formFields[$formid])) { // delete corrupted entries if (strlen($this->formFields[$formid][$fname]['fieldname']) > 0) { $listview[$formid][$this->formFields[$formid][$fname]['fieldname']] = $this->formFields[$formid][$fname]['fieldname']; } else { //unset($formFields[$k]); continue; } // check for cropping entries if ($this->formFields[$formid][$fname]['listviewcrop'] > 0) { $listviewcrop[$formid][$fname] = $this->formFields[$formid][$fname]['listviewcrop']; } // check for datetime values if (preg_match("/^datetime.*/", $this->formFields[$formid][$fname]['fieldname'])) { if (strlen($this->formFields[$formid][$fname]['format']) > 0) { $datetimefields[$formid][$fname] = $this->formFields[$formid][$fname]['format']; } } } // if there's a filter for the element, but the field is not visible in list view, just join else if ( array_key_exists('listfiltercontainer', $this->formFields[$formid][$fname]) && (int) $this->formFields[$formid][$fname]['listfiltercontainer'] >= 0 && $this->recursiveArraySearchAll($this->listfilter, $formid . '_f_' . $this->formFields[$formid][$fname]['fieldname']) !== false ) { if (isset($foreign_field[$formid][$this->formFields[$formid][$fname]['fieldname']])) { $query[$formid]->join('left', $table->getTableName() . '_' . $this->formFields[$formid][$fname]['fieldname'] . ' ON ' . $table->getTableName() . '.id = ' . $table->getTableName() . '_' . $this->formFields[$formid][$fname]['fieldname'] . '.dataset_id'); } } // load fieldvalues if an effect isset foreach ( $this->formFields[$formid] as $k => $formField ) { if (array_key_exists('fx', $formField)) { $listview[$formid][$formField['fieldname']] = $formField['fieldname']; } } } $query[$formid] = $db->getQuery('true'); $query[$formid]->select( (int) $formid . ' AS formid'); $query[$formid]->select($db->quoteName($table->getTableName()) . '.id AS id'); $table = $this->getTable('formresultlist'); $table->initialize($db, $formid); $column = 0; foreach ($listviewData as $formidColumn => $fname) { // do format replacements // crop entries by chars if detected above if (isset($listviewcrop[$formid]) && isset($listviewcrop[$formid][$fname])) { $listview[$formid][$fname] = 'INSERT(' . $db->quoteName($table->getTableName() . '.' . $fname) . ', ' . $listviewcrop[$formid][$fname] . ', -1, "...") AS c_' . $column; } // format datetime entries if detected above if (isset($datetimefields[$formid]) && isset($datetimefields[$formid][$fname])) { $format = preg_replace("/%M/", "%i", $datetimefields[$formid][$fname]); $listview[$formid][$fname] = 'DATE_FORMAT('.$db->quoteName($table->getTableName() . '.' . $fname).', "'.$format.'") AS c_' . $column; } // finally build query if (count($listview) == 0) { return null; } else { if (empty($fname)) { $query[$formid]->select( 'NULL AS c_' . $column ); } else { if (isset($own_field[$formid]) && isset($own_field[$formid][$fname])) { if (isset($datetimefields[$formid][$fname]) || isset($listviewcrop[$formid][$fname])) { $query[$formid]->select( $listview[$formid][$fname] ); // save columns to map against foreign fields in forms for the filters $fieldMapping[$formid][$fname] = $table->getTableName() . '.' . $fname; } else { $query[$formid]->select( $table->getTableName() . '.' . $listview[$formid][$fname] . ' AS c_' . $column); // save columns to map against foreign fields in forms for the filters $fieldMapping[$formid][$fname] = $table->getTableName() . '.' . $listview[$formid][$fname]; } } if (isset($foreign_field[$formid]) && isset($foreign_field[$formid][$fname])) { $query[$formid]->select('GROUP_CONCAT(DISTINCT ' . $table->getTableName() . '_' . $fname . '.data SEPARATOR "\0") AS c_' . $column); $query[$formid]->join('left', $db->quoteName($table->getTableName() . '_' . $fname) . ' ON ' . $db->quoteName($table->getTableName()) . '.' . $db->quoteName('id') . ' = ' . $table->getTableName() . '_' . $fname . '.dataset_id'); // save columns to map against foreign fields in forms for the filters $fieldMapping[$formid][$fname] = $table->getTableName() . '_' . $fname . '.data'; } } } $column++; } $query[$formid]->from($table->getTableName()); $query[$formid]->join('left', '#__raforms f' . $formid . ' ON f' . $formid . '.id = ' . $table->getTableNameId() ); // check access $viewlevels = $user->getAuthorisedViewLevels(); $viewlevels = array_flip($viewlevels); $viewlevels = array_flip($viewlevels); $query[$formid]->where('f' . $formid . '.access IN (' . implode(',', $viewlevels) . ')'); $datetimes = array(); $query[$formid]->group('id'); } } foreach ($query as $formid => $q) { // build where if ($state = $this->getState()) { foreach ($state as $rawFilterFieldname => $values) { if (is_array($values) && count($values) == 1 && $values[0] == '') { // no value, no where :) continue; } if ((is_array($values) && count($values) == 0) || (is_string($values) && $values == '')) { // no value, no where :) continue; } if (!is_array($values)) { $values = array(0 => $values); } foreach ($values as $valK => $fieldValue) { if (!preg_match("/^filter\.lvc_" . $this->catid . "_/", $rawFilterFieldname)) { // no filter field, skip continue; } if (preg_match("/^[^_]*_v_[^-]*-.*/", $fieldValue)) { $filterFormid = preg_replace("/^([^_]*)_v_.*$/", "$1", $fieldValue); $filterFieldname = preg_replace("/^[^_]*_v_([^-]*)-.*/", "$1", $fieldValue); list($unused, $filterValue) = preg_split('/-/', $fieldValue); } else { // if values came from a plugin and those values are unique $filterFormid = preg_replace("/^filter\.lvc_" . $this->catid . "_([^_]*)_f_.*$/", "$1", $rawFilterFieldname); $filterFieldname = preg_replace("/^filter\.lvc_" . $this->catid . "_[^_]*_f_(.*)/", "$1", $rawFilterFieldname); $filterValue = $fieldValue; } if ($filterFormid != $formid) { $foreignForm = true; // so it's a column of a foreign form // now we have to address its corresponding column in list view // taken from the original form's column mapping if (isset($this->columnMapping[$filterFormid]) && isset($this->columnMapping[$filterFormid][$filterFieldname])) { $column = $this->columnMapping[$filterFormid][$filterFieldname]; if (($resultArray = $this->recursiveArraySearchAll($this->columnMapping[$formid], $column)) === false) { // argh ... not found continue; } else { // found it! $tableFieldName = $fieldMapping[$formid][$resultArray[0]]; } } else { // we have no entry for this form :( // so we cannot map any fields // skip continue; } } else { $foreignForm = false; if (isset($fieldMapping[$formid]) && isset($fieldMapping[$formid][$filterFieldname])) { // found it! $tableFieldName = $fieldMapping[$formid][$filterFieldname]; } else { // we have not found the corresponding column :( // skip continue; } } // column has been found, let's work if (preg_match("/^user/", $filterFieldname)) { // User Filter $userid = $this->getUserStateFromRequest($this->context.'.filter.lvc_' . $this->catid . '_' . $formid . '_f_user', 'filter_lvc_' . $this->catid . '_' . $formid . '_f_user'); if ($userid != "") { $query[$formid]->where($tableFieldName . " = " . $userid, 'OR'); } continue; } if (preg_match("/^datetime.*/", $filterFieldname)) { if (strlen($filterValue) > 0) { $datetimes[strtotime($filterValue)] = 'DATE(' . $tableFieldName . ')|' . $filterValue; } } else { $fieldtype = preg_replace("/^.*_f_([^_]*)_.*$/", "$1", $rawFilterFieldname); $foreignIds = array(); switch ($fieldtype) { case 'radio': case 'select': case 'checkbox': if ($foreignForm) { // we resolve foreign from fields values // to get eventually better results when comparing different data types if (array_key_exists('selopt', $this->formFields[$filterFormid][$filterFieldname]) && is_array($this->formFields[$filterFormid][$filterFieldname]['selopt'])) { // refill value //$filterValue = trim($this->formFields[$filterFormid][$filterFieldname]['selopt'][$filterValue]['option']); // in case, it's a select / radio / checkbox // and we don't take its' values from the database, // we have to resolve their id to similar textual values foreach ($this->formFields[$filterFormid][$filterFieldname]['selopt'] as $fId => $fOption) { if (preg_match('/' . $filterValue . '/', $fOption['option'])) { $foreignIds[] = $fId; } } } } if ($foreignForm && count($foreignIds) > 0) { $query[$formid]->where($tableFieldName . ' IN ( ' . implode(',', $foreignIds) . ' ) ', 'OR'); } else { $query[$formid]->where($tableFieldName . ' = ' . $db->quote($filterValue), 'OR'); } break; case 'text': case 'textarea': if (strlen($filterValue) > 0) { $query[$formid]->where($tableFieldName . ' LIKE ' . $db->quote('%' . $filterValue . '%'), 'OR'); } break; default: $query[$formid]->where($tableFieldName . ' ' . (($foreignForm) ? 'LIKE ' . $db->quote('%' . $filterValue . '%') : '= ' . $db->quote($filterValue)), 'OR'); break; } } } } if (count($datetimes) == 1) { list($fieldname, $values) = preg_split('/\|/', array_shift($datetimes)); $query[$formid]->where($fieldname . " = " . $db->quote($values), 'OR'); } if (count($datetimes) > 1) { ksort($datetimes); list($fieldname, $values) = explode('|', array_shift($datetimes)); $values = strftime('%Y-%m-%d %H:%M:%S', strtotime($values)); $query[$formid]->where($fieldname . " >= " . $db->Quote($values), 'OR'); list($fieldname, $values) = explode('|', array_pop($datetimes)); $values = strftime('%Y-%m-%d %H:%M:%S', (strtotime($values) + 86400)); $query[$formid]->where($fieldname . " <= " . $db->Quote($values), 'OR'); } } // pack in parantheses for union $sql[] = '(' . $q . ')'; } /* $trace = debug_backtrace(); print "<pre>"; print_r($trace); print "</pre>"; $caller = array_shift($trace); echo '<br><br>'; echo 'called by '.$caller['function']; if (isset($caller['class'])) echo ' in '.$caller['class']; echo '<br><br>'; */ if (count($query) == 0) { return false; } else { $query = implode(' UNION ALL ', $sql); // Add the list ordering clause. $query .= ' ORDER BY ' . $db->quoteName($this->getState($this->_context . '.lvc.' . $this->catid . '.list.ordering')) . ' ' . $this->getState($this->_context . '.lvc.' . $this->catid . '.list.direction'); } //print "SQL: " . preg_replace('/#__/', 'jos_', $query) . "<br/><br/><br/>"; //die(); return $query; } public function getItems() { // Get a storage key. $store = $this->getStoreId(); // Try to load the data from internal storage. if (!empty($this->cache[$store])) { return $this->cache[$store]; } // Load the list items. $query = $this->_getListQuery(); if ($query === false) { $this->setError(JText::_('RAFORMS_QUERY_EMPTY')); return false; } $items = $this->_getList($query, $this->getStart(), $this->getState($this->context.'.list.limit')); // Check for a database error. if ($this->_db->getErrorNum()) { $this->setError($this->_db->getErrorMsg()); return false; } // resolve effects $this->dataEffects = $this->resolveEffects($items); // resolve fields which rely on db tables, are serialized arrays or are userids $items = $this->resolveFields($items); // Add the items to the internal cache. $this->cache[$store] = $items; return $this->cache[$store]; } protected function getStoreId($id = '') { // Add the list state to the store id. $id .= ':'.$this->getState($this->context.'.list.start'); $id .= ':'.$this->getState($this->context.'.list.limit'); $id .= ':'.$this->getState('list.ordering'); $id .= ':'.$this->getState('list.direction'); return md5($this->context.':'.$id); } protected function getUserIDs() { $db =& JFactory::getDbo(); $sql = $db->getQuery(true); $sql->select('DISTINCT user_id'); $sql->from('#__user_usergroup_map'); $sql->where('group_id IN (' . implode(",", $this->params->get('addgroups')) . ')'); $db->setQuery($sql); if (! $userIDs = $db->loadColumn()) { return array(0); } return $userIDs; } function &resolveFields(&$items) { $dbData = array(); $userids = array(); if (count($this->formFields) == 0) { return $items; } // resolve items by fieldname foreach ($items as $item => $itemFields) { $formid = $itemFields->formid; $id = $itemFields->id; foreach ($itemFields as $itemField => $itemValue) { if ($itemField == 'id' || $itemField == 'formid') { continue; } $field = (isset($this->listview[$formid][$itemField])) ? $this->listview[$formid][$itemField] : $itemField; //print "$field => $itemValue <br>"; // check if data for field was pulled from database if (array_key_exists($field, $this->formFields[$formid])) { if (array_key_exists('seloptfromdb', $this->formFields[$formid][$field]) && $this->formFields[$formid][$field]['seloptfromdb'] == 'true') { if (!array_key_exists($field, $dbData)) { $db = JFactory::getDBo(); $db->setQuery($this->formFields[$formid][$field]['seloptfromdbsql']); $dbData[$field] = $db->loadObjectList($this->formFields[$formid][$field]['seloptfromdbvalue']); } if (is_array($values = @unserialize($itemValue))) { isset($resolvedArray) ? $resolvedArray = array() : true; foreach ($values as $k_val => $v_val) { $resolvedArray[$k_val] = $dbData[$field][$v_val]->{$this->formFields[$formid][$field]['seloptfromdbtext']}; } if (count($resolvedArray) == 1) { $items[$item]->$itemField = array_shift($resolvedArray); } else { $items[$item]->$itemField = $resolvedArray; } } else { if (strlen($itemValue) > 0) { if (preg_match("/\\0/", $itemValue)) { $svalues = preg_split("/\\0/", $itemValue); foreach ($svalues as $skey => $svalue) { if (!isset($dbData[$field][$svalue]) || ! $resolved = $dbData[$field][$svalue]->{$this->formFields[$formid][$field]['seloptfromdbtext']}) { $resolved_values[] = $svalue; } else { $resolved_values[] = $resolved; } } $items[$item]->$itemField = implode($resolved_values, ', '); unset($svalue); unset($svalues); unset($resolved_values); } else { if ( ! strlen($itemValue) > 0 || ! $resolved_value = @$dbData[$field][$itemValue]->{$this->formFields[$formid][$field]['seloptfromdbtext']}) { $items[$item]->$itemField = $itemValue; } else { $items[$item]->$itemField = $resolved_value; } unset($resolved_value); } } } } else if ($unerialized = @unserialize($itemValue)) { $items[$item]->$itemField = $unerialized; } else { switch ($this->formFields[$formid][$field]['elementtype']) { case 'select': case 'checkbox': case 'radio': if (preg_match("/\\0/", $itemValue)) { $svalues = preg_split("/\\0/", $itemValue); foreach ($svalues as $skey => $svalue) { if (!isset($this->formFields[$formid][$field]['selopt'][$svalue]) || !isset($this->formFields[$formid][$field]['selopt'][$svalue]) || ! $resolved = $this->formFields[$formid][$field]['selopt'][$svalue]['option']) { $resolved_values[] = $svalue; } else { $resolved_values[] = $resolved; } } $items[$item]->$itemField = implode($resolved_values, ', '); unset($svalue); unset($svalues); unset($resolved_values); } else { if ( strlen($itemValue) == 0 || !isset($this->formFields[$formid][$field]['selopt'][$itemValue]) || ! $resolved_value = $this->formFields[$formid][$field]['selopt'][$itemValue]['option']) { $items[$item]->$itemField = $itemValue; } else { $items[$item]->$itemField = $resolved_value; } unset($resolved_value); } break; default: if (preg_match("/\\0/", $itemValue)) { $svalues = preg_split("/\\0/", $itemValue); $items[$item]->$itemField = implode($svalues, ', '); unset($svalues); } break; } } } // collect userids if existent if ($field == 'user') { $userids[] = $itemValue; $useridMapping[$item][$itemField] = $itemValue; } } } // resolve usernames if applicable if (count($userids) > 0) { $items = $this->resolveUsernames($userids, $useridMapping, $items); } return $items; } function resolveUsernames(&$userids, &$useridMapping, &$items) { $db =& JFactory::getDBO(); $users = array(); $sql = $db->getQuery(true); $sql = "SHOW TABLES LIKE '#__comprofiler'"; $db->setQuery($sql); $rs = $db->query($sql); $numRows = $db->getNumRows(); $comprofiler = (empty($numRows)) ? false : true; $ids = implode(',', $userids); if ($comprofiler && !JPluginHelper::isEnabled('authentication', 'ldap')) { $sql = "SELECT user_id, firstname, lastname " . "FROM #__comprofiler " . "WHERE user_id IN (".$ids.")" ; $db->setQuery($sql); $users = $db->loadObjectList('user_id'); } else { $sql = "SELECT id, name AS firstname " . "FROM #__users " . "WHERE id IN (".$ids.")" ; $db->setQuery($sql); $users = $db->loadObjectList('id'); } foreach ($useridMapping as $item => $itemData) { foreach ($itemData as $itemField => $userId) { if ($userId == 0) { $items[$item]->$itemField = JText::_('RAFORMS_GUEST'); } else { $firstname = (property_exists($users[$userId], 'firstname')) ? $users[$userId]->firstname : ''; $lastname = (property_exists($users[$userId], 'lastname')) ? $users[$userId]->lastname : ''; $fullname = $lastname . " " . $firstname; if ($fullname == ' ') { $fullname = $users[$id]->username; } $items[$item]->$itemField = $fullname; } } } return $items; } protected function allowEditSelOptFromDb($data = array()) { // Check specific edit permission then general edit permission. foreach ($data as $id => $object) { $allowed = JFactory::getUser()->authorise('core.edit.own', $object->asset_id ); if ( ! $allowed ) { unset($data[$id]); } } return $data; } function getFilters() { $app = JFactory::getApplication(); $document = JFactory::getDocument(); $db = JFactory::getDBO(); $filter = array(); foreach($this->formFields as $formid => $fields) { foreach($fields as $fieldName => $field) { // skip field if it's not to be displayed if (($listfilterContainerArray = $this->recursiveArraySearchAll($this->listfilter, $formid . '_f_' . $fieldName)) === false) { continue; } $filterNames = array(); // check if there are columns to be filtered from other forms if (isset($this->filterMapping[$formid . '_f_' . $fieldName])) { $column = $this->filterMapping[$formid . '_f_' . $fieldName]; // now we have the column number .. we need all columns which are involved $filterNames = $this->recursiveArraySearchAll($this->filterMapping, $column); } else { $filterNames[] = $formid . '_f_' . $fieldName; } $filterNames = array_unique($filterNames); $listfilterPositionArray = $this->recursiveArraySearchAll($this->listfilter[$listfilterContainerArray[0]], $formid . '_f_' . $fieldName); $lvcContainer = $listfilterContainerArray[0]; $lvcPosition = $listfilterPositionArray[0]; // get the values in case it's a radio / select / checkbox $filterOptions = array(); foreach ($filterNames as $fn => $rawFilterName) { $filterFormid = preg_replace("/^([^_]*)_f_.*$/", "$1", $rawFilterName); $filterName = preg_replace("/^[^_]*_f_(.*)/", "$1", $rawFilterName); if (isset($this->formFields[$filterFormid]) && isset($this->formFields[$filterFormid][$filterName]) && isset($this->formFields[$filterFormid][$filterName]['seloptfromdb']) && $this->formFields[$filterFormid][$filterName]['seloptfromdb'] == 'true') { $db->setQuery($this->formFields[$filterFormid][$filterName]['seloptfromdbsql']); if (!$seloptfromdb = $db->loadObjectList()) { $seloptfromdb = array(); } else { if (preg_match("/asset_id/", $this->formFields[$filterFormid][$filterName]['seloptfromdbsql'])) { $seloptfromdb = $this->allowEditSelOptFromDb($seloptfromdb); } } foreach ($seloptfromdb as $row => $fieldInfo) { $value = $filterFormid . '_v_' . $filterName . '-' . $fieldInfo->{$this->formFields[$filterFormid][$filterName]['seloptfromdbvalue']}; $text = $fieldInfo->{$this->formFields[$filterFormid][$filterName]['seloptfromdbtext']}; $filterOptions[$value] = $text; } } if (isset($this->formFields[$filterFormid]) && isset($this->formFields[$filterFormid][$filterName]) && isset($this->formFields[$filterFormid][$filterName]['selopt']) && is_array($this->formFields[$filterFormid][$filterName]['selopt'])) { foreach ($this->formFields[$filterFormid][$filterName]['selopt'] as $id => $option) { if (isset($option['hidden']) && $option['hidden'] != 1) { $value = $filterFormid . '_v_' . $filterName . '-' . $id; $text = isset($option['option']) ? rtrim($option['option']) : ''; $filterOptions[$value] = $text; } } } } asort($filterOptions); $filterOptions = array_unique($filterOptions); $filter_value = $app->getUserStateFromRequest('filter.lvc_' . $this->catid . '_'.$field['fieldname'], 'filter_lvc_' . $this->catid . '_' . $formid . '_f_' . $field['fieldname']); // filter empty filter values if (is_array($filter_value)) { foreach ($filter_value as $filter_k => $filter_v) { if (strlen($filter_v) < 1) { unset($filter_value[$filter_k]); } } } $label = (array_key_exists('label', $field)) ? $field['label'] : ''; $filter_label = (array_key_exists('filterlabel', $field)) ? $field['filterlabel'] : $label ; switch ($field['elementtype']) { case 'textarea': $filter[$lvcContainer][$lvcPosition]['label'] = $filter_label; $filter[$lvcContainer][$lvcPosition]['element'] = "<input type='text' name='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] ."' value='".$filter_value."' >"; break; case 'text': $filter[$lvcContainer][$lvcPosition]['label'] = $filter_label; $filter[$lvcContainer][$lvcPosition]['element'] = "<input type='text' name='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] ."' value='".$filter_value."' >"; break; case 'select': $filter_value = (!is_array($filter_value)) ? array() : $filter_value; $filter[$lvcContainer][$lvcPosition]['label'] = $filter_label; if ($field['multi'] == 'true') { $multi = "multiple='multiple'"; $field['seloptsize'] += 1; } else { $multi = ""; } $filter[$lvcContainer][$lvcPosition]['element'] = "<select name='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] ."[]' size='".$field['seloptsize']."' ".$multi.">\n"; if (array_key_exists('seloptempty', $field) && strlen($field['seloptempty']) > 0) { $filter[$lvcContainer][$lvcPosition]['element'] .= "<option value=''>".$field['seloptempty']."</option>\n"; } else { $filter[$lvcContainer][$lvcPosition]['element'] .= "<option value=''> -- </option>\n"; } if (count($filterOptions) > 0) { foreach ($filterOptions as $value => $text) { $selected = (in_array($value, $filter_value)) ? " selected='selected'" : ""; $filter[$lvcContainer][$lvcPosition]['element'] .= "<option value='" . $value . "'" . $selected . ">" . $text . "</option>\n"; } } $filter[$lvcContainer][$lvcPosition]['element'] .= "</select>"; break; case 'radio': case 'checkbox': $filter_value = (!is_array($filter_value)) ? array() : $filter_value; $filter[$lvcContainer][$lvcPosition]['label'] = $filter_label; $filter[$lvcContainer][$lvcPosition]['element'] = "<input class='filter_checkbox' type='hidden' name='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] ."' value=''>"; $labelstyle = ($field['position'] == "1") ? 'float: left;' : 'float: right;'; $inputstyle = ($field['position'] == "1") ? 'float: right;' : 'float: left;'; $divstyle = ($field['hv'] == 1) ? 'float: left;' : 'float: left; clear: both;'; $labelstyle .= 'margin: 0 5px;'; if (count($filterOptions) > 0) { foreach ($filterOptions as $value => $text) { $checked = (in_array($value, $filter_value)) ? " checked='checked'" : ""; $filter[$lvcContainer][$lvcPosition]['element'] .= "<div style='" . $divstyle . "'>"; $filter[$lvcContainer][$lvcPosition]['element'] .= "<label for='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] . "' style='" . $labelstyle . "'>" . $text . "</label>"; $filter[$lvcContainer][$lvcPosition]['element'] .= "<input class='filter_checkbox filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] . "' type='checkbox' name='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] ."[]' value='" . $value . "' style='" . $inputstyle . "' ".$checked.">"; $filter[$lvcContainer][$lvcPosition]['element'] .= "</div>"; } } // handle checkbox selections $checked = ($app->getUserStateFromRequest('filter.form_' . $formid . '_' . $field['fieldname'] . '_all', 'filter_lvc_' . $this->catid . '_' . $formid . '_f_' . $field['fieldname'] . '_all') == 1) ? "checked='checked'" : ""; $filter[$lvcContainer][$lvcPosition]['element'] .= "<input class='filter_checkbox' type='hidden' name='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] . "_all' value=''>"; $filter[$lvcContainer][$lvcPosition]['element'] .= "<div style='" . $divstyle . "'>"; $filter[$lvcContainer][$lvcPosition]['element'] .= "<label for='filter_lvc_" . $this->catid . "_" . $formid . '_f_' . $field['fieldname']."_all' style='" . $labelstyle . "'>" . JText::_('RAFORMS_SELECT_ALL') . "</label>"; $filter[$lvcContainer][$lvcPosition]['element'] .= "<input id='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] . "_all' class='filter_checkbox' type='checkbox' name='filter_lvc_" . $this->catid . "_" . $formid . '_f_' . $field['fieldname']."_all' value='1' style='" . $inputstyle . "' ".$checked.">"; $filter[$lvcContainer][$lvcPosition]['element'] .= "</div>"; $fieldname = $field['fieldname']; $catid = $this->catid; $javascript = <<<EOT window.addEvent('domready', function() { $('filter_lvc_${catid}_${formid}_f_${fieldname}_all').addEvent('click', function () { var all = $('filter_lvc_${catid}_${formid}_f_${fieldname}_all').checked; $$('.filter_lvc_${catid}_${formid}_f_${fieldname}').each( function (el) { (all == true) ? el.checked = true : el.checked = false; }); }); var all = $('filter_lvc_${catid}_${formid}_f_${fieldname}_all').checked; $$('.filter_lvc_${catid}_${formid}_f_${fieldname}').each( function (el) { (all == true) ? el.checked = true : el.checked = el.checked; }); }); EOT; $document->addScriptDeclaration($javascript); break; case 'ip': $filter[$lvcContainer][$lvcPosition]['label'] = $filter_label; $filter[$lvcContainer][$lvcPosition]['element'] = "<input type='text' name='filter_lvc_" . $this->catid . "_" . $formid . "_f_" . $field['fieldname'] ."' value=''>"; break; case 'user': $sql = $db->getQuery(true); $sql->select("DISTINCT b.id as id, a.user, b.name"); $sql->from("#__raforms_" . (int) $formid . " a "); $sql->join("LEFT", "#__users b ON a.user = b.id"); $sql->order("b.name ASC"); $db->setQuery($sql); $users = $db->loadObjectList('id'); if (array_key_exists(null, $users)) { $users[null]->name = JTEXT::_('RAFORMS_GUEST'); $unknown = $users[null]; unset($users[null]); $users[] = $unknown; } if (array_key_exists(0, $users)) { $users[0]->name = JTEXT::_('RAFORMS_GUEST'); $unknown = $users[0]; unset($users[0]); $users[] = $unknown; } $filter[$lvcContainer][$lvcPosition]['label'] = $filter_label; $filter[$lvcContainer][$lvcPosition]['element'] = ' <select name="filter_lvc_' . $this->catid . '_' . $formid . '_f_user"> <option value="">-- ' . $filter_label . ' --</option>' . JHtml::_('select.options', $users, 'user', 'name', $filter_value) . ' </select>'; break; default : $pluginPath = JPATH_SITE . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'extension' . DIRECTORY_SEPARATOR . 'raform_' . $field['elementtype'] . DIRECTORY_SEPARATOR . 'site'; $plugin = $pluginPath . DIRECTORY_SEPARATOR . 'filter_' . $field['elementtype'] . '.php'; if (JFile::exists($plugin)) { include_once($plugin); $elementlabel = JFile::stripExt(JFile::getName($plugin)).'label'; $element = JFile::stripExt(JFile::getName($plugin)).'element'; $filter[$lvcContainer][$lvcPosition]['label'] = $elementlabel($field); $filter[$lvcContainer][$lvcPosition]['element'] = $element($field, $app->getUserStateFromRequest('filter.lvc_' . $this->catid . '_' . $formid . '_f_' . $field['fieldname'], 'filter_lvc_' . $this->catid . '_' . $formid . '_f_' . $field['fieldname']), $formid, 'filter_lvc_' . $this->catid . '_' . $formid . '_f_' . $field['fieldname']); } } } } return $filter; } function recursiveArraySearchAll($haystack, $needle, $index = null) { $aIt = new RecursiveArrayIterator($haystack); $it = new RecursiveIteratorIterator($aIt); $resultkeys = array(); while ($it->valid()) { if (!is_array($it->current())) { if ( ( (isset($index) AND ($it->key() == $index)) OR (!isset($index)) ) AND ( strpos($it->current(), $needle) !== false ) ) { $resultkeys[] = $aIt->key(); } } $it->next(); } if (!isset($resultkeys) || count($resultkeys) == 0) { return false; } else { return $resultkeys; } } public function getCatForms() { asort($this->catForms); return $this->catForms; } public function getFormsAccess() { return $this->formsAccess; } }