DB = $DB; $this->TTable = $ST['dbpf'].'_fshp_fakkaf'; $this->polya = $this->table_create(); } //id kafedra fakultet kodkaf kodkaf_old /** * Создание таблицы и возвращение полей таблицы * @return string[][] */ private function table_create(){ $polya = array( 0=>array('name'=>'id','type'=>'int(11)','nul'=>'NOT NULL','dop'=>'AUTO_INCREMENT'), array('name'=>'kafedra','type'=>'varchar(255)','nul'=>'NOT NULL','dop'=>''), array('name'=>'fakultet','type'=>'varchar(255)','nul'=>'NOT NULL','dop'=>''), array('name'=>'kodkaf','type'=>'varchar(255)','nul'=>'NOT NULL','dop'=>''), array('name'=>'kodkaf_old','type'=>'varchar(255)','nul'=>'NOT NULL','dop'=>''), ); $sql = 'CREATE TABLE IF NOT EXISTS `'.$this->TTable.'` ('; foreach($polya as $k => $v){ $sql .= '`'.$v['name'].'` '.$v['type'].' '.$v['nul'].' '.$v['dop'].',';} $sql .= 'primary key (id) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;'; $this->DB->QUR($sql); return $polya; } /** * Основа работы с интерфейсом * @return string[]|void * @throws SmartyException */ public function HTML(){ GLOBAL $WorkDIR; $CHPU = new class_CHPU($WorkDIR); $URLS = $CHPU->uri(); $smartyC = new class_smarty(); $smarty = $smartyC->config(__DIR__.'\..\tpl'); $view = 'all'; $out = array('name'=>'Факультеты и кафедры','body'=>''); //обработка АЯКС запросов if(isset($_POST['ajdes'])){ header('Content-Type: application/json; charset=utf-8'); $out=array(); //Установка кафедры для дисциплины if($_POST['ajdes']=='setkaf_update'){ $id = (int)$_POST['id']; $kafedra_new = $this->DB->rescape($_POST['kafedra_new']); $sql = 'UPDATE acs_fshp_uchebplans SET kaf_name="'.$kafedra_new.'" WHERE id='.$id.' LIMIT 1'; $rez = $this->DB->QUR($sql); if(!$rez['err']){ $out['err']=0; $out['msg']='Обновили'; }else{ $out['err']=1; $out['msg']='Не обновили'; } } //Смена кафедр в УП на правильные if($_POST['ajdes']=='compare_update'){ $kafedra_old = $this->DB->rescape($_POST['kafedra_old']); $kafedra_new = $this->DB->rescape($_POST['kafedra_new']); $sql = 'UPDATE acs_fshp_uchebplans SET kaf_name="'.$kafedra_new.'" WHERE kaf_name="'.$kafedra_old.'"'; $rez = $this->DB->QUR($sql); if(!$rez['err']){ $out['err']=0; $out['msg']='Обновили'; }else{ $out['err']=1; $out['msg']='Не обновили'; } } //удаление записи комп.модели if($_POST['ajdes']=='Dele_TR'){ $data['id'] = (int)$_POST['idr']; $out = $this->Delete($data); } //сохранение записи комп.модели if($_POST['ajdes']=='Save_TR'){ $data['kafedra'] = $this->DB->rescape($_POST['kafedra']); $data['fakultet'] = $this->DB->rescape($_POST['fakultet']); $data['kodkaf'] = $this->DB->rescape($_POST['kodkaf']); $data['kodkaf_old'] = $this->DB->rescape($_POST['kodkaf_old']); $data['id'] = (int)$_POST['idr']; if($data['id']==0) $out = $this->Save($data); else $out = $this->Edit($data); }echo json_encode($out); exit(); } if(isset($URLS[1])){ $view = $URLS[1]; if($URLS[1]=='compare'){ $fakkaf=array(); $sql = 'SELECT * FROM `'.$this->TTable.'` ORDER BY id';//fakultet ASC'; $rez = $this->DB->QUR_SEL($sql); //echo '
'.print_r($rez,1).'
';exit(); if(!$rez['err']&&$rez['kol']){ foreach($rez['rez'] as $key => $val){ $fakkaf[$val['kodkaf']]['kod'] = $val['kodkaf']; $fakkaf[$val['kodkaf']]['name'] = $val['kafedra']; } } $items = array(); $sql = 'SELECT `kaf_kod`,`kaf_name` FROM `acs_fshp_uchebplans` GROUP BY `kaf_name` ORDER BY `acs_fshp_uchebplans`.`kaf_kod` ASC'; $rez = $this->DB->QUR_SEL($sql); if(!$rez['err']&&$rez['kol']){ foreach($rez['rez'] as $key => $val){ $val['rr'] = $this->str_compareColor($fakkaf[$val['kaf_kod']]['name'],$val['kaf_name']); $items[] = $val; } } $smarty->assign('items', $items); $smarty->assign('fakkaf', $fakkaf); } if($URLS[1]=='kafnul'){ $items=array(); $sql = 'SELECT kodkaf,kafedra FROM `'.$this->TTable.'` ORDER BY id'; $rez = $this->DB->QUR_SEL($sql); if(!$rez['err']&&$rez['kol']){ foreach($rez['rez'] as $key => $val){ $sql1 = 'SELECT id,index1,name,`kaf_kod`,`kaf_name` FROM `acs_fshp_uchebplans` WHERE kaf_kod="'.$val['kodkaf'].'" AND kaf_name!="'.$val['kafedra'].'"'; $rez1 = $this->DB->QUR_SEL($sql1); if(!$rez1['err']&&$rez1['kol']){ foreach($rez1['rez'] as $key1 => $val1){ $val1['rr'] = $this->str_compareColor($val['kafedra'],$val1['kaf_name']); $val1['kafkod_1'] = $val['kodkaf']; $val1['kafname_1'] = $val['kafedra']; $items[] = $val1; } } } } $smarty->assign('items', $items); } $smarty->assign('view', $view); $out['body'] = $smarty->fetch('page_fakkaf.html'); } //Отображение всех if(!isset($URLS[1])||$URLS[1]=='all') { $smarty->assign('items', $this->Items()); $smarty->assign('view', $view); $out['body'] = $smarty->fetch('page_fakkaf.html'); } return $out; } function str_compareColor($str1,$str2){ $out=''; $array1 = mb_str_split($str1); $array2 = mb_str_split($str2); foreach($array1 as $k1 => $v1){ if($array2[$k1]!=$v1){ $out .= ''.$array2[$k1].''; }else{ $out .= $array2[$k1]; } } return $out; } /** * Подготовка данных для таблицы * @param $data * @return mixed */ private function Prepare_data($data){ if(!isset($data['kafedra'])) $data['kafedra']=''; else $data['kafedra']=$this->DB->rescape($data['kafedra']); if(!isset($data['fakultet'])) $data['fakultet']=''; else $data['fakultet']=$this->DB->rescape($data['fakultet']); if(!isset($data['kodkaf'])) $data['kodkaf']=''; else $data['kodkaf']=$this->DB->rescape($data['kodkaf']); if(!isset($data['kodkaf_old'])) $data['kodkaf_old']=''; else $data['kodkaf_old']=$this->DB->rescape($data['kodkaf_old']); if($data['pos']=='') $data['pos']=0; return $data; } /** * Редактирование записи * @param $data * @return array */ private function Edit($data){ $data = $this->Prepare_data($data); $sql = 'UPDATE `'.$this->TTable.'` SET kafedra="'.$data['kafedra'].'",fakultet="'.$data['fakultet'].'",kodkaf="'.$data['kodkaf'].'",kodkaf_old="'.$data['kodkaf_old'].'" WHERE id='.$data['id'].' LIMIT 1'; $rez = $this->DB->QUR($sql); if(!$rez['err']){ $out['err']=0; $out['msg']='Обновили запись'; $out['id']=$this->DB->lastinsertID(); }else{ $out['err']=1; $out['msg']='Не обновили запись. Ошибка!'; $out['id']=$data['id']; } return $out; } /** * Сохранение записи * @param $data * @return array */ private function Save($data){ $data = $this->Prepare_data($data); $sql = 'INSERT INTO `'.$this->TTable.'` VALUES(0,"'.$data['kafedra'].'","'.$data['fakultet'].'","'.$data['kodkaf'].'","'.$data['kodkaf_old'].'");'; $rez = $this->DB->QUR($sql); if(!$rez['err']){ $out['err']=0; $out['msg']='Добавили запись'; $out['id']=$this->DB->lastinsertID(); }else{ $out['err']=1; $out['msg']='Не добавили запись. Ошибка!'; $out['id']=0; } return $out; } /** * Удаление записи * @param $data * @return array */ private function Delete($data){ $sql = 'DELETE FROM `'.$this->TTable.'` WHERE id='.$data['id'].' LIMIT 1'; $rez = $this->DB->QUR($sql); if(!$rez['err']){ $out['err']=0; $out['msg']='Удалили запись'; $out['id']=$data['id']; }else{ $out['err']=1; $out['msg']='Не удалили запись. Ошибка!'; $out['id']=0; } return $out; } /** * Получение * @return array|mixed */ public function Items(){ $out=array(); $sql = 'SELECT * FROM `'.$this->TTable.'` ORDER BY kodkaf';//fakultet ASC'; $rez = $this->DB->QUR_SEL($sql); if(!$rez['err']&&$rez['kol']){ $out = $rez['rez']; } return $out; } /** * Получение по названию кафедры * @return array|mixed */ public function Item_by_kafname($kafname){ $out=array(); //Истории и права if($kafname=='истории государства и права') $kafname = 'Истории и права'; if($kafname=='Истории государства и права') $kafname = 'Истории и права'; $sql = 'SELECT * FROM `'.$this->TTable.'` WHERE kafedra="'.$this->DB->rescape($kafname).'"'; //echo $sql; //SELECT * FROM `acs_fshp_fakkaf` WHERE kafedra="истории государства и права" $rez = $this->DB->QUR_SEL($sql); if(!$rez['err']&&$rez['kol']){ $out = $rez['rez'][0]; if (strpos($out['fakultet'], 'Факультет') !== false) { $out['fakultet'] = str_replace('Факультет','',$out['fakultet']); } } return $out; } /** * Получение по коду * @return array|mixed */ public function Item_by_kod($kodkaf){ $out=array(); $sql = 'SELECT * FROM `'.$this->TTable.'` WHERE kodkaf="'.$this->DB->rescape($kodkaf).'"'; $rez = $this->DB->QUR_SEL($sql); if(!$rez['err']&&$rez['kol']){ $out = $rez['rez'][0]; } return $out; } /** * Получение по старому коду * @return array|mixed */ public function Item_by_oldkod($kodkaf){ $out=array(); $sql = 'SELECT * FROM `'.$this->TTable.'` WHERE kodkaf_old="'.$this->DB->rescape($kodkaf).'"'; $rez = $this->DB->QUR_SEL($sql); if(!$rez['err']&&$rez['kol']){ $out = $rez['rez'][0]; } return $out; } }