forum.class.php 15.3 KB
<?php
class Forum{
 var $db = null;
 var $tpl = null;
 var $error = null;
 var $tree = null;

 function Forum(&$db,&$tpl,&$error){
  $this->db = &$db;
  $this->tpl = &$tpl;
  $this->error = &$error;
  $this->tree = &new CDBTree($this->db,"tree","id");
 }
 

 function trim(&$data){
  foreach($data as $key=>$value){
   $data[$key] = strip_tags(trim($value));
  }
 }
 

 function valid($data,$upload = null,$user_id = false){

        
  if(isset($data['register_login'])){
   if( !preg_match("/^.{1,30}$/i",$data['register_login']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ëîãèí, îò 1 äî 30 ñèìâîëîâ.";
   elseif($this->isLogin($data['register_login'],$user_id))$this->error[] = "Îøèáêà ââîäà, òàêîé Ëîãèí óæå åñòü!";
  }
  
  if(isset($data['register_psw'])){
   if( !preg_match("/^.{1,30}$/i",$data['register_psw']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ïàðîëü, îò 1 äî 30 ñèìâîëîâ.";
  }
  
  if(isset($data['register_email'])){
   if( !preg_match("/^([^@]+)+@([^@]+)\.([^@]+)$/i",$data['register_email']) || strlen($data['register_email'])>50 ) $this->error[] = "Îøèáêà ââîäà ïîëÿ E-mail, äî 50 ñèìâîëîâ.";
  }
  
  if(isset($data['register_name'])){
   if( !preg_match("/^.{1,30}$/i",$data['register_name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èìÿ, îò 1 äî 30 ñèìâîëîâ.";
  }
  
  if(isset($data['title'])){
   if( !preg_match("/^.{1,250}$/i",$data['title']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Íàçâàíèå òåìû, îò 1 äî 250 ñèìâîëîâ.";
  }

  
  if(isset($data['about'])){
   if( !preg_match("/.+/is",$data['about']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Îïèñàíèå.";
  }
  
  if(isset($upload['pic']['name']) && $upload['pic']['name']!=null){
   $type = substr(strrchr($upload['pic']['name'],"."),1);
   if( !preg_match("/^(jpeg|jpg|gif|png)$/i",$type) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èçîáðàæåíèÿ, òîëüêî jpg,png,gif.";
  }

  if(isset($data['login'])){
   if( !preg_match("/.{1,50}/i",$data['login']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èìÿ.";
     elseif($this->isLogin($data['login'],$user_id))$this->error[] = "Îøèáêà ââîäà, òàêîé Ëîãèí óæå åñòü!";
  }


  if(isset($data['about_comment'])){
   if( !preg_match("/.+/is",$data['about_comment']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Êîììåíòàðèè.";
  }

  if(isset($data['treeID'])){
   if( !preg_match("/^([0-9]+)$/",$data['treeID']) && $data['treeID']>0 ) $this->error[] = "Îøèáêà íîìåðà âåòêè!!!.";
  }

  if(isset($data['topicID'])){
   if( !preg_match("/^([0-9]+)$/",$data['topicID']) && $data['topicID']>0 ) $this->error[] = "Îøèáêà íîìåðà òåìû ôîðóìà!!!.";
  }

  
 return ( count($this->error) ) ? true : false;
 }
 
 function isLogin($login,$user_id){
  $sql = "select count(*) from forum_user where login=?";
  if($user_id>0)$sql .= "and id<>'$user_id'";
  return $this->db->getOne($sql,array($login));
 }
 
 function isUser($login,$psw){
  $sql = "select id from forum_user where login=? and psw=?";
  $user_id = $this->db->getOne($sql,array($login,$psw));
  return $user_id;
 }
 
 function viewUser($id){
  $sql = "select * from forum_user where id=?";
  $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
  $this->tpl->assign("user",$row);
 }
 
 function saveUser($data,$upload,$user_id = false){
 $table_name = "forum_user";
 $DB_AUTOQUERY = ($user_id>0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
 $where = ($user_id>0) ? "id='$user_id'" : '';
 $fields_values = array('login' => $data['register_login'],'psw' => $data['register_psw'],'email' => $data['register_email'],'name' => $data['register_name'],'about' => $data['register_about']);
  if($upload['pic']['tmp_name']){
   if($user_id>0){$this->deleteUserPic($user_id);}
   $fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"90",'height'=>"90",'upload_path'=>"./uploaded/pic/forum/user/"));
  }


  $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,$where);
 }
 
 function deleteUserPic($id){
  $sql = "select pic from forum_user where id=?";
  $pic = $this->db->getOne($sql,array($id));
  @unlink("./uploaded/pic/forum/user/$pic");
 }
 
 function delete($id){
  $sql = "select tree_id,pic from topic where id=?";
  $topic_row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
  if($topic_row['pic']){@unlink("./pic/forum/{$topic_row['pic']}");}
  $this->tree->deleteAll($topic_row['tree_id']);
  
  $sql = "delete from topic where id=?";
  $this->db->query($sql,array($id));
 }
 
 function deleteParentElemant($parent_table,$parent_id){
  $sql = "select id,tree_id,pic from topic where parent_table=? and parent_table_id=?";
  $topic_row = $this->db->getRow($sql,array($parent_table,$parent_id),DB_FETCHMODE_ASSOC);
  if($topic_row['pic']){@unlink("./pic/forum/{$topic_row['pic']}");}
  $this->tree->deleteAll($topic_row['tree_id']);

  $sql = "delete from topic where id=?";
  $this->db->query($sql,array($topic_row['id']));
 }
 
 function deleteComment($id){
   $sql1 = "select topic_id,pic from tree where id=? limit 1";
   $row = $this->db->getRow($sql1,array($id),DB_FETCHMODE_ASSOC);
   $topic_id = $row['topic_id'];
      if($row['pic']){@unlink("./uploaded/pic/forum/{$row['pic']}");}
      $this->tree->delete($id);
   $sql = "select id,mktime from tree where topic_id=? And _level>1 order by mktime desc limit 1";
   $last = $this->db->getRow($sql,array($topic_id),DB_FETCHMODE_ASSOC);
   $sql2 = "update topic set comments=comments-1,comment_last_mktime=?,comment_last=? where id=?";
   $this->db->query($sql2,array($last['mktime'],$last['id'],$topic_id));
 }
 
 function updateTopic($data,$upload = null){
  $table_name = "topic";
  $fields_values = array("title"=>$data['data']['title'],"about"=>$data['data']['about'],"meta_title"=>$data['data']['meta_title'],"meta_description"=>$data['data']['meta_description'],"meta_keywords"=>$data['data']['meta_keywords'],"meta_about"=>$data['data']['meta_about']);
  $this->db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_UPDATE,"id='".$data['update_id']."'");
 }
 
 function SaveTopic($data,$upload = null,$user_id = 0){
  $table_name = "topic";
  $topic_next_id = $this->db->nextId('mySequenceTopic');
  
  //Ñîçäàåì íà÷àëüíóþ âåòêó...
  $tree_parent_id = 1;
  $tree_next_id = $this->db->nextId('mySequenceTree');
  $arrayData = array("id"=>$tree_next_id,"parent_id"=>$tree_parent_id,"topic_id"=>$topic_next_id,"user_id"=>$user_id,"mktime"=>mktime());
  //print_r($arrayData);
  $this->tree->insert($tree_parent_id,$arrayData);
  //Êîíåö...

  //Ñîçäàåì òåìó ôîðóìà...
  switch($data['parent_table']){
   case "rooms" :
    $fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
                           "parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
                           "title"=>$data['data']['company'],"about"=>$data['data']['about'],
                           "mktime"=>mktime());
    if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"120",'height'=>"120",'upload_path'=>"./uploaded/pic/forum/topic/"));}
   break;
   case "product" :
    $fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
                           "parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
                           "title"=>$data['data']['name'],"about"=>$data['data']['about'],
                           "mktime"=>mktime());
    if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"120",'height'=>"120",'upload_path'=>"./uploaded/pic/forum/topic/"));}
   break;
   case "articles" :
    $fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
                           "parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
                           "title"=>$data['data']['title'],"about"=>$data['data']['text'],
                           "mktime"=>mktime());
    if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"200",'height'=>"200",'upload_path'=>"./uploaded/pic/forum/topic/"));}
   break;
   case "news" :
    $fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
                           "parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
                           "title"=>$data['data']['title'],"about"=>$data['data']['text'],
                           "mktime"=>mktime());
    if($upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"200",'height'=>"200",'upload_path'=>"./uploaded/pic/forum/topic/"));}
   break;
   case "questioning" :
    $fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
                           "parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
                           "title"=>$data['data']['name'],
                           "mktime"=>mktime());
   break;
   default :   //TOPIC
    $fields_values = array("id"=>$topic_next_id,"tree_id"=>$tree_next_id,"user_id"=>$user_id,
                           "parent_table"=>$data['parent_table'],"parent_table_id"=>$data['parent_table_id'],
                           "title"=>$data['data']['title'],"about"=>$data['data']['about'],
                           "mktime"=>mktime(),"comment_last_mktime"=>mktime());
//    if($upload = null && $upload['pic']['tmp_name']){$fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"120",'height'=>"120",'upload_path'=>"./pic/forum/"));}
   break;
  }

        //print_r($fields_values);exit;
  $this->db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_INSERT);
 }
 
 function reload(){
        print'
                <script language="JavaScript">
                <!--
                function closeIt() {
                window.opener.document.location.reload();
                window.close();
                }
                closeIt();
                // -->
                </script>
             ';
 }
 

 function displaySaveComment(){
  $this->tpl->display("save_comment.tpl");
 }
 
 function displayUpdateComment(){
  $this->tpl->display("update_comment.tpl");
 }
 
 function beginOneTreeID($id){
  $sql = "select a.id from tree a, tree b where b.id=? and (a._left<b._left and a._right>b._right) and a._level=1 limit 1";
  return $this->db->getOne($sql,$id);
 }

 function saveComment($data,$user_id = 0,$upload){
  $tree_next_id = $this->db->nextId('mySequenceTree');
  $arrayData = array("id"=>$tree_next_id,"user_id"=>$user_id,"parent_id"=>$data['treeID'],"topic_id"=>$data['topicID'],"about"=>$data['about_comment'],"login"=>$data['login'],"mktime"=>mktime());

  if(isset($upload['pic']['tmp_name']) && $upload['pic']['tmp_name']){
   $arrayData['pic'] = upload_ImageResize($upload['pic'],array('width'=>"300",'height'=>"300",'upload_path'=>"./uploaded/pic/forum/"));
  }
  $this->tree->insert($data['treeID'],$arrayData);
  
  $beginID = $this->beginOneTreeID($tree_next_id);
  $sql = "update topic set comments=comments+1,comment_last=?,comment_last_mktime=? where tree_id=?";
  $this->db->query($sql, array($tree_next_id,mktime(),$beginID));
 }
 
 function updateComment($data,$upload){
  $table_name = "tree";
  $fields_values = array("about"=>$data['about_comment']);
  if(isset($upload['pic']['tmp_name']) && $upload['pic']['tmp_name']){
   $fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"300",'height'=>"300",'upload_path'=>"./uploaded/pic/forum/"));
  }
  $this->db->autoExecute($table_name,$fields_values,DB_AUTOQUERY_UPDATE,"id={$data['update_id']}");
 }
 
 function viewComments($id){
  $sql = "select a.*,forum_user.pic as pic1 from tree b, tree a LEFT JOIN forum_user ON forum_user.id=a.user_id where b.id=? and (a._left>b._left and a._right<b._right) and a._level=2 order by a.mktime desc";
  $pagerOptions = Array(
    'mode' => 'Sliding',
    'delta' => 6,
    'perPage' => 30,
  );
  $res = Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array($id));
  foreach($res['data'] as $key=>$row){
   $sql = "select * from tree where parent_id=? order by mktime desc";
   $res['data'][$key]['child'] = $this->db->getAll($sql,array($row['id']),DB_FETCHMODE_ASSOC);
  }
  //print_r($res);
  $this->tpl->assign('commentsData', $res);
 }
 
 function viewCommentOne($id){
  $sql = "select * from tree where id=?";
  $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
  $this->tpl->assign('comment',$row);
 }
 
 function viewTopicsCommentsLast(){
  $sql = "select topic.*,tree.about as comment_str from topic left join tree on tree.id=topic.comment_last Where 1=1 order by topic.comment_last_mktime desc, topic.mktime desc limit 7";
  $row = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
  $this->tpl->assign('topics',$row);
 }
 
  function last_comment(){
   $sql = "select tree.*,forum_user.login from tree LEFT JOIN forum_user ON forum_user.id=tree.user_id where tree._level>1 order by tree.mktime desc limit 3";
   $row = $this -> db -> getAll($sql,array(),DB_FETCHMODE_ASSOC);
   $this -> tpl -> assign("last_comments",$row);
  }
 
 function viewTopics($param){
  $search = array();
  if(isset($param['firmID']) && $param['firmID']>0)$search[] = "topic.firm_id={$param['firmID']}";
  $sql = "select topic.* from topic where 1=1 ";
  if(count($search))$sql .= " and " . implode(" and ",$search) . " ";
  $sql .= "order by topic.comment_last_mktime desc";

  $pagerOptions = Array(
    'mode' => 'Sliding',
    'delta' => 6,
    'perPage' => 20,
    'linkClass'=>"link_class",
    'curPageLinkClassName'=>"link_class_active",
    'spacesBeforeSeparator' => 1,
    'spacesAfterSeparator' => 1
  );
  $this->tpl->assign('topicData', Pager_Wrapper_DB($this->db, $sql, $pagerOptions));
 }
 
 function viewFirmTopics($firm_id){
  $sql = "select * from topic where firm_id=? order by mktime desc";
  $pagerOptions = Array(
    'mode' => 'Sliding',
    'delta' => 6,
    'perPage' => 10,
    'spacesBeforeSeparator' => 1,
    'spacesAfterSeparator' => 1
  );
  $this->tpl->assign('topicFirmData', Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array($firm_id)));
 }
 
 function viewTopic($id,&$row){
  $sql = "select * from topic where id=? limit 1";
  $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
  $this->tpl->assign('topic', $row);
 return $row['tree_id'];
 }
 
 function viewParentTopic($parent_table,$parent_id){
  $sql = "select * from topic where parent_table=? and parent_table_id=? limit 1";
  $row = $this->db->getRow($sql,array($parent_table,$parent_id),DB_FETCHMODE_ASSOC);
  $this->tpl->assign('topic_parent', $row);
 }
 
 function Click($id){
  $sql = "update topic set view=view+1 where id=? limit 1";
  $this->db->query($sql,$id);
 }
 
 function yourComments($firm_id,$parent_table = 'gallery'){
  $mktime = mktime(0,0,0,date("m"),date("d"),date("Y"));
  $end_mktime = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
  $sql = "select count(a.id) from tree a,tree b,topic where topic.parent_table=? AND topic.firm_id=? AND b.id=topic.tree_id AND (a._left>b._left and a._right<b._right) and (a.mktime>=? and a.mktime<=?)";
  $count = $this->db->getOne($sql,array($parent_table,$firm_id,$mktime,$end_mktime));

  $this->tpl->assign('countCommensDays_'.$parent_table,$count);
 }
 
 function displayForum(){
  $this->tpl->assign("tpl","forum.tpl");
 }
 
 function displayTopic(){
  $this->tpl->assign("tpl","topic.tpl");
 }
 

 function displayRegister(){
  $this->tpl->assign("tpl","forum_register.tpl");
 }
 
}
?>