<?php

class LayoutHandler extends Handler{

	function __construct(){
		parent::__construct('LayoutHandler');
	}

	function openLayout($request = array('id' => 1)) {
		global $TOOLS_PATH;
		if(!empty($request['undo'])) {
			$Undo_field= array('type' => 1,'id' => $request['id'],'undo' => 0,'version' => $request['undo']);
			include($TOOLS_PATH.'\undo.php');
			return $print_arr;
		}
		$layout = new Layout();
		$layout->id = $request['id'];
		$this->DB->initObject($layout);
		return $layout;
	}

	function openCR($request = array('id' => 1)) {
		global $TOOLS_PATH;
		if(!empty($request['undo'])) {
			$Undo_field= array('type' => 19,'id' => $request['id'],'undo' => 0,'version' => $request['undo']);
			include($TOOLS_PATH.'\undo.php');
			return $print_arr;
		}
		$cr = new CustomerReview();
		$cr->id = $request['id'];
		$this->DB->initObject($cr);
		return $cr;
	}

	function selectCustomerReviews($request = array()){
		$result = array();
//		$result = $this->DB->SelectAny('SELECT id, name, date_changed, (\'now\'::date - date_changed::date) AS unmodified, access_flag, date_locked FROM customerreview ORDER BY id');
		$result = $this->DB->SelectAny('SELECT id, name, date_changed, TIMESTAMPDIFF(day,date_changed,NOW()) AS unmodified, access_flag, date_locked FROM customerreview ORDER BY id');
		$presets = array();
		$selected = $this->DB->SelectAny('SELECT id, name FROM preset');
		$presets_count = count($selected);
		for ($i = 0; $i < $presets_count; $i++) $presets[$selected[$i]['id']] = $selected[$i]['name'];
		$packages = array();
		$hostpaths = array();
		$usernames = array();
		$count = count($result);
		for($i = 0; $i < $count; $i++){
			$date_changed = explode('.', $result[$i]['date_changed']);
			$result[$i]['date_changed'] = $date_changed[0];
			$date_locked = explode('.', $result[$i]['date_locked']);
			$result[$i]['date_locked'] = $date_locked[0];
			$result[$i]['username'] = $result[$i]['access_flag'] ? $this->getUserName(array('type' => 1, 'userid' => $result[$i]['access_flag'])) : '';
			$result[$i]['publish'] = array();
			$query = 'SELECT packagepublish.id, packagepublish.ownerid as package_id, packagepublish.host, packagepublish.userid, packagepublish.date_published FROM packagepublish WHERE packagepublish.type = 1 AND packagepublish.successful = 1 AND packagepublish.date_published > \''.$result[$i]['date_changed'].'\' AND packagepublish.ownerid IN (SELECT DISTINCT packageitem.ownerid FROM packageitem WHERE packageitem.type = 19 AND packageitem.object = \''.$result[$i]['id'].'\') ORDER BY packagepublish.date_published DESC';
			$packagepublish = $this->DB->SelectAny($query);
			$presets_unique = array();
			$c = count($packagepublish);
			$index = 0;
			for ($j = 0; $j < $c; $j++){
				if(isset($presets_unique[$packagepublish[$j]['host']])) continue;
				$presets_unique[$packagepublish[$j]['host']] = true;
				$result[$i]['publish'][$index] = $packagepublish[$j];
				$date_published = explode ('.', $packagepublish[$j]['date_published']);
				$result[$i]['publish'][$index]['date_published'] = $date_published[0];
				if(!isset($packages[$packagepublish[$j]['package_id']])){
					$sel = $this->DB->SelectAny('SELECT name FROM package WHERE id = '.$packagepublish[$j]['package_id'].' LIMIT 1');
					$packages[$packagepublish[$j]['package_id']] = empty($sel) ? '[deleted]' : $sel[0]['name'];
				}
				$result[$i]['publish'][$index]['package_name'] = $packages[$packagepublish[$j]['package_id']];
				if(!isset($hostpaths[$packagepublish[$j]['host']])){
					$sel = $this->DB->SelectAny('SELECT name FROM preset WHERE id = '.$packagepublish[$j]['host'].' LIMIT 1');
					$hostpaths[$packagepublish[$j]['host']] = empty($sel) ? '[unknown]' : $sel[0]['name'];
				}
				$result[$i]['publish'][$index]['hostpath'] = $hostpaths[$packagepublish[$j]['host']];
				if(!isset($usernames[$packagepublish[$j]['userid']])){
					$sel = $this->DB->SelectAny('SELECT username FROM member WHERE id = '.$packagepublish[$j]['userid'].' LIMIT 1');
					$usernames[$packagepublish[$j]['userid']] = empty($sel) ? '[unknown]' : $sel[0]['username'];
				}
				$result[$i]['publish'][$index]['username'] = $usernames[$packagepublish[$j]['userid']];
				$index++;
			}
			$result[$i]['published_count'] = count($result[$i]['publish']).'/'.$presets_count;
		}
		return $result;
	}

	
	function selectLayouts($request = array()){
		$result = array();
//		$result = $this->DB->SelectAny('SELECT id, name, date_changed, (\'now\'::date - date_changed::date) AS unmodified, access_flag, date_locked FROM layout ORDER BY id');
		$result = $this->DB->SelectAny('SELECT id, name, date_changed, TIMESTAMPDIFF(day,date_changed,NOW()) AS unmodified, access_flag, date_locked FROM layout ORDER BY id');
		$presets = array();
		$selected = $this->DB->SelectAny('SELECT id, name FROM preset');
		$presets_count = count($selected);
		for ($i = 0; $i < $presets_count; $i++) $presets[$selected[$i]['id']] = $selected[$i]['name'];
		$packages = array();
		$hostpaths = array();
		$usernames = array();
		$count = count($result);
		for($i = 0; $i < $count; $i++){
			$date_changed = explode('.', $result[$i]['date_changed']);
			$result[$i]['date_changed'] = $date_changed[0];
			$date_locked = explode('.', $result[$i]['date_locked']);
			$result[$i]['date_locked'] = $date_locked[0];
			$result[$i]['username'] = $result[$i]['access_flag'] ? $this->getUserName(array('type' => 1, 'userid' => $result[$i]['access_flag'])) : '';
			$result[$i]['publish'] = array();
			$query = 'SELECT packagepublish.id, packagepublish.ownerid as package_id, packagepublish.host, packagepublish.userid, packagepublish.date_published FROM packagepublish WHERE packagepublish.type = 1 AND packagepublish.successful = 1 AND packagepublish.date_published > \''.$result[$i]['date_changed'].'\' AND packagepublish.ownerid IN (SELECT DISTINCT packageitem.ownerid FROM packageitem WHERE packageitem.type = 1 AND packageitem.object = \''.$result[$i]['id'].'\') ORDER BY packagepublish.date_published DESC';
			$packagepublish = $this->DB->SelectAny($query);
			$presets_unique = array();
			$c = count($packagepublish);
			$index = 0;
			for ($j = 0; $j < $c; $j++){
				if(isset($presets_unique[$packagepublish[$j]['host']])) continue;
				$presets_unique[$packagepublish[$j]['host']] = true;
				$result[$i]['publish'][$index] = $packagepublish[$j];
				$date_published = explode ('.', $packagepublish[$j]['date_published']);
				$result[$i]['publish'][$index]['date_published'] = $date_published[0];
				if(!isset($packages[$packagepublish[$j]['package_id']])){
					$sel = $this->DB->SelectAny('SELECT name FROM package WHERE id = '.$packagepublish[$j]['package_id'].' LIMIT 1');
					$packages[$packagepublish[$j]['package_id']] = empty($sel) ? '[deleted]' : $sel[0]['name'];
				}
				$result[$i]['publish'][$index]['package_name'] = $packages[$packagepublish[$j]['package_id']];
				if(!isset($hostpaths[$packagepublish[$j]['host']])){
					$sel = $this->DB->SelectAny('SELECT name FROM preset WHERE id = '.$packagepublish[$j]['host'].' LIMIT 1');
					$hostpaths[$packagepublish[$j]['host']] = empty($sel) ? '[unknown]' : $sel[0]['name'];
				}
				$result[$i]['publish'][$index]['hostpath'] = $hostpaths[$packagepublish[$j]['host']];
				if(!isset($usernames[$packagepublish[$j]['userid']])){
					$sel = $this->DB->SelectAny('SELECT username FROM member WHERE id = '.$packagepublish[$j]['userid'].' LIMIT 1');
					$usernames[$packagepublish[$j]['userid']] = empty($sel) ? '[unknown]' : $sel[0]['username'];
				}
				$result[$i]['publish'][$index]['username'] = $usernames[$packagepublish[$j]['userid']];
				$index++;
			}
			$result[$i]['published_count'] = count($result[$i]['publish']).'/'.$presets_count;
		}
		return $result;
	}

	function getLock($request = array()){
		empty($request['layout']) and $request['layout'] = 0;
		empty($request['member']) and $request['member'] = 0;
		if(!empty($request['member']) && !empty($request['layout'])) {
			$query='select l.id, l.access_flag, l.date_locked, u.username from layout l, member u where l.id='.$request['layout'].' and l.access_flag=u.id';
			$selected = $this->DB->SelectAny($query);
		}		
		return !empty($selected[0])? $selected[0] : array();
	}

	function getLockCR($request = array()){
		empty($request['cr']) and $request['cr'] = 0;
		empty($request['member']) and $request['member'] = 0;
		if(!empty($request['member']) && !empty($request['cr'])) {
			$query='select l.id, l.access_flag, l.date_locked, u.username from customerreview l, member u where l.id='.$request['cr'].' and l.access_flag=u.id';
			$selected = $this->DB->SelectAny($query);
		}		
		return !empty($selected[0])? $selected[0] : array();
	}


	function makeLock($request = array()) {
		empty($request['layout']) and $request['layout'] = 0;
		empty($request['member']) and $request['member'] = 0;
		if(!empty($request['member']) && !empty($request['layout'])) {
			$this->DB->begin();
			$this->DB->update('layout',array('access_flag' => $request['member'],'date_locked' => 'now()'),array('id' => $request['layout']),0);
			$this->setLogEvent(3,26,3,'Lock Layout ['.$request['layout'].']',$request['layout']);
			$this->DB->commit();
		}
		return $request;
	}

	function makeLockCR($request = array()) {
		empty($request['cr']) and $request['cr'] = 0;
		empty($request['member']) and $request['member'] = 0;
		if(!empty($request['member']) && !empty($request['cr'])) {
			$this->DB->begin();
			$this->DB->update('customerreview',array('access_flag' => $request['member'],'date_locked' => 'now()'),array('id' => $request['cr']),0);
			$this->setLogEvent(3,8,3,'Lock Customer Review ['.$request['cr'].']',$request['cr']);
			$this->DB->commit();
		}
		return $request;
	}

	function unLock($request = array()) {
		empty($request['layout']) and $request['layout'] = 0;
		empty($request['member']) and $request['member'] = 0;
		if(!empty($request['member']) && !empty($request['layout'])) {
			$this->DB->begin();
			$this->DB->update('layout',array('access_flag' => '0'),array('id' => $request['layout']));
			$this->setLogEvent(3,8,4,'UnLock Layout ['.$request['layout'].']',$request['layout']);
			$this->DB->commit();
		}
		return $request;
	}

	function unLockCR($request = array()) {
		empty($request['cr']) and $request['cr'] = 0;
		empty($request['member']) and $request['member'] = 0;
		if(!empty($request['member']) && !empty($request['cr'])) {
			$this->DB->begin();
			$this->DB->update('customerreview',array('access_flag' => '0'),array('id' => $request['cr']));
			$this->setLogEvent(3,26,4,'UnLock CustomerReview ['.$request['cr'].']',$request['cr']);
			$this->DB->commit();
		}
		return $request;
	}

	function getUser($request = array()){
		$query='select username from member where id='.$request['member'];
			$selected = $this->DB->SelectAny($query);
		return !empty($selected[0])? $selected[0] : array();
	}

	function saveCR($request = array('id' => 1)) {
		global $Server, $TOOLS_PATH;;
		$this->undo = '';
		$cr = new CustomerReview();
		$this->parseRequest($request,$cr);
		$selected = $this->DB->SelectAny("select id from customerreview where name='".$cr->name."'".(empty($cr->id)? "" : " and id != ".$cr->id));
		if(!empty($selected)) {
			$this->DB->good = FALSE;
			$cr->_error = 'Customer Review with such name already exists';
			return $cr;
		}
		$this->DB->begin();
		if(empty($cr->id)) {
			$cr->uid = $this->getUID('customerreview');
			$cr->date_created = 'now';
		}
		$cr->access_flag = 0;
		$cr->date_changed = 'now';
		if(!empty($cr->id)) {
			$Undo_field= array('type' => 19,'id' => $cr->id,'undo' => 1);
			include($TOOLS_PATH.'\undo.php');
			$this->undo = $undo_handler->arr_obj;
		}
		if(!empty($request['duplicate_id'])) $this->DB->update('product',array('access_flag' => 0),array('id' => $request['duplicate_id']));
		if(empty($cr->id)) $this->setLogEvent(3,26,11,'Add Customer Review "'.$cr->name.'"');
		else $this->setLogEvent(3,26,12,'Edit Customer Review "'.$cr->name.'" ['.$cr->id.']',$cr->id);
		$this->DB->processObject($cr,'save',array(),array(),TRUE);
		if(empty($cr->uid)) {
			$selected = $this->DB->SelectAny('SELECT uid FROM customerreview WHERE id = '.$cr->id.' LIMIT 1');
			if(!empty($selected) && !empty($selected[0]['uid'])) $this->DB->delete('deleted', array('uid' => $selected[0]['uid'], 'type' => 1));
		}
		else $this->DB->delete('deleted', array('uid' => $cr->uid, 'type' => 1));
		// comment
		$crcomment= new CustomerReviewComment();
		$crcomment->version = 1;
		$crcomment->value = 'Customer Review has been created';
		$selected = $this->DB->SelectAny("select version from customerreviewcomment where ownerid = ".$cr->id." order by date desc LIMIT 1 OFFSET 0");
		if(!empty($selected[0]['version'])) {
			$crcomment->version=++$selected[0]['version'];
			$crcomment->value = (!empty($request['_undo_'])? 'Customer Review has been undone up to '.$request['_undo_']." version<br>\r\n" : '').htmlspecialchars($request['comment']);
		}
		$crcomment->ownerid = $cr->id;
		$crcomment->member = $request['id_user'];
		$crcomment->date = 'now';
		$this->DB->processObject($crcomment,'save',array(),array(),TRUE);
		//$this->DB->rollback();
		if(!empty($request['_undo_'])) $this->setLogEvent(3,26,10,'Customer Review "'.$cr->name.'" ['.$cr->id.'] has been undone up to '.$request['_undo_'].' version',$cr->id);
		$this->DB->commit();
		if($this->DB->good && !empty($this->undo)) {
			$undo_handler->GetArrayInFile($this->undo,'Undo_object', $ROOT_PATH."/undo/".$pub_type[0]['name'].'/'.$Undo_field['id'].'/'.($crcomment->version > 1? $crcomment->version-1 : 1).".inc");
		}
		return $cr;
	}

	function saveLayout($request = array('id' => 1)) {
//printArray($request);exit;
		global $Server, $TOOLS_PATH,$REGISTRY;
		$this->undo = '';
		$layout = new Layout();
		$this->parseRequest($request,$layout);
		$selected = $this->DB->SelectAny("select id from layout where name='".$layout->name."'".(empty($layout->id)? "" : " and id != ".$layout->id));
		if(!empty($selected)) {
			$this->DB->good = FALSE;
			$layout->_error = 'Layout with such name already exists';
			return $layout;
		}
		$this->DB->begin();
		if(!empty($layout->id)) $layout->old_name = $this->DB->SelectSingle(array('layout'),'name',array('id' => $layout->id));
		if(empty($layout->id)) {
			$layout->uid = $this->getUID('layout');
			$layout->date_created = 'now';
		}
		$layout->access_flag = 0;
		$layout->date_changed = 'now';
		if(!empty($layout->id)) {
			$Undo_field= array('type' => 1,'id' => $layout->id,'undo' => 1);
			include($TOOLS_PATH.'\undo.php');
			$this->undo = $undo_handler->arr_obj;
		}
		if(empty($layout->id)) $this->setLogEvent(3,8,11,'Add layout "'.$layout->name.'"');
		else $this->setLogEvent(3,8,12,'Edit layout "'.$layout->name.'" ['.$layout->id.']',$layout->id);
		$this->DB->processObject($layout,'save',array(),array(),TRUE);
		if(empty($layout->uid)) {
			$selected = $this->DB->SelectAny('SELECT uid FROM layout WHERE id = '.$layout->id.' LIMIT 1');
			if(!empty($selected) && !empty($selected[0]['uid'])) $this->DB->delete('deleted', array('uid' => $selected[0]['uid'], 'type' => 1));
		}
		else $this->DB->delete('deleted', array('uid' => $layout->uid, 'type' => 1));
		$this->DB->delete('layoutcontenttype',array('ownerid' => $layout->id));
		empty($request['layoutcontenttype']) and $request['layoutcontenttype'] = array();
		foreach($request['layoutcontenttype'] as $key => $value) {
			$lct = new LayoutContentType();
			$lct->ownerid = $layout->id;
			$lct->type = $value['type'];
			$this->DB->processObject($lct,'save',array(),array(),TRUE);
		}
		// comment
		$layoutcomment= new LayoutComment();
		$layoutcomment->version = 1;
		$layoutcomment->value = 'Layout has been created';
		$selected = $this->DB->SelectAny("select version from layoutcomment where ownerid = ".$layout->id." order by date desc LIMIT 1 OFFSET 0");
		if(!empty($selected[0]['version'])) {
			$layoutcomment->version=++$selected[0]['version'];
			$layoutcomment->value = (!empty($request['_undo_'])? 'Layout has been undone up to '.$request['_undo_']." version<br>\r\n" : '').htmlspecialchars($request['comment']);
		}
		$layoutcomment->ownerid = $layout->id;
		$layoutcomment->member = $request['id_user'];
		$layoutcomment->date = 'now';
		$this->DB->processObject($layoutcomment,'save',array(),array(),TRUE);
		//$this->DB->rollback();
		if(!empty($request['_undo_'])) $this->setLogEvent(3,8,10,'Layout "'.$layout->name.'" ['.$layout->id.'] has been undone up to '.$request['_undo_'].' version',$layout->id);
		$this->DB->commit();
		if($this->DB->good && !empty($this->undo)) {
			$undo_handler->GetArrayInFile($this->undo,'Undo_object', $ROOT_PATH."/undo/".$pub_type[0]['name'].'/'.$Undo_field['id'].'/'.($layoutcomment->version > 1? $layoutcomment->version-1 : 1).".inc");
		}
		// clear cache
/*		foreach($REGISTRY['VirtualSlotTarget'][''] as $path => $layoutid) {
			if($layoutid == $layout->id) 
				clearCacheByURL($path);		
		}*/
		clearCache();
		return $layout;
	}

	function saveCSS($request = array('id' => 1)) {
		global $TOOLS_PATH, $ROOT_PATH;
		$Undo_field= array('type' => 0,'id' => 'default','undo' => 1);
		include($TOOLS_PATH.'\undo.php');
		$this->undo = $undo_handler->arr_obj;
		$csscomment= new CSSComment();
		$csscomment->version = 1;
		$csscomment->value = 'CSS has been created';
		$selected = $this->DB->SelectAny("select version from csscomment order by date desc LIMIT 1 OFFSET 0");
		if(!empty($selected[0]['version'])) {
			$csscomment->version=++$selected[0]['version'];
			$csscomment->value = (!empty($request['_undo_'])? 'CSS has been undone up to '.$request['_undo_']." version<br>\r\n" : '').htmlspecialchars($request['comment']);
		}
		$csscomment->member = $request['id_user'];
		$csscomment->date = 'now';
		$this->DB->processObject($csscomment,'save',array(),array(),TRUE);
		$this->setLogEvent(3,7,12,'Edit default CSS file');
		if(!empty($request['_undo_'])) $this->setLogEvent(3,7,10,'CSS file has been undone up to '.$request['_undo_'].' version');
		$this->DB->commit();
		if($this->DB->good) $undo_handler->GetArrayInFile($this->undo,'Undo_object', $ROOT_PATH."/undo/".$pub_type[0]['name']."/".($csscomment->version>1? $csscomment->version-1 : 1).".inc");
		return $request;
	}

	function deleteCR($request = array('id' => 1)) {
		global $REGISTRY, $Server;
		$cr = new CustomerReview();
		$this->parseRequest($request,$cr);
		$selected = $this->DB->SelectAny("select id from customerreviewitem where object=".$cr->id);
		if(!empty($selected)) {
			$this->DB->good = FALSE;
			$cr->_error = 'This Customer Review can\'t be removed because it uses a page, category or product';
			return $cr;
		}
		$this->DB->begin();
		$this->DB->initObject($cr);
		if(!empty($cr->name)) {
			$this->setLogEvent(3,26,13,'Delete Customer Review "'.$cr->name.'" ['.$cr->id.']',$cr->id);
			$this->DB->processObject($cr,'delete',array(),array(),TRUE);
			$this->DB->delete('customerreviewcomment',array('ownerid' => $cr->id));
		}
		$this->DB->commit();
		return $cr;
	}

	function deleteLayout($request = array('id' => 1)) {
		global $REGISTRY, $Server;
		$layout = new Layout();
		$this->parseRequest($request,$layout);
		$this->DB->begin();
		$this->DB->initObject($layout);
		if(!empty($layout->name)) {
			$this->DB->delete('layoutcontenttype',array('ownerid' => $layout->id));
			$presets = $this->DB->SelectGuide(array('preset g'),'*',array());
			foreach($presets as $prekey => $prevalue)
				$this->DB->insert('deleted',array('uid' => $layout->uid,'preset' => $prekey,'type' => 1));
			$this->setLogEvent(3,8,13,'Delete layout "'.$layout->name.'" ['.$layout->id.']',$layout->id);
			$this->DB->processObject($layout,'delete',array(),array(),TRUE);
			$this->DB->delete('layoutcomment',array('ownerid' => $layout->id));
		}
		//$this->DB->rollback();
		$this->DB->commit();
		return $layout;
	}

	function selectLayoutComments($request = array()){
		if(empty($request['fields'])) $request['fields']='lc.*,m.username as member';
		if(empty($request['sortColumn'])) { $request['sortColumn']='lc.version'; $request['sortType']='desc';}
		$request['conditions']['m.id']=array('='=>'lc.member');
		$request['conditions']['lc.ownerid']=array('='=>$request[1]);
		foreach($request['conditions'] as $k=>$v) $rels[]='and';
		if(empty($request['curPageNumber'])) $request['curPageNumber']=1;
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty']=10;
		$selected=$this->DB->SelectArray(Array('member m','layoutcomment lc'),'count(*) as qty',$request['conditions'],0,0,0,array(),'CND_COMPLEX',$rels);
		$request['recordsTotal']=$selected[0]['qty'];
		$request['pagesTotal'] = ceil($request['recordsTotal']/$request['pageRecordsQty']);
		$request['curPage']=$this->DB->SelectArray(Array('member m','layoutcomment lc'),$request['fields'],$request['conditions'],$this->getOrderBy($request),$this->getLimit($request),0,array(),'CND_COMPLEX',$rels);
		return $request;
	}

	function selectCRComments($request = array()){
		if(empty($request['fields'])) $request['fields']='cc.*,m.username as member';
		if(empty($request['sortColumn'])) { $request['sortColumn']='cc.version'; $request['sortType']='desc';}
		$request['conditions']['m.id']=array('='=>'cc.member');
		foreach($request['conditions'] as $k=>$v) $rels[]='and';
		if(empty($request['curPageNumber'])) $request['curPageNumber']=1;
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty']=10;
		$selected=$this->DB->SelectArray(Array('member m','customerreviewcomment cc'),'count(*) as qty',$request['conditions'],0,0,0,array(),'CND_COMPLEX',$rels);
		$request['recordsTotal']=$selected[0]['qty'];
		$request['pagesTotal'] = ceil($request['recordsTotal']/$request['pageRecordsQty']);
		$request['curPage']=$this->DB->SelectArray(Array('member m','customerreviewcomment cc'),$request['fields'],$request['conditions'],$this->getOrderBy($request),$this->getLimit($request),0,array(),'CND_COMPLEX',$rels);
		return $request;
	}

	function selectCSSComments($request = array()){
		if(empty($request['fields'])) $request['fields']='cc.*,m.username as member';
		if(empty($request['sortColumn'])) { $request['sortColumn']='cc.version'; $request['sortType']='desc';}
		$request['conditions']['m.id']=array('='=>'cc.member');
		foreach($request['conditions'] as $k=>$v) $rels[]='and';
		if(empty($request['curPageNumber'])) $request['curPageNumber']=1;
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty']=10;
		$selected=$this->DB->SelectArray(Array('member m','csscomment cc'),'count(*) as qty',$request['conditions'],0,0,0,array(),'CND_COMPLEX',$rels);
		$request['recordsTotal']=$selected[0]['qty'];
		$request['pagesTotal'] = ceil($request['recordsTotal']/$request['pageRecordsQty']);
		$request['curPage']=$this->DB->SelectArray(Array('member m','csscomment cc'),$request['fields'],$request['conditions'],$this->getOrderBy($request),$this->getLimit($request),0,array(),'CND_COMPLEX',$rels);
		return $request;
	}
	
	function getHostPath($request = array()){
		$selected = $this->DB->SelectAny('SELECT '.($request['type'] == 1 ? 'name' : 'host').' AS hostpath FROM '.($request['type'] == 1 ? 'preset' : 'publisher').' WHERE id = '.$request['host'].' LIMIT 1');
		$hostpath = empty($selected) ? '[undefined]' : $selected[0]['hostpath'];
		return $hostpath;
	}
	
	function getUserName($request = array()){
		$selected = $this->DB->SelectAny('SELECT username FROM '.($request['type'] == 1 ? 'member' : 'publisher_user').' WHERE id = '.$request['userid'].' LIMIT 1');
		$username = empty($selected) ? '[undefined]' : $selected[0]['username'];
		return $username;
	}
}
?>