function listToHash($list, $keyName, $valueName) { $hash = array(); if (arrayIsOk($list)) { foreach($list as $item) { $hash[$item[$keyName]] = $item[$valueName]; } } return $hash; }
/** * @return array or value * @param string $cond * @param mixed $fields * @param string $table * @desc get oner row/value */ function get($cond = '', $fields = '', $table = '', $nonstrip = '') { //define type of return value if ($fields == '' or $fields == '*') { $fields = '*'; $return = array(); } else { if (arrayIsOk($fields)) { $fields = implode(',', $fields); $return = array(); } else { if (strpos($fields, ',') !== false) { $return = array(); } else { $return = ''; } } } //select table if ($table == '') { if ($this->table == '') { //error TABLE NOT SELECTED $this->error = array('message' => ERROR_NOTABLE); return false; } $table = $this->table; } // //build sql $sql = "select {$fields} from {$table}"; if ($cond != '') { if (is_array($cond)) { $sql .= " where "; foreach ($cond as $field => $value) { $value = addslashes($value); $sql .= "{$field}='{$value}' and "; } $sql = substr($sql, 0, strlen($sql) - 5); } else { $sql .= " where {$cond}"; } } // //execute and save time for debug $start_time = getmicrotime(); //print $sql; $res = mysql_query($sql); //print mysql_error(); $GLOBALS['SQLS'][] = array('sql' => $sql, 'time' => getmicrotime() - $start_time); // if (!$res) { //error MYSQL ERROR $this->__logging($sql); $this->error = array('message' => mysql_error()); return false; } //fetch result if (is_array($return)) { $return = _stripslashes(mysql_fetch_array($res, MYSQL_ASSOC), $nonstrip); if (!is_array($return)) { $return = false; } } else { $row = mysql_fetch_array($res, MYSQL_ASSOC); // by War 07-12-2004 if (preg_match('/,/', $fields)) { $return = _stripslashes(@$row, $nonstrip); } else { $return = _stripslashes(@$row[$fields], $nonstrip); } if ($return == '') { $return = false; } } // mysql_free_result($res); return $return; }
function makeElementView($value) { GLOBAL $parser; GLOBAL $db; GLOBAL $app; // get type info details if ($parser->element_type_info=='arr') { $matches[1]=$parser->element_type_info; } else { preg_match('/^([a-z]+)\_([a-z_0-9]+)$/', $parser->element_type_info, $matches); } switch ($matches[1]) { case 'def': $arr=$GLOBALS['LIST_'.strtoupper($matches[2])]; if (!arrayIsOk($arr)) { $app->raiseError('Array <b>LIST_'.strtoupper($matches[2]).'</b> not found. In parser->makeFormElements()'); } break; case 'table': $cross_field = 'name'; if (isset($parser->element_params['cross_field'])) { $cross_field = $parser->element_params['cross_field']; } return $db->get("id=$value",$cross_field,$matches[2]); break; case 'data': $data=$GLOBALS['DATA_'.strtoupper($matches[2])]; $list_table=$data['table']; // get field that will be shown $cross_field = 'name'; if (isset($parser->element_params['cross_field'])) { $cross_field = $parser->element_params['cross_field']; } if ($value!='') { $rez=$db->get("id=$value",$cross_field,$list_table); } return $rez; break; case 'arr': $arr=$parser->form_values; break; default: GLOBAL $app; $app->raiseError('Unknown list type <b>'.$parser->element_type_info.'</b> (list.type :: makeElementView)'); break; } return $arr[$value]; }
/** * @return boolean * @param array $struct * @desc check if auth structure is valid */ function _checkStruct($struct) { if (!arrayIsOk($struct)) { return false; } $required = array('table', 'default', 'login_page', 'logged_page', 'jump_to_logged_page', 'sid_name', 'pages', 'remove_auto_login'); $structOk = true; $structKeys = array_keys($struct); foreach ($required as $v) { if (!in_array($v, $structKeys)) { $structOk = false; break; } } return $structOk; }
function makeFormElement($value = null, $element_params = null) { GLOBAL $parser; GLOBAL $db; GLOBAL $app; // get type info details if ($parser->element_type_info == 'arr') { $matches[1] = $parser->element_type_info; } else { preg_match('/^([a-z]+)\_([a-z_]+)$/', $parser->element_type_info, $matches); } switch ($matches[1]) { case 'def': $arr = $GLOBALS['LIST_'.strtoupper($matches[2])]; if (!arrayIsOk($arr)) { $app->raiseError('Array <b>LIST_'.strtoupper($matches[2]).'</b> not found. In parser->makeFormElements()'); } break; case 'table': $cross_field = 'name'; if (isset($parser->element_params['cross_field'])) { $cross_field = $parser->element_params['cross_field']; } $arr = $db->select($element_params['cond'], '', 'id,'.$cross_field.' as name', $matches[2]); break; case 'data': $data = $GLOBALS['DATA_'.strtoupper($matches[2])]; $listbox_table = $data['table']; $cross_field = 'name'; if (isset($parser->element_params['cross_field'])) { $cross_field = $parser->element_params['cross_field']; } $arr = $db->select($element_params['cond'], $data['order'], 'id,'.$cross_field.' as name', $listbox_table); break; case 'arr': $arr = $parser->form_values; break; default: GLOBAL $app; $app->raiseError('Unknown listbox type <b>'.$parser->element_type_info.'</b> (listbox.type :: makeFormElements)'); break; } if (arrayIsOk($arr)) { // add additional values if (arrayIsOk($element_params['add_values'])) { $arr = array_merge($element_params['add_values'], $arr); } $this->__makeListboxFormElement($arr, $value, $element_params['form']); } return trim($parser->tpl->get()); }
/** * @return void * @param array $arr - hash^2 of values * @param int $cols - number of columns * @param string $block_name * @param template &$tpl * @desc parse table data */ function __tableHandler($arr, $cols, $block_name = 'table', &$tpl) { $table_name = $block_name; $column_separator_left_name = $block_name . '_column_separator_left'; $column_separator_right_name = $block_name . '_column_separator_right'; $column_separator_name = $block_name . '_column_separator'; $column_name = $block_name . '_column'; $row_separator_name = $block_name . '_row_separator'; $row_name = $block_name . '_row'; $empty_name = $block_name . '_empty'; $empty_separator_name = $block_name . '_empty_separator'; $size = sizeof($arr); // size of array $rows = round($size / $cols); // number of rows if ($rows * $cols < $size) { $rows++; } $i = 0; // current element $c = 0; // current column $r = 0; // current row $add = $this->__getAddVariables(); // get add variables if (is_array($arr)) { foreach ($arr as $row) { // new row begining if ($c == 0) { $tpl->touchBlock($column_separator_left_name); } // make column separator if ($c != 0) { $tpl->touchBlock($column_separator_name); } // parse current column variables $tpl->setCurrentBlock($column_name); $row = array_merge($row, $add); // add images fields if needs if (arrayIsOk($this->fields)) { foreach ($this->fields as $name => $type) { if (substr($type, 0, 5) == 'image' && $row['id'] != '') { $row[$name] = $row['id']; } if (substr($type, 0, 4) == 'file' && $row['id'] != '') { $row[$name] = $row['id'] . '_' . $row[$name]; } } } foreach ($row as $key => $value) { if ($this->fields[$key] != '') { $this->setElement($key, $this->fields[$key]); $value = $this->makeElementView($value, $row['id']); } if (!arrayIsOk($value)) { $tpl->setVariable(array(strtoupper($key) => $value)); } else { $tpl->setVariable($value); } } $tpl->parseCurrentBlock(); $c++; // column parsed - new column // row ending if ($c == $cols) { $tpl->touchBlock($column_separator_right_name); } // row separator processing if (++$i % $cols == 0) { if (sizeof($arr) - $i > 0) { $tpl->touchBlock($row_separator_name); } $tpl->setCurrentBlock($row_name); $tpl->parseCurrentBlock(); $c = 0; $r++; } } } else { // $tpl->touchBlock($empty_name); } // empty columns parsing if ($i % $cols != 0) { while ($i++ % $cols != 0) { // empty column separator if ($c != 0) { $tpl->touchBlock($empty_separator_name); } // empty column $tpl->touchBlock($empty_name); // new column $c++; } // table column separator right $tpl->touchBlock($column_separator_right_name); // make row $tpl->setCurrentBlock($row_name); $tpl->parseCurrentBlock(); } // parse main block table & add caption variables $tpl->setCurrentBlock($table_name); if (isset($this->caption_variables)) { $tpl->setVariable($this->caption_variables); } $tpl->parseCurrentBlock(); // remove caption & add variables $this->__clearAddVariables(); }
function nodeSort($parent, $rows, $cl, $first_node_field_name) { STATIC $rez; if (arrayIsOk($rows)) { foreach($rows as $row) { if ($row['parent_id']==$parent) { $rez[$row['id']] = str_repeat(" ",substr_count($row['parents'],">")*2).$row[$first_node_field_name]; $this->nodeSort($row['id'], $rows, $cl, $first_node_field_name); } } } return $rez; }
/** * Enter description here... * * @param array $variable * @return string */ function addGetVariable($variable) { if (arrayIsOk($variable)) { // create url with get variables that are not in $variable array $return = $this->getBaseName() . '?'; if (arrayIsOk($_GET)) { foreach ($_GET as $key => $value) { if (!in_array($key, array_keys($variable)) && strpos($_SERVER['QUERY_STRING'], $key . '=') !== false) { $return .= $key . '=' . $value . '&'; } } } foreach ($variable as $key => $value) { $return .= $key . '=' . $value . '&'; } $return = substr($return, 0, -1); return $return; } else { if ($variable != '') { $this->raiseError('$app->addGetVariable require ARRAY !!!'); } return $this->getCurUrl(); } }
/** * on success returns html code of parsed faq * * @return string */ function makeFaqFullList() { GLOBAL $data; GLOBAL $parser; if (FAQ_TYPE==2) { $data->set('faq_section'); $sections=$data->select('id!=1'); if (arrayIsOk($sections)) { foreach ($sections as $key=>$section) { $data->set('faq'); $faqs=$data->makeList("parent_id={$section['id']}",'modules/faq','faq_full_list'); $sections[$key]['faqs']=$faqs; } } $data->set('faq_section'); $data->setParserFields(); return $parser->makeList($sections,'modules/faq','faq_full_section_list'); } }
function makeElementView($value) { global $parser; global $db; global $app; global $data; $element_params = $parser->element_params; $tree = $GLOBALS['TREE_' . strtoupper($parser->element_type_info)]; if (!arrayIsOk($tree)) { $app->raiseError('Category type error. <b>TREE_' . strtoupper($parser->element_type_info) . '</b> not found. In category.type.php->makeElementView()'); } $cross_field = isset($element_params['cross_field']) ? $element_params['cross_field'] : 'name'; $node = $tree['node']; $point = $tree['point']; $old_data = $data->name; $data->set($node); $parents = $db->get("id={$value}", 'parents', $data->table) . '<' . $value . '>'; $parents_ids = treeToArray($parents); $parent_in = implode(',', $parents_ids); $rows = $db->select("id IN ({$parent_in})", '', "id,{$cross_field}", $data->table); //sort result by known parents $new_rows = array(); $hash = listToHash($rows, 'id', $cross_field); foreach ($parents_ids as $v) { if (isset($hash[$v])) { $new_rows[$v] = $hash[$v]; } } if ($this->view_root === false) { reset($new_rows); unset($new_rows[key($new_rows)]); //unset first } //back from hash to array $rows = array_values($new_rows); $data->set($old_data); $rez = arrayIsOk($rows) ? implode($this->delimiter, $rows) : ''; return $rez; }
/** * @return string * @param string $cond * @param array $fields * @desc make data dynamic list */ function makeDynamicList($cond = '', $fields = null) { global $parser; if (!isset($fields)) { $fields = $this->fields; } $arr = $this->select($cond, $this->order, $fields); if (arrayIsOk($arr)) { $parser->setListColumns($this->fields_name); $this->setParserFields(); return $parser->makeDynamicList($arr); } }