Site Datamanager Error. Method = ".$func ."
"; echo mysql_error()."
"; return true; } else return false; } // -------------------------------------------------------------------- // --------------------- Base site Datamanager ------------------- //--------------------------------------------------------------------- class dataman { var $adminTarget = ''; // ------ данные для подключения к базе var $dbhost; var $dbuser; var $dbpasswd; var $dbname; var $dbtable ; // имя базовой таблицы, хранящей общую структуру var $dbconnection; var $datamanagers = array(); function addDatamanager($name) { $this->datamanagers[sizeof($this->datamanagers)] = $name; } function setAdminTarget($str) { $this->adminTarget = $str; } function query($str,$message='Произошла ошибка в работе сайта! Приношу свои извинения. Попробуйте зайти снова.') { //echo $str.'
'; $result = mysql_db_query($this->dbname, $str, $this->dbconnection); if ($result == false) { echo "
".$str."
"; echo "
Site Datamanager Error. Method = ".$this->classname.$message."
"; echo mysql_error()."
"; exit; } else { //echo mysql_insert_id($this->dbconnection).'
'; //error_log($str.";\n", 3, "query.log"); return $result; } } function dataman($structTable = 'dmstruct') { GLOBAL $dbhost, $dbuser, $dbpasswd, $dbname; $this->dbhost = $dbhost; $this->dbuser = $dbuser; $this->dbpasswd = $dbpasswd; $this->dbname = $dbname; $this->dbtable = $structTable; $this->dbconnection = mysql_pconnect($this->dbhost, $this->dbuser , $this->dbpasswd ); if (dm2DbError($this->dbconnection, "init connection")) exit; } function addObject($fdmclass = '-',$fdmparent = '-') { GLOBAL $old_id,$dmclass,$dmparent,$lang,$s_relnum; switch ($lang) { case 'eng': GLOBAL $name; $post = ''; break; case 'chi': GLOBAL $name_chi; $post = '_chi'; $name = $name_chi; break; case 'jap': GLOBAL $name_jap; $post = '_jap'; $name = $name_jap; break; case 'kor': GLOBAL $name_kor; $post = '_kor'; $name = $name_kor; break; } if ($fdmclass != '-') $dmclass = $fdmclass; if ($fdmparent != '-') $dmparent = $fdmparent; if (!isset($s_relnum)) { $_result = $this->query("SELECT * FROM ".$this->dbtable." WHERE parent='".$dmparent."' AND dataclass='".$dmclass."' order by relnum desc", '.addObject()-1'); if ($nextItem = mysql_fetch_object($_result)) $relnum = $nextItem->relnum + 1; else $relnum = 1; } else { $relnum = $s_relnum; } $_result = $this->query('INSERT INTO '.$this->dbtable.' (parent, dataclass,relnum,rel_jap,rel_chi,rel_kor,mdate,cdate,name'.$post.',old_id) VALUES ( "'.$dmparent.'","'.$dmclass.'", "'.$relnum.'", "'.$relnum.'", "'.$relnum.'", "'.$relnum.'", "'.time().'", "'.time().'", "'.$name.'", "'.$old_id.'")', '.addObject()-2'); $ins_id = mysql_insert_id($this->dbconnection); $this->addObjectData($ins_id); return $ins_id; } function addObjectData($id) { GLOBAL $dmclass,$dmparent,$s_structure; if (!isset($s_structure)) { $structure = $this->getObjectStructure($dmclass); } else { $structure = $s_structure; } $length = sizeof($structure); if ($length < 1) return $id; $str1 = 'INSERT INTO '.$dmclass.' ( id, '; $str2 = ") VALUES ( '".$id."', "; for ($i=0;$i<$length;$i++) { GLOBAL $$structure[$i]['name']; $tmpStr = $$structure[$i]['name']; $str1 .= $structure[$i]['name']; switch ($structure[$i]['type']) { case 'string': case 'blob': case 'text': $str2 .= "'".urldecode($tmpStr)."'"; break; default : $str2 .= "'".$tmpStr."'"; break; } if ($i+1<$length) { $str2 .= ', '; $str1 .= ', '; } else { $str2 .= ' '; $str1 .= ' '; } } $str = $str1.$str2.")"; $_result = $this->query($str, '.addObjectData()'); // echo "data added: ".$id; return $id; } function editObject($item) { Global $no_all,$lang; if (!is_object($item)) $item = $this->getItem($item); switch ($lang) { case 'eng': GLOBAL $name; $post = ''; break; case 'chi': GLOBAL $name_chi; $post = '_chi'; $name = $name_chi; break; case 'jap': GLOBAL $name_jap; $post = '_jap'; $name = $name_jap; break; case 'kor': GLOBAL $name_kor; $post = '_kor'; $name = $name_kor; break; } $structure = $this->getObjectStructure($item->dataclass); $length = sizeof($structure); $_result = $this->query("UPDATE ".$this->dbtable." SET mdate='".time()."', name".$post."='".$name."' WHERE id = '".$item->id."'", '.editObject()'); $str1 = 'UPDATE '.$item->dataclass.' SET '; $str2 = ''; for ($i=0;$i<$length;$i++) { GLOBAL $$structure[$i]['name']; $tmpStr = $$structure[$i]['name']; if (isset($no_all) && $tmpStr == '') { } else { switch ($structure[$i]['type']) { case 'string': case 'blob': case 'text': $str2 .= $structure[$i]['name'].' = "'.$tmpStr.'"'; break; default : $str2 .= $structure[$i]['name'].' = "'.$tmpStr.'"'; break; } $str2 .= ', '; } } $str3 = ' WHERE id = "'.$item->id.'"'; if ($str2 == '') return; $str2 = substr($str2,0,(strlen($str2)-2)); //echo $str1.$str2.$str3; $_result = $this->query($str1.$str2.$str3, '.editObject()'); } function dropObject($structID, $changeRelative = true) { GLOBAL $delButton; $item = $this->getItem($structID,false); $parent = $item->parent; if ($delButton != "DELETE") return $parent; // ---------- drop external item information $exist = $this->getTableExists($item->dataclass); if (!$exist) $_result = $this->query("DELETE FROM ".$item->dataclass." WHERE id = '".$item->id."'", '.dropObject()-1'); // ------------- delete item $_result = $this->query("DELETE FROM ".$this->dbtable." WHERE id = '".$structID."'", '.dropObject()-4'); if ($item->dataclass == 'product') $_result = $this->query("DELETE FROM prod_ref WHERE prod_id = '".$structID."'", '.dropObject()-4'); // ----------- find all similar items with relativ numbers greate AND decrease these relative numbers if ($changeRelative) $this->orderRelnum($item->parent, $item->dataclass); // ------- find and delete all subitems $_childs = $this->getChilds($structID,'any',false); $length = sizeof($_childs); for ($i=0; $i < $length; $i++) { $this->dropObject($_childs[$i]->id,false); } return $parent; } function goBottom($structID) { $item = $this->getItem($structID,false); if ($item->relnum <= 0) return false; if ($item->relnum == 1) return true; $_childs = $this->getChilds($item->parent,$item->dataclass,false); $length = sizeof($_childs); for ($i=0; $i < $length; $i++) { $nextItem = $_childs[$i]; $str=""; if ($nextItem->id == $item->id) { $str = 'UPDATE '.$this->dbtable.' SET relnum = "1" WHERE id = "'.$nextItem->id.'"'; } else { if ($nextItem->relnum < $item->relnum) $str = 'UPDATE '.$this->dbtable.' SET relnum = '.($nextItem->relnum +1).' WHERE id = "'.$nextItem->id.'"'; } if ($str!="") $_tmpResult = $this->query($str, '.goBottom()'); } return true; } function goTop($structID) { $item = $this->getItem($structID,false); if ($item->relnum <= 0) return false; $_childs = $this->getChilds($item->parent,$item->dataclass,false); $maxnum=0; $length = sizeof($_childs); for ($i=0; $i < $length; $i++) { $nextItem = $_childs[$i]; $str=""; if ($nextItem->relnum > $maxnum) $maxnum = $nextItem->relnum; if ($nextItem->id == $item->id) { $str = ""; } else { if ($nextItem->relnum > $item->relnum) $str = 'UPDATE '.$this->dbtable.' SET relnum = '.($nextItem->relnum -1).' WHERE id = "'.$nextItem->id.'"'; } if ($str!="") $_tmpResult = $this->query($str, '.goTop()-1'); } $_tmpResult = $this->query('UPDATE '.$this->dbtable.' SET relnum = '.$maxnum.' WHERE id = "'.$item->id.'"', '.goTop()-2'); return true; } function step($structID,$dir) { //if (is_long($structID)) global $lang; $item = $this->getItem($structID,false); switch ($lang) { case 'eng': $rel = 'relnum'; $irelnum = $item->relnum; break; case 'chi': $rel = 'rel_chi'; $irelnum = $item->rel_chi; break; case 'jap': $rel = 'rel_jap'; $irelnum = $item->rel_jap; break; case 'kor': $rel = 'rel_kor'; $irelnum = $item->rel_kor; break; } $str = "SELECT id,".$rel." FROM ".$this->dbtable." WHERE parent=".$item->parent." AND dataclass='".$item->dataclass."' AND ".$rel; if ($dir==1) $str .= ">".$irelnum." order by ".$rel; else $str .= "<".$irelnum." order by ".$rel." desc"; $_result = $this->query($str, ".step()-1"); if ($prewItem = mysql_fetch_array($_result)) { $str = 'UPDATE '.$this->dbtable.' SET '.$rel.' = '.($prewItem[1] - $dir ).' WHERE id = '.$prewItem[0]; $_result = $this->query($str, ".step()-2"); } else { return $item->parent; } $str = 'UPDATE '.$this->dbtable.' SET '.$rel.' = '.($prewItem[1]).' WHERE id = '.$item->id; $_result = $this->query($str, ".step()-3"); return $item->parent; } function stepUp($structID) { return $this->step($structID, 1); } function stepDown($structID) { return $this->step($structID, -1); } function levelUp($structID) { GLOBAL $currentCategory; $parent = $this->getParent($structID); $this->moveItem($structID,$parent->parent); $currentCategory=$parent->parent; } function moveItem($structID,$toParent) { // get item and parent info $item = $this->getItem($structID,false); $parent = $this->getItem($toParent,false); if (!$parent) return false; // calculate relative number and insert Item to the end of list $str = "SELECT relnum FROM ".$this->dbtable." WHERE parent='".$parent->id."' AND dataclass='".$item->dataclass."' ORDER BY relnum DESC"; $_result = $this->query($str, '.moveItem()-1'); $newNum = mysql_fetch_object($_result); if ($newNum) $str = 'UPDATE '.$this->dbtable.' SET relnum = '.($newNum->relnum +1).' , parent = "'.$parent->id.'" WHERE id = "'.$item->id.'" '; else $str = 'UPDATE '.$this->dbtable.' SET relnum = 1 , parent = "'.$parent->id.'" WHERE id = "'.$item->id.'" '; $_result = $this->query($str, '.moveItem()-2'); // check all relative numbers in old item's branch $this->orderRelnum($item->parent, $item->dataclass); return true; } function orderRelnum($structID,$dataclass) { global $lang; $item = $this->getItem($structID,false); switch ($lang) { case 'eng': $reln = 'relnum'; break; case 'chi': $reln = 'rel_chi'; break; case 'jap': $reln = 'rel_jap'; break; case 'kor': $reln = 'rel_kor';; break; } $childs = $this->getChilds($structID,$dataclass,false); $length = sizeof($childs)-1; $rel = 1; for ($i = $length; $i >= 0; $i--) { $tmpItem = $childs[$i]; switch ($lang) { case 'eng': $irelnum = $tmpItem->relnum; break; case 'chi': $irelnum = $tmpItem->relnum_chi; break; case 'jap': $irelnum = $tmpItem->relnum_jap; break; case 'kor': $irelnum = $tmpItem->relnum_kor; break; } if ($irelnum != ($rel)) { $str = 'UPDATE '.$this->dbtable.' SET '.$reln.' = '.($rel).' WHERE id = "'.$tmpItem->id.'" '; $tmp_result = $this->query($str, '.orderRelnum()'); } $rel++; } return true; } function reverseRelnum($structID,$dataclass) { $childs = $this->getChilds($structID,$dataclass,false); $length = sizeof($childs)-1; for ($i = $length; $i >= 0; $i--) { $tmpItem = $childs[$i]; $str = 'UPDATE '.$this->dbtable.' SET relnum = '.($i+1).' WHERE id = "'.$tmpItem->id.'"'; $tmp_result = $this->query($str, '.reverseRelnum()'); } return true; } function isParent($inp1, $inp2) { // если inp2 - потомок inp1 - return TRUE static $ID1=1; static $ID2=1; $ID1=$inp1; $ID2=$inp2; if($ID2==0 ) { return FALSE; } else { if($ID2==$ID1) { return TRUE; } else { $rest = $this->query("SELECT * FROM ".$this->dbtable." WHERE id='".$ID2."'", '.isParent()'); $row = mysql_fetch_row($rest); $ID2 = $row[1]; return $this->isParent($ID1, $ID2); } } } var $rootObject = '0'; function setRoot($rootID='0') { $this->rootObject = $rootID; } function printArray ($data) { echo "

size of array:"; echo sizeof ($data); echo "
"; for ($eachData = each($data);$eachData;$eachData = each($data)) { echo $eachData['key']; echo " :".$eachData['value']."
"; } } function getItem($itemID, $full = true) { $_item = $this->query('SELECT * FROM '.$this->dbtable.' WHERE id = "'.$itemID.'"', "getItem()-1"); if ($item = mysql_fetch_object($_item)) { if ($item->dataclass =='root' || !$full || $this->getTableExists($item->dataclass)) return $item; $dclass = $item->dataclass; $query = 'SELECT * FROM '.$this->dbtable.' LEFT JOIN '.$dclass.' USING (id) WHERE '.$this->dbtable.'.id="'.$itemID.'"'; $_item = $this->query($query, "getItem()-2"); $data = mysql_fetch_object($_item); return $data; } else { return false; } } function getTableExists($dataclass) { $res = $this->query("SHOW TABLES LIKE '".$dataclass."'"); return !is_object(mysql_fetch_object($res)); } function getObjectStructure($classname, $name = false) { $result = Array(); $r = 0; if($name) { $result[$r]['name'] = 'name'; $result[$r]['type'] = 'VARCHAR'; $result[$r]['length'] = 75; $r++; } if (!$this->getTableExists($classname)) { $_f_result = mysql_list_fields($this->dbname, $classname, $this->dbconnection); $fields = mysql_num_fields($_f_result); for ($i=1;$i< $fields;$i++) { $result[$r]['name'] = mysql_field_name($_f_result,$i); //echo "s_structure[]['name'] = ".$result[$r]['name']."
"; $result[$r]['type'] = mysql_field_type($_f_result,$i); //echo "s_structure[]['type'] = ".$result[$r]['type']."
"; $result[$r]['length'] = mysql_field_len($_f_result,$i); $r++; } } //echo "

"; return $result; } function getChild($itemID, $dataclass = 'any', $rel = 0) { $query = "SELECT * FROM ".$this->dbtable." WHERE parent='".$itemID."'"; if ($dataclass != 'any') $query .= " AND dataclass = '".$dataclass."' "; if ($rel != 0) $query .= " AND relnum = '".$rel."' "; $query .= " ORDER BY relnum DESC"; $_item = $this->query($query, "getChild()-1"); if ($item = mysql_fetch_array($_item)) { $_item = $this->getItem($item['id']); return $_item; } else { return false; } } function getLastAdded($dataclass = 'any', $mod) { $day = 24*3600; if ($mod == 4) $per = $day * 31; else $per = $day * 7 * $mod; $query = "SELECT * FROM ".$this->dbtable." WHERE dateCreated > '".(time() - $per)."'"; if ($dataclass != 'any') $query .= " and dataclass = '".$dataclass."' "; $query .= " ORDER BY cdate desc"; $_items = $this->query($query, "getLastAdded()-1"); while ($item = mysql_fetch_object($_items)) { $result[] = $this->getItem($item->id); } return $result; } function getChilds($parentID, $dataclass = 'any', $full = true, $order = 'relnum DESC') { $query = "SELECT * FROM ".$this->dbtable; $cl = ''; if ($dataclass != 'any') { $cl = 'AND dataclass="'.$dataclass.'"'; if ($full && !$this->getTableExists($dataclass)) $query .= ' LEFT JOIN '.$dataclass.' USING (id)'; } $query .= ' WHERE parent='.$parentID.' '.$cl; $query .= ' AND dis=0'; $query .= ' ORDER BY '.$order; $_items = $this->query($query, "getChilds()-11"); while ($item = mysql_fetch_object($_items)) { if ($full && $dataclass == 'any') $result[] = $this->getItem($item->id); else $result[] = $item; } return $result; } function getAll($dataclass = 'any', $full = true, $order = 'relnum DESC') { $query = "SELECT * FROM ".$this->dbtable; $cl = ''; if ($dataclass != 'any') { $cl = 'AND dataclass="'.$dataclass.'"'; if ($full) $query .= ' LEFT JOIN '.$dataclass.' USING (id)'; } $query .= ' WHERE dis=0 '.$cl; $query .= ' ORDER BY '.$order; $_items = $this->query($query, "getAll()-1"); while ($item = mysql_fetch_object($_items)) { if ($full && $dataclass == 'any') $result[] = $this->getItem($item->id); else $result[] = $item; } return $result; } function getParent($itemID, $full = true) { if ($this->rootObject == $itemID) return false; if (!($item = $this->getItem($itemID, $full))) return false; if ($item->parent==$this->rootObject) return false; return $this->getItem($item->parent, $full); } function putLevelUpButton($structID,$text='one Level Up') { echo " ".$text." "; } function putMoveIntoButton($structID,$cat,$text='move into') { echo " ".$text." "; } function putGoTopButton($structID,$cat,$text='go Top') { echo " ".$text." "; } function putGoBottomButton($structID,$cat,$text='go Bottom') { echo " ".$text." "; } function putStepUpButton($structID,$cat,$text='stepUp') { echo " ".$text." "; } function putStepDownButton($structID,$cat,$text='stepDown') { echo " ".$text." "; } function putEditButton($structID,$cat,$text='edit') { echo " ".$text." "; } function putDropButton($structID,$cat,$text='drop') { echo " ".$text." "; } function putAllButtons($structID,$currentCategory) { echo ""; echo "  "; $this->putEditButton($structID,$currentCategory,'edit'); echo "   "; $this->putStepUpButton($structID,$currentCategory); echo "       "; $this->putGoTopButton($structID,$currentCategory); echo "   "; $this->putLevelUpButton($structID); echo ""; echo "
"; echo ""; echo "  "; $this->putDropButton($structID,$currentCategory,'drop'); echo "  "; $this->putStepDownButton($structID,$currentCategory); echo "  "; $this->putGoBottomButton($structID,$currentCategory); echo "   "; $this->putMoveIntoButton($structID,$currentCategory); echo ""; } function putAddChildButton($structID,$text='add child:') { $js = ''; echo $js; $addChildMenu .= ''; echo "
".$addChildMenu."".$text.""; echo "
"; } function showObjectForm ($data,$mode) { $structure = $this->getObjectStructure($data->dataclass,true); echo "

\n"; echo ""; echo ""; $length = sizeof($structure); for ($i=0;$i<$length;$i++) { echo ""; } echo "

"; if ($mode=='add') echo "Add new "; else echo "Edit "; echo ucfirst($data->dataclass); echo "

"; echo ucfirst($structure[$i]['name']).""; switch ($structure[$i]['type']) { case 'string': case 'CHAR': case 'VARCHAR': echo ''; break; case 'blob': case 'text': case 'TINYBLOB': case 'TINYTEXT': case 'TEXT': case 'BLOB': case 'MEDIUMBLOB': case 'MEDIUMTEXT': case 'LONGBLOB': case 'LONGTEXT': echo ""; break; default : echo ""; break; } echo "
"; if ($mode=="add") { echo ''; echo ''; } else { echo ''; } echo ''; echo ''; echo ''; echo ''; echo "
"; echo "
\n"; } function showAddObjectForm($parent,$classname) { //$classname.="()"; $addFile = new blank(); $addFile->parent = $parent; $addFile->dataclass = $classname; $this->showObjectForm ( $addFile, "add"); } function showEditObjectForm($u_file) { $this->showObjectForm ($u_file,"edit"); } function showObject ($data) { if (!is_object($data)) $data = $this->getItem($data); if (!is_object($data)) return; $structure = $this->getObjectStructure($data->dataclass,true); echo ""; $length = sizeof($structure); for ($i=0;$i<$length;$i++) { echo ""; } echo "
 "; echo ucfirst($structure[$i]['name'])."   "; echo htmlspecialchars($data->$structure[$i]['name']); echo " 
"; } function showObjectSys ($data) { if (!is_object($data)) $data = $this->getItem($data); if (!is_object($data)) return; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
 "; echo "ID   "; echo $data->id." 
 "; echo "Parent   "; echo $data->parent." 
 "; echo "Dataclass   "; echo $data->dataclass." 
 "; echo "Relnum   "; echo $data->relnum." 
 "; echo "Cr date   "; if ($data->cdate > 0) $cdate = strftime('%d.%m.%y %H:%M:%S', $data->cdate); echo $cdate." 
 "; echo "Md date   "; if ($data->mdate > 0) $mdate = strftime('%d.%m.%y %H:%M:%S', $data->mdate); echo $mdate." 
"; } function showPathInit($mode) { ?> getItem($cat); if ($cat == $code) $id = "0"; else $id = $cat; $pname = $item->name2; if ($pname == '') $pname = $item->name; if ($pname == '') $pname = $item->subject; if ($pname == '') $pname = $item->search_class; $in = ereg_replace('"','\"', ereg_replace("'","\'",$pname)); while ($pathItem = $this->getParent($cat)) { $pname = $pathItem->name2; if ($pname == '') $pname = $pathItem->name; if ($pname == '') $pname = $pathItem->subject; if ($pname == '') $pname = $pathItem->search_class; if ($pathItem->id == $code) $id .= "/0"; else $id .= "/".$pathItem->id; $in .= "/".ereg_replace('"','\"', ereg_replace("'","\'",$pname)); $cat = $pathItem->id; } $id .= "/0"; $in .= "/Клуб"; echo "show7('".$id."','".$in."');"."\n"; return true; } function showDropForm($structID,$cat) { global $lang; echo "

Are you sure?

"; echo "

\n"; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo "
"; } function doAction() { GLOBAL $dmaction,$dmclass,$dmparent,$dmstage,$dmid; if (!isset($dmaction)) return false; switch ($dmaction) { case "add": if($dmstage == 0) { if (!isset($dmclass)) return false; $this->showAddObjectForm($dmparent,$dmclass); } else { $dmid = $this->addObject(); return false; } break; case "drop": if($dmstage == 0) { $this->showDropForm($dmid,$dmparent); } else { $dmid = $this->dropObject($dmid); return false; } break; case "reverse": $this->reverseRelnum($dmid,$dmclass); return false; break; case "edit": $item = $this->getItem($dmid); if($dmstage == 0) { $this->showEditObjectForm($item); } else { $this->editObject($item); return false; } break; case "stepUp": $this->stepUp($dmid); return false; break; case "stepDown": $this->stepDown($dmid); return false; break; case "goTop": $this->goTop($dmid); return false; break; case "goBottom": $this->goBottom($dmid); return false; break; case "levelUp": $this->levelUp($dmid); return false; break; case "moveInto": if($dmstage == 1) { if(!$this->isParent($dmid,$dmparent)) { $this->moveItem($dmid,$dmparent); return false; } else { echo "error
"; return true; } break; } default : return false; break; } return true; } } ?>