AAAAdb_multi_record_loader.php000066600000030070151375013250011746 0ustar00 0, 'notfound' => 0, 'nodata' => 0); var $group = array('id' => 'data_operations', 'title' => 'Data/DB Operations'); var $details = array('title' => 'DB Multi Record Loader', 'tooltip' => 'Load Database table records based on a request parameter value or a custom DB query.'); function run($form, $actiondata){ $params = new JParameter($actiondata->params); $table_name = $params->get('table_name', ''); if(!empty($table_name) && (int)$params->get('load_data', 1) == 1){ $mainframe = JFactory::getApplication(); $database = JFactory::getDBO(); $table_field = $params->get('dbfield', ''); if(trim($actiondata->content1)){ $where = " WHERE ".$this->_processWhere(trim($actiondata->content1), $form); }else if(!empty($table_field)){ //$where = " WHERE `".$params->get('dbfield', '')."` = '".$form->data[$params->get('request_param', '')]."'"; $req_param = $form->get_array_value($form->data, explode('.', $params->get('request_param', ''))); if(is_null($req_param)){ $req_param = ''; $where = " WHERE `".$table_field."` = '".$form->escapeVar($req_param)."'"; }else{ if(is_array($req_param) && !empty($req_param)){ $where = " WHERE `".$table_field."` IN ('".implode("','", $form->escapeVar($req_param))."')"; }else{ $where = " WHERE `".$table_field."` = '".$form->escapeVar($req_param)."'"; } } }else{ $where = ""; } //load the model_id $model_id_sub = preg_replace('/(?:^|_)(.?)/e', "strtoupper('$1')", $table_name); $model_id = $params->get('model_id', ''); if(empty($model_id)){ $model_id = $model_id_sub; } //check fields $fields = array(); if(trim($params->get('fields', ''))){ $fields_list = explode(",", trim($params->get('fields', ''))); foreach($fields_list as $field){ $fields[] = "`".$field."`"; } }else{ $fields = array("`".$model_id."`.*"); } //check the association $assoc = ''; $assoc_models = array(); $primaries = array(); if($params->get('enable_association', 0) == 1 && $params->get('load_data', 1) == 1){ if(trim($params->get('associated_models', ''))){ //add primary key for the main table $fields_list = $database->getTableFields(array(trim($params->get('table_name', ''))), false); $table_fields = $fields_list[trim($params->get('table_name', ''))]; foreach($table_fields as $table_field => $field_data){ if($field_data->Key == 'PRI'){ $primaries[$model_id] = $table_field; } } //find associated models list $assoc_models = explode(',', $params->get('associated_models')); foreach($assoc_models as $k => $assoc_model){ $assoc_models[$k] = trim($assoc_model); } //find other multi record loaders $models_list = array(); $wheres_list = array(); foreach($form->form_actions as $form_action){ if($form_action->type == 'db_multi_record_loader'){ $action_params = new JParameter($form_action->params); if(trim($action_params->get('model_id', '')) && trim($action_params->get('table_name', ''))){ $models_list[trim($action_params->get('model_id', ''))] = array('table' => trim($action_params->get('table_name', ''))); $models_list[trim($action_params->get('model_id', ''))]['join_type'] = trim($action_params->get('join_type', 'INNER')); $models_list[trim($action_params->get('model_id', ''))]['join_rule'] = trim($action_params->get('join_rule', '')); if(in_array(trim($action_params->get('model_id', '')), $assoc_models)){ //find some table info (primary key) $fields_list = $database->getTableFields(array(trim($action_params->get('table_name', ''))), false); $table_fields = $fields_list[trim($action_params->get('table_name', ''))]; foreach($table_fields as $table_field => $field_data){ if($field_data->Key == 'PRI'){ $primaries[trim($action_params->get('model_id', ''))] = $table_field; } } //get table fields list if(trim($action_params->get('fields', ''))){ $table_fields = explode(",", trim($action_params->get('fields', ''))); foreach($table_fields as $table_field){ $table_field = trim($table_field); $field_alias = "`".trim($action_params->get('model_id', '')).".".$table_field."`"; $field_name = "`".trim($action_params->get('model_id', ''))."`.`".$table_field."`"; $fields[] = $field_name." AS ".$field_alias; } }else{ foreach($table_fields as $table_field => $field_data){ $fields[] = "`".trim($action_params->get('model_id', ''))."`.`".$table_field."` AS `".trim($action_params->get('model_id', '')).".".$table_field."`"; } //$fields[] = "`".trim($action_params->get('model_id', ''))."`.*"; } //append any WHERE data if(trim($form_action->content1)){ if(!empty($where)){ //$where .= " AND ".$this->_processWhere($form_action->content1, $form); $wheres_list[trim($action_params->get('model_id', ''))] = " ON ".$this->_processWhere($form_action->content1, $form); }else{ $where = " WHERE ".$this->_processWhere($form_action->content1, $form); } } } } } } //build the JOIN statement foreach($assoc_models as $assoc_model){ if(isset($models_list[$assoc_model])){ $assoc .= " ".$models_list[$assoc_model]['join_type']." JOIN `".$models_list[$assoc_model]['table']."` AS `".$assoc_model."`"; if(strlen(trim($models_list[$assoc_model]['join_rule'])) > 0){ $assoc .= " ON ".$models_list[$assoc_model]['join_rule']; } if(isset($wheres_list[$assoc_model])){ $assoc .= $wheres_list[$assoc_model]; } } } } } //add the page navigation data if((bool)$params->get('enable_pagination', 0) === true && $params->get('load_type', 'all') == 'all'){ $option = 'com_chronoforms.db_multi_record_loader.'.$actiondata->id; $session = JFactory::getSession(); if(isset($form->data['limit'])){ $session->set($option.'.limit', (int)$form->data['limit'], md5('chrono')); } $limit = $mainframe->getUserStateFromRequest($option.'.limit', 'limit', $session->get($option.'.limit', (int)$params->get('pagination_limit', 50), md5('chrono')), 'int'); $limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int'); $sql = "SELECT count(*) FROM `".$params->get('table_name', '')."` AS `".$model_id."`".$assoc.$where; $database->setQuery($sql); $total = $database->loadResult(); jimport('joomla.html.pagination'); $pageNav = new JPagination($total, $limitstart, $limit); //get current page url //load show_html action helper class $form->loadActionHelper('show_html'); $url = CfactionShowHtmlHelper::selfURL(); if($form->admin === false){ $form->paginatior_footer = '
'; }else{ $form->paginatior_footer = ''; } $form->paginatior_footer .= $pageNav->getListFooter(); $form->paginatior_footer .= '
'; //check order data $order = ''; if(isset($form->data['order'])){ $order = ' ORDER BY `'.trim($form->data['order']).'`'.(isset($form->data['direction']) ? ' '.$form->data['direction']: ' ASC'); } $fields = implode(", ", $fields); if((bool)$params->get('count', 0) === true){ $fields = 'count(*)'; } $sql = "SELECT ".$fields." FROM `".$params->get('table_name', '')."` AS `".$model_id."`".$assoc.$where.$order." LIMIT $pageNav->limitstart,$pageNav->limit"; }else{ $fields = implode(", ", $fields); if((bool)$params->get('count', 0) === true){ $fields = 'count(*)'; } $sql = "SELECT ".$fields." FROM `".$params->get('table_name', '')."` AS `".$model_id."`".$assoc.$where; } //echo $form->paginatior_footer; //add debug data $form->debug['db_multi_record_loader'][] = $sql; //run the sql and get the data $database->setQuery($sql); if($params->get('load_type', 'all') == 'first'){ $data = $database->loadAssoc(); }else{ $data = $database->loadAssocList(); } //process the data if association was enabled if(!empty($assoc_models) && !empty($data)){ if($params->get('load_type', 'all') == 'first'){ foreach($data as $k => $v){ if(strpos($k, '.')){ $details = explode('.', $k); if((bool)$params->get('group_model_data', 1) === true){ $data[$details[0]][$details[1]] = $v; }else{ $form->data[$details[0]][$details[1]] = $v; } unset($data[$k]); } } }else{ foreach($data as $datak => $datav){ if(is_array($datav)){ foreach($datav as $k => $v){ if(strpos($k, '.')){ $details = explode('.', $k); $data[$datak][$details[0]][$details[1]] = $v; unset($data[$datak][$k]); }/*else{ $data[$datak][$model_id][$k] = $v; unset($data[$datak][$k]); }*/ } } } if((int)$params->get('group_model_data', 1) == 1){ $data = $this->group_model_data($data, $model_id, $primaries); } } } //print_r2($data); //data must be loaded under some model id $form->data[$model_id] = $data; //check the result //$request_val = $form->data[$params->get('request_param', '')]; if(empty($data)){ $this->events['notfound'] = 1; }else{ $this->events['found'] = 1; } //print_r2($form->data); } } function group_model_data($data, $main_model_id, $primaries){ if(!empty($primaries)){ if(isset($primaries[$main_model_id])){ $primary = $primaries[$main_model_id]; unset($primaries[$main_model_id]); }else{ return $data; } $unique_values = array(); $new_data = array(); foreach($data as $datak => $datav){ if(is_array($datav)){ if(isset($datav[$primary])){ if(!isset($unique_values[$datav[$primary]])){ $unique_values[$datav[$primary]] = $datak; //$new_data[] = $datav; foreach($primaries as $model => $pr){ if(isset($datav[$model])){ $temp_model_data = $datav[$model]; unset($datav[$model]); $datav[$model][] = $temp_model_data; } } $new_data[$datak] = $datav; }else{ foreach($primaries as $model => $pr){ if(isset($datav[$model])){ $temp_model_data = $datav[$model]; unset($datav[$model]); $new_data[$unique_values[$datav[$primary]]][$model][] = $temp_model_data; } } } } } } foreach($primaries as $model => $pr){ foreach($new_data as $k => $v){ if(is_array($v) && isset($v[$model])){ $new_data[$k][$model] = $this->group_model_data($v[$model], $model, $primaries); } } } $data = $new_data; } return $data; } function _processWhere($code, $form){ ob_start(); eval("?>".$code); $code = ob_get_clean(); return $code; } function load($clear){ if($clear){ $action_params = array( 'dbfield' => '', 'table_name' => '', 'request_param' => '', 'load_data' => 1, 'model_id' => '', 'fields' => '', 'count' => 0, 'join_type' => 'INNER', 'join_rule' => '', 'load_type' => 'all', 'enable_association' => 0, 'associated_models' => '', 'group_model_data' => 1, 'pagination_limit' => '50', 'enable_pagination' => 0, 'data_display_fields' => '', 'enable_data_displayer' => 0, 'data_order_fields' => '', 'content1' => '' ); } return array('action_params' => $action_params); } } ?>db_multi_record_loader.ctp000066600000031311151375013250011744 0ustar00
DB Multi Record Loader
Header(array('basic' => 'Basic', 'advanced' => 'Advanced', 'displayer' => 'Data Displayer', 'help' => 'Help'), 'db_multi_record_loader_config_{n}'); ?> tabStart('basic'); ?> input('action_db_multi_record_loader_{n}_dbfield_config', array('type' => 'text', 'label' => "DB Field", 'class' => 'medium_input', 'value' => '', 'smalldesc' => "The field name which will be used to query the table record, if left empty then all records will be loaded.")); ?> getTableList(); $options = array(); foreach($tables as $table){ $options[$table] = $table; } ?> input('action_db_multi_record_loader_{n}_table_name_config', array('type' => 'select', 'label' => 'Table', 'options' => $options, 'empty' => " - ", 'class' => 'medium_input', 'smalldesc' => "The table name to load the data from.")); ?> input('action_db_multi_record_loader_{n}_request_param_config', array('type' => 'text', 'label' => "Request Param", 'class' => 'medium_input', 'value' => '', 'smalldesc' => "The param name which will exist in the request url to the form, its value will be used to load the target db records, if the value of this parameter is an array then the array values will be used inside 'IN' statement.")); ?> input('action_db_multi_record_loader_{n}_model_id_config', array('type' => 'text', 'label' => "Model ID", 'class' => 'medium_input', 'value' => '', 'smalldesc' => "The key under which the loaded record data will be stored in the form->data array.
this is obligatory, if left empty then a camilized version of the table name will be used, e.g: jos_my_table = JosMyTable")); ?> input('action_db_multi_record_loader_{n}_fields_config', array('type' => 'text', 'label' => "Fields", 'class' => 'big_input', 'label_over' => true, 'smalldesc' => "List of comma separated fields names to load from this table (field_name1,field_name2..etc), leave empty to load all fields.")); ?> tabEnd(); ?> tabStart('advanced'); ?> input('action_db_multi_record_loader_{n}_load_data_config', array('type' => 'select', 'label' => 'Load Data', 'options' => array(0 => 'No', 1 => 'Yes'), 'class' => 'medium_input', 'smalldesc' => "Do you want to load the data of this model OR just use it in the models associations ?")); ?> input('action_db_multi_record_loader_{n}_load_type_config', array('type' => 'select', 'label' => 'Data Load Type', 'options' => array('all' => 'ALL', 'first' => 'First Record'), 'smalldesc' => "Which data to load ? All data (normal behavior) OR first record only (similar to the Record Loader Action).")); ?> input('action_db_multi_record_loader_{n}_enable_association_config', array('type' => 'select', 'label' => 'Enable Associations', 'options' => array(0 => 'No', 1 => 'Yes'), 'class' => 'medium_input', 'smalldesc' => "Do you want to enable the associations for this model ? this will allow you to load data from multiple tables and have them associated together.")); ?> input('action_db_multi_record_loader_{n}_join_type_config', array('type' => 'select', 'label' => 'JOIN Type', 'options' => array('INNER' => 'INNER', 'LEFT' => 'LEFT', 'RIGHT' => 'RIGHT'), 'smalldesc' => "The JOIN type used in case of an association.")); ?> input('action_db_multi_record_loader_{n}_join_rule_config', array('type' => 'text', 'label' => 'JOIN Rule', 'class' => 'big_input', 'smalldesc' => "The JOIN rule used in case of an association, example: MODEL_ID1.field1=MODEL_ID2.field2")); ?> input('action_db_multi_record_loader_{n}_associated_models_config', array('type' => 'text', 'label' => "Associated Models", 'class' => 'big_input', 'label_over' => true, 'smalldesc' => "list of models ids to associate this one with, comma separated, exactly as they are in other 'Multi DB Record Loader' configs.")); ?> input('action_db_multi_record_loader_{n}_group_model_data_config', array('type' => 'select', 'label' => 'Group Model Data', 'options' => array(0 => 'No', 1 => 'Yes'), 'class' => 'medium_input', 'smalldesc' => "This will add a bit of overhead on the server, but will remove any duplicates from the results and will group any associated models data under the same item.")); ?> input('action_db_multi_record_loader_{n}_content1_config', array('type' => 'textarea', 'label' => 'WHERE statement', 'rows' => 10, 'cols' => 50, 'smalldesc' => "The code used for the WHERE statement, some notes:
1 - leave empty to use the default request param with column name formula (associations not enabled), OR to load ALL records (associations enabled).
2 - don't use the WHERE word.
3 - in case of associations, pay attention to write the join rule, e.g: `User`.`id` = `Profile`.`user_id`
4 - in case of associations, if other associated models have WHERE statements then all the WHERE data will be appended using AND.
5 - You can use PHP code with tags. ")); ?> tabEnd(); ?> tabStart('displayer'); ?> input('action_db_multi_record_loader_{n}_enable_data_displayer_config', array('type' => 'select', 'label' => 'Enable Data Displayer', 'options' => array(0 => 'No', 1 => 'Yes'), 'class' => 'medium_input', 'smalldesc' => "Do you want to enable the generic data displayer ?")); ?> input('action_db_multi_record_loader_{n}_data_display_fields_config', array('type' => 'text', 'label' => "Display Fields", 'class' => 'big_input', 'smalldesc' => "Enter comma separated list of fields and titles in this format:
field_name:Field_Title
e.g:field1:Name,field2:Company")); ?> input('action_db_multi_record_loader_{n}_data_order_fields_config', array('type' => 'text', 'label' => "Order Fields", 'class' => 'big_input', 'smalldesc' => "Enter comma separated list of fields names to be sortable.")); ?> input('action_db_multi_record_loader_{n}_enable_pagination_config', array('type' => 'select', 'label' => 'Enable Pagination', 'options' => array(0 => 'No', 1 => 'Top', 2 => 'Bottom'), 'class' => 'medium_input', 'smalldesc' => "Do you want to enable the pagination feature for the records returned ? if so, where do you want it displayed ?")); ?> input('action_db_multi_record_loader_{n}_pagination_limit_config', array('type' => 'text', 'label' => "Limit", 'class' => 'small_input', 'smalldesc' => "The number of records per page.")); ?> tabEnd(); ?> tabStart('help'); ?>

tabEnd(); ?>
cfaction_db_multi_record_loader.ctp000066600000000231151375013250013607 0ustar00load($form, $actiondata); ?>.htaccess000066600000000177151375013250006355 0ustar00 Order allow,deny Deny from all cfaction_db_multi_record_loader.php000066600000010600151375013250013611 0ustar00params); $output = ''; if((bool)$params->get('enable_data_displayer', 0) === true && $params->get('load_type', 'all') == 'all'){ //find the model data $table_name = $params->get('table_name', ''); $model_id_sub = preg_replace('/(?:^|_)(.?)/e', "strtoupper('$1')", $table_name); $model_id = $params->get('model_id', ''); if(empty($model_id)){ $model_id = $model_id_sub; } //pre output creation $fields_names = array(); $fields_headings = array(); $order_fields = array(); //get order fields $order_str = trim($params->get('data_order_fields', '')); if(!empty($order_str)){ $order_fields = explode(",", trim($params->get('data_order_fields', ''))); } //get display fields $fields_data = trim($params->get('data_display_fields', '')); if(!empty($fields_data)){ $fields_data = explode(",", $fields_data); foreach($fields_data as $field_data){ $field_data = explode(":", trim($field_data)); $fields_names[] = $field_name = $field_data[0]; if(!isset($field_data[1])){ $field_data[1] = strtoupper($field_data[0]); } $fields_headings[] = $field_heading = $field_data[1]; } } //create the table code $output .= ''; $output .= ''; $output .= ''; $form->loadActionHelper('show_html'); $showHTMLHelper = new CfactionShowHtmlHelper(); foreach($fields_headings as $k => $field_heading){ if(in_array($fields_names[$k], $order_fields)){ $direction = 'asc'; if(isset($form->data['order']) && ($form->data['order'] == $fields_names[$k]) && isset($form->data['direction'])){ if($form->data['direction'] == 'asc'){ $direction = 'desc'; }else{ $direction = 'asc'; } } $class = ''; if(isset($form->data['order']) && ($form->data['order'] == $fields_names[$k])){ $class = ' direction_'.(isset($form->data['direction']) ? $form->data['direction']: 'asc'); } $field_heading = ''.$field_heading.''; }else{ $field_heading = $field_heading; } $output .= ''; } $output .= ''; $output .= ''; $output .= ''; $i = 0; if(!empty($form->data[$model_id])){ foreach($form->data[$model_id] as $r => $record){ $output .= ''; foreach($fields_names as $k => $field_name){ if(strpos($field_name, '.') !== false){ $record[$field_name] = $form->get_array_value($record, explode('.', $field_name)); } $output .= ''; } $output .= ''; $i = 1 - $i; } } $output .= ''; $output .= '
'.$field_heading.'
'.$record[$field_name].'
'; } if((bool)$params->get('enable_pagination', 0) === true && isset($form->paginatior_footer)){ //check the position of the pagination if((int)$params->get('enable_pagination', 0) == 1){ $output = $form->paginatior_footer.$output; }else if((int)$params->get('enable_pagination', 0) == 2){ $output = $output.$form->paginatior_footer; } } //add CSS $this->_addCSS($form, $actiondata); //end echo $output; } function _addCSS($form, $actiondata){ $document = JFactory::getDocument(); //add some CSS formatting for pagination ob_start(); ?> .list-footer ul li { display: inline; } #db_multi_record_loader_id; ?> { width: 100%; margin: 0px 0px; } #db_multi_record_loader_id; ?> .cell { padding: 2px; } #db_multi_record_loader_id; ?> .cell { padding: 2px; } #db_multi_record_loader_id; ?> thead { background-color: #dedede; } addStyleDeclaration($script); } } ?>index.html000066600000000035151375013250006545 0ustar00