<?php

class FeedHandler extends Handler{
	
	function createImportedFeed ($with_data = false) {
		$importedfeed = new ImportedFeed();
		$importedfeed->feedsources = new ObjectsList();
		$importedfeed->feedsources->type = $this->createImportedFeedSource($with_data);
		return $importedfeed;
	}
	
	function createImportedFeedSource ($with_data = false){
		$feedsource = new ImportedFeedSource();
		if($with_data){
			$feedsource->feeddata = new ObjectsList();
			$feedsource->feeddata->type = $this->createImportedFeedData();
		}
		return $feedsource;
	}
	
	function createImportedFeedData(){
		return new ImportedFeedData();
	}
	
	function selectImportedFeeds ($request = array()) {
		GLOBAL $ROOT_PATH, $COMMON_VARS;
		$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();
		$result = $this->DB->SelectAny('SELECT id, name, title, date_changed, (\'now\'::date - date_changed::date) AS unmodified, access_flag, date_locked FROM importedfeed ORDER BY id');
		$count = count($result);
		for ($i = 0; $i < $count; $i++) {
			$result[$i]['user_name'] = $this->getUsername($result[$i]['access_flag']);
			$result[$i]['_error'] = '';
			$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 = 13 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 getImportedFeedLock ($request = array()) {
		$query = 'SELECT importedfeed.access_flag, importedfeed.date_locked, member.username FROM importedfeed, member WHERE importedfeed.id = '.$request['id'].' AND member.id = importedfeed.access_flag LIMIT 1';
		$selected = $this->DB->SelectAny($query);
		return empty($selected[0]) ? array() : $selected[0];
	}
	
	function lockImportedFeed ($request = array()) {
		$this->setLogEvent(3, 19, 3, 'Lock Imported Feed ['.$request['id'].']', $request['id']);
		$this->DB->update('importedfeed', array('access_flag' => $request['member'], 'date_locked' => 'now'), array('id' => $request['id']));
		$this->DB->commit();
		return $request;
	}
	
	function unlockImportedFeed ($request = array()) {
		$this->setLogEvent(3, 19, 4, 'UnLock Imported Feed ['.$request['id'].']', $request['id']);
		$this->DB->update('importedfeed', array('access_flag' => 0), array('id' => $request['id']));
		$this->DB->commit();
		return array();
	}
	
	function openImportedFeed ($request = array()) {
		GLOBAL $ROOT_PATH, $COMMON_VARS, $TOOLS_PATH;
		$importedfeed = empty($request['with_data']) ? $this->createImportedFeed() : $this->createImportedFeed(true);
		if(empty($request['undo'])) {
			$importedfeed->id = $request['id'];
			$this->DB->initObject($importedfeed);
		}
		else {
			$Undo_field = array('type' => 13, 'id' => $request['id'], 'undo' => 0, 'version' => $request['undo']);
			include ($TOOLS_PATH.'/undo.php');
			$importedfeed = $print_arr;
		}
		$importedfeed->comments = $this->DB->SelectAny('SELECT ifc.*, m.username AS member FROM importedfeedcomment ifc, member m WHERE m.id = ifc.member AND ifc.ownerid = '.$request['id'].' ORDER BY ifc.version DESC LIMIT 10 OFFSET 0');
		$count = count($importedfeed->comments);
		for ($i = 0; $i < $count; $i++) $importedfeed->comments[$i]['file'] = file_exists($ROOT_PATH.'/undo/importedfeed/'.$request['id'].'/'.$importedfeed->comments[$i]['version'].'.inc') ? true : false;
		return $importedfeed;
	}
	
	function saveImportedFeed ($request = array()) {
		GLOBAL $ROOT_PATH, $COMMON_VARS, $TOOLS_PATH;
		$this->undo = 0;
		$importedfeed = $this->createImportedFeed(true);
		$this->parseRequest($request, $importedfeed);
		$selected = $this->DB->SelectAny('SELECT id, name FROM importedfeed WHERE name = \''.$importedfeed->name.'\' LIMIT 1');
		if(!empty($selected) && $selected[0]['id'] != $importedfeed->id) {
			$importedfeed->_error = 'Imported Feed with such name already exists';
			return $importedfeed;
		}
		if(empty($importedfeed->id)){
			$importedfeed->uid = getUnique32();
			$importedfeed->date_created = 'now';
			$this->setLogEvent(3, 19, 11, 'Add Imported Feed "'.$importedfeed->name.'"');
		}
		else {
			$this->setLogEvent(3, 19, 12, 'Edit Imported Feed "'.$importedfeed->name.'" ['.$importedfeed->id.']', $importedfeed->id);
			$Undo_field = array('type' => 13, 'id' => $importedfeed->id, 'undo' => 1);
			include($TOOLS_PATH.'/undo.php');
			$this->undo = $undo_handler->arr_obj;
		}
		$importedfeed->date_changed = 'now';
		if (empty($importedfeed->uid)) $importedfeed->uid = getUnique32();
		$importedfeed->link = $COMMON_VARS['imported_feed_path'].'/'.$importedfeed->uid;
		$this->DB->processObject($importedfeed, 'save', array(), array(), TRUE);
		$this->DB->commit();
		$this->DB->delete('deleted', array('uid' => $importedfeed->uid, 'type' => 13));
		$this->DB->begin();
		$comment = new ImportedFeedComment();
		$comment->version = 1;
		$comment->value = 'Imported Feed has been created';
		$selected = $this->DB->SelectAny('SELECT version FROM importedfeedcomment WHERE ownerid = '.$importedfeed->id.' ORDER BY date DESC LIMIT 1 OFFSET 0');
		if(!empty($selected[0]['version'])) {
			$comment->version = ++$selected[0]['version'];
			$comment->value = (!empty($request['undo']) ? 'Imported Feed has been undone up to '.$request['undo'].' version<br>'."\r\n" : '').htmlspecialchars($request['comment']);
		}
		$comment->ownerid = $importedfeed->id;
		$comment->member = empty($request['id_user']) ? $this->Security->currentuser->id : $request['id_user'];
		$comment->date = 'now';
		$this->DB->processObject($comment, 'save', array(), array(), TRUE);
		if(!empty($request['undo'])) $this->setLogEvent(3, 19, 10, 'Imported Feed "'.$importedfeed->name.'" ['.$importedfeed->id.'] has been undone up to '.$request['undo'].' version', $importedfeed->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'].'/'.($comment->version > 1 ? $comment->version - 1 : 1).'.inc');
		include_once($TOOLS_PATH.'/ImportedFeedDataUpdateHandler.class.inc');
		$Handler = new ImportedFeedDataUpdateHandler($this, false);
		$Handler->updateFeed($importedfeed->id, true);//@$request['forced_update']
		return $importedfeed;
	}
	
	function openImportedFeedSource ($request = array()){
		$feedsource = empty($request['with_data']) ? $this->createImportedFeedSource() : $this->createImportedFeedSource(true);
		$feedsource->id = $request['id'];
		$this->DB->initObject($feedsource);
		if(empty($feedsource->_error)) {
			$selected = $this->DB->SelectAny('SELECT updating_period FROM importedfeed WHERE id = '.$feedsource->ownerid.' LIMIT 1');
			if(empty($selected)) $feedsource->_error = 'Parent Imported Feed doesn\'t exist';
			else $feedsource->feed_updating_period = $selected[0]['updating_period'];
		}
		return $feedsource;
	}
	
	function saveImportedFeedSource ($request = array()){
		$importedfeedsource = $this->createImportedFeedSource(true);
		$this->parseRequest($request, $importedfeedsource);
		$this->DB->begin();
		$this->DB->processObject($importedfeedsource, 'save', array(), array(), TRUE);
		$this->DB->commit();
		return $importedfeedsource;
	}
	
	function deleteImportedFeed($request = array()){
		GLOBAL $REGISTRY, $Server;
		$importedfeed = $this->createImportedFeed(true);
		$this->parseRequest($request, $importedfeed);
		$this->DB->initObject($importedfeed);
		$presets = $this->DB->SelectGuide(array('preset g'), '*', array());
		$this->setLogEvent(3, 19, 13, 'Delete Imported Feed "'.$importedfeed->name.'" ['.$importedfeed->id.']', $importedfeed->id);
		foreach($presets as $prekey => $prevalue) {
			$selected = $this->DB->SelectAny('SELECT id FROM deleted WHERE uid = \''.$importedfeed->uid.'\' AND preset = '.$prekey.' AND type = 13 LIMIT 1');
			if(empty($selected)) $this->DB->insert('deleted', array('uid' => $importedfeed->uid, 'preset' => $prekey, 'type' => 13));
		}
		$this->DB->processObject($importedfeed, 'delete', array(), array(), TRUE);
		$this->DB->delete('importedfeedcomment', array('ownerid' => $importedfeed->id));
		$this->DB->commit();
		return $importedfeed;
	}
	
	function createFeed () {
		$feed = new Feed();
		$feed->items = new ObjectsList();
		$feed->items->type = $this->createFeedItem();
		return $feed;
	}
	
	function createFeedItem(){
		return new FeedItem();
	}
	
	function selectFeeds($request = array()) {
		GLOBAL $ROOT_PATH, $COMMON_VARS;
		$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();
		$result = $this->DB->SelectAny('SELECT id, name, title, date_changed, (\'now\'::date - date_changed::date) AS unmodified, access_flag, date_locked FROM feed ORDER BY id');
		$count = count($result);
		for ($i = 0; $i < $count; $i++) {
			$result[$i]['user_name'] = $this->getUsername($result[$i]['access_flag']);
			$result[$i]['_error'] = '';
			$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 = 15 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 getFeedLock ($request = array()) {
		$query = 'SELECT feed.access_flag, feed.date_locked, member.username FROM feed, member WHERE feed.id = '.$request['id'].' AND member.id = feed.access_flag LIMIT 1';
		$selected = $this->DB->SelectAny($query);
		return empty($selected[0]) ? array() : $selected[0];
	}
	
	function lockFeed ($request = array()) {
		$this->setLogEvent(3, 21, 3, 'Lock Feed ['.$request['id'].']', $request['id']);
		$this->DB->update('feed', array('access_flag' => $request['member'], 'date_locked' => 'now'), array('id' => $request['id']));
		$this->DB->commit();
		return $request;
	}
	
	function unlockFeed ($request = array()) {
		$this->setLogEvent(3, 21, 4, 'UnLock Feed ['.$request['id'].']', $request['id']);
		$this->DB->update('feed', array('access_flag' => 0), array('id' => $request['id']));
		$this->DB->commit();
		return array();
	}
	
	function openFeed ($request = array()) {
		GLOBAL $ROOT_PATH, $COMMON_VARS, $TOOLS_PATH;
		$feed = $this->createFeed();
		if(empty($request['undo'])) {
			$feed->id = $request['id'];
			$this->DB->initObject($feed);
		}
		else {
			$Undo_field = array('type' => 15, 'id' => $request['id'], 'undo' => 0, 'version' => $request['undo']);
			include ($TOOLS_PATH.'/undo.php');
			$feed = $print_arr;
		}
		$feed->comments = $this->DB->SelectAny('SELECT fc.*, m.username AS member FROM feedcomment fc, member m WHERE m.id = fc.member AND fc.ownerid = '.$request['id'].' ORDER BY fc.version DESC LIMIT 10 OFFSET 0');
		$count = count($feed->comments);
		for ($i = 0; $i < $count; $i++) $feed->comments[$i]['file'] = file_exists($ROOT_PATH.'/undo/feed/'.$request['id'].'/'.$feed->comments[$i]['version'].'.inc') ? true : false;
		return $feed;
	}
	
	function saveFeed($request = array()) {
		GLOBAL $ROOT_PATH, $COMMON_VARS, $TOOLS_PATH;
		$this->undo = 0;
		$feed = $this->createFeed();
		$this->parseRequest($request, $feed);
		$selected = $this->DB->SelectAny('SELECT id, name FROM feed WHERE name = \''.$feed->name.'\' LIMIT 1');
		if(!empty($selected) && $selected[0]['id'] != $feed->id) {
			$feed->_error = 'Feed with such name already exists';
			return $feed;
		}
		if(empty($feed->id)){
			$feed->uid = getUnique32();
			$feed->date_created = 'now';
			$this->setLogEvent(3, 21, 11, 'Add Feed "'.$feed->name.'"');
		}
		else {
			$this->setLogEvent(3, 21, 12, 'Edit Feed "'.$feed->name.'" ['.$feed->id.']', $feed->id);
			$Undo_field = array('type' => 15, 'id' => $feed->id, 'undo' => 1);
			include($TOOLS_PATH.'/undo.php');
			$this->undo = $undo_handler->arr_obj;
		}
		$feed->date_changed = 'now';
		if (empty($feed->uid)) $feed->uid = getUnique32();
		$feed->link = $COMMON_VARS['feed_path'].'/'.$feed->uid;
		$this->DB->processObject($feed, 'save', array(), array(), TRUE);
		$this->DB->commit();
		$this->DB->delete('deleted', array('uid' => $feed->uid, 'type' => 15));
		$this->DB->begin();
		$comment = new FeedComment();
		$comment->version = 1;
		$comment->value = 'Feed has been created';
		$selected = $this->DB->SelectAny('SELECT version FROM feedcomment WHERE ownerid = '.$feed->id.' ORDER BY date DESC LIMIT 1 OFFSET 0');
		if(!empty($selected[0]['version'])) {
			$comment->version = ++$selected[0]['version'];
			$comment->value = (!empty($request['undo']) ? 'Feed has been undone up to '.$request['undo'].' version<br>'."\r\n" : '').htmlspecialchars($request['comment']);
		}
		$comment->ownerid = $feed->id;
		$comment->member = empty($request['id_user']) ? $this->Security->currentuser->id : $request['id_user'];
		$comment->date = 'now';
		$this->DB->processObject($comment, 'save', array(), array(), TRUE);
		if(!empty($request['undo'])) $this->setLogEvent(3, 21, 10, 'Feed "'.$feed->name.'" ['.$feed->id.'] has been undone up to '.$request['undo'].' version', $feed->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'].'/'.($comment->version > 1 ? $comment->version - 1 : 1).'.inc');
		return $feed;
	}
	
	function deleteFeed($request = array()){
		GLOBAL $REGISTRY, $Server;
		$feed = $this->createFeed();
		$this->parseRequest($request, $feed);
		$this->DB->initObject($feed);
		$presets = $this->DB->SelectGuide(array('preset g'), '*', array());
		$this->setLogEvent(3, 21, 13, 'Delete Feed "'.$feed->name.'" ['.$feed->id.']', $feed->id);
		foreach($presets as $prekey => $prevalue) {
			$selected = $this->DB->SelectAny('SELECT id FROM deleted WHERE uid = \''.$feed->uid.'\' AND preset = '.$prekey.' AND type = 15 LIMIT 1');
			if(empty($selected)) $this->DB->insert('deleted', array('uid' => $feed->uid, 'preset' => $prekey, 'type' => 15));
		}
		$this->DB->processObject($feed, 'delete', array(), array(), TRUE);
		$this->DB->delete('feedcomment', array('ownerid' => $feed->id));
		$this->DB->commit();
		return $feed;
	}
	
	function flushFeed($feedid, $feedtype = 1) {
		GLOBAL $TOOLS_PATH, $REGISTRY;
		$result = array();
		if(!is_numeric($feedid)) return $result;
		switch($feedtype) {
			case 2: $query = 'SELECT link FROM feed WHERE id = '.$feedid.' LIMIT 1'; break;
			case 1: default: $query = 'SELECT link FROM importedfeed WHERE id = '.$feedid.' LIMIT 1'; break;
		}
		$selected = $this->DB->SelectAny($query);
		if(empty($selected)) return $result;
		$flush_path = array('http://'.strtolower($REGISTRY['Core']['SiteName']).'/'.$selected[0]['link']);
		include_once($TOOLS_PATH.'/FlushHandler.class.inc');
		$FlushHandler = new FlushHandler();
		$FlushHandler->setFlushPaths($flush_path);
		//$FlushHandler->setEmails(array('whcrow@a3d.od.ua', 'gladkov@a3d.od.ua', 'winwolf@a3d.od.ua'));
		$result = $FlushHandler->launchFlushScript();
		return $result;
	}
	
	function flushPages($feedid, $feedtype = 1) {
		GLOBAL $TOOLS_PATH, $REGISTRY;
		$result = array();
		if(!is_numeric($feedid)) return $result;
		switch($feedtype) {
			case 2: $query = 'SELECT DISTINCT ownerid FROM modulefeed WHERE feed = '.$feedid; break;
			case 1: default: $query = 'SELECT DISTINCT ownerid FROM moduleimportedfeed WHERE importedfeed = '.$feedid; break;
		}
		$selected = $this->DB->SelectAny($query);
		$flush_paths = array();
		$count = count($selected);
		for ($i = 0; $i < $count; $i++) {
			$pagepath = $this->getPagePath($selected[$i]['ownerid']);
			$flush_paths[] = $pagepath;
		}
		$prepath = 'http://'.strtolower($REGISTRY['Core']['SiteName']).'/';
		$count = count($flush_paths);
		for ($i = 0; $i < $count; $i++) $flush_paths[$i] = $prepath.$flush_paths[$i];
		include_once($TOOLS_PATH.'/FlushHandler.class.inc');
		$FlushHandler = new FlushHandler();
		$FlushHandler->setFlushPaths($flush_paths);
		//$FlushHandler->setEmails(array('whcrow@a3d.od.ua', 'gladkov@a3d.od.ua', 'winwolf@a3d.od.ua'));
		$result = $FlushHandler->launchFlushScript();
		return $result;
	}
	
	function getPagePath($id){
		$path = '';
		while($id) {
			$selected = $this->DB->SelectAny('SELECT path, parent FROM module WHERE id = '.$id.' LIMIT 1');
			if(empty($selected)) break;
			$path = $selected[0]['path'].(empty($path) ? '' : '/'.$path);
			$id = $selected[0]['parent'];
		}
		return $path;
	}
	
	function getUsername ($id) {
		if(!is_numeric($id)) $id = 0;
		$selected = $this->DB->SelectAny('SELECT username FROM member WHERE id = '.$id.' LIMIT 1');
		return empty($selected) ? '' : $selected[0]['username'];
	}
	
	function selectImportedFeedComments ($request = array()) {
		GLOBAL $ROOT_PATH;
		if(empty($request['curPageNumber'])) $request['curPageNumber'] = 1;
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty'] = 20;
		$importedfeed = $this->DB->SelectAny('SELECT name FROM importedfeed WHERE id = '.$request['id'].' LIMIT 1');
		$request['name'] = $importedfeed[0]['name'];
		$selected = $this->DB->SelectAny('SELECT ifc.*, m.username AS member FROM importedfeedcomment ifc, member m WHERE m.id = ifc.member AND ifc.ownerid = '.$request['id'].' ORDER BY ifc.version DESC LIMIT '.$request['pageRecordsQty'].' OFFSET '.(($request['curPageNumber'] - 1) * $request['pageRecordsQty']));
		$count = count($selected);
		for ($i = 0; $i < $count; $i++) $selected[$i]['file'] = file_exists($ROOT_PATH.'/undo/importedfeed/'.$request['id'].'/'.$selected[$i]['version'].'.inc') ? true : false;
		$count = $this->DB->SelectAny('SELECT count(id) AS count FROM importedfeedcomment WHERE ownerid = '.$request['id']);
		$request['recordsTotal'] = $count[0]['count'];
		$request['pagesTotal'] = ceil($request['recordsTotal'] / $request['pageRecordsQty']);
		$request['curPage'] = $selected;
		return $request;
	}
	
	function selectFeedComments ($request = array()) {
		GLOBAL $ROOT_PATH;
		if(empty($request['curPageNumber'])) $request['curPageNumber'] = 1;
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty'] = 20;
		$feed = $this->DB->SelectAny('SELECT name FROM feed WHERE id = '.$request['id'].' LIMIT 1');
		$request['name'] = $feed[0]['name'];
		$selected = $this->DB->SelectAny('SELECT fc.*, m.username AS member FROM feedcomment fc, member m WHERE m.id = fc.member AND fc.ownerid = '.$request['id'].' ORDER BY fc.version DESC LIMIT '.$request['pageRecordsQty'].' OFFSET '.(($request['curPageNumber'] - 1) * $request['pageRecordsQty']));
		$count = count($selected);
		for ($i = 0; $i < $count; $i++) $selected[$i]['file'] = file_exists($ROOT_PATH.'/undo/feed/'.$request['id'].'/'.$selected[$i]['version'].'.inc') ? true : false;
		$count = $this->DB->SelectAny('SELECT count(id) AS count FROM feedcomment WHERE ownerid = '.$request['id']);
		$request['recordsTotal'] = $count[0]['count'];
		$request['pagesTotal'] = ceil($request['recordsTotal'] / $request['pageRecordsQty']);
		$request['curPage'] = $selected;
		return $request;
	}
	
	function getFeed($request = array()) {
		if($request['type'] == 'I'){
			$selected = $this->DB->SelectAny('SELECT id FROM importedfeed WHERE uid = \''.$request['uid'].'\' LIMIT 1');
			$feed = $this->createImportedFeed(true);
		}
		else if($request['type'] == 'E'){
			$selected = $this->DB->SelectAny('SELECT id FROM feed WHERE uid = \''.$request['uid'].'\' LIMIT 1');
			$feed = $this->createFeed();
		}
		$feed->id = @$selected[0]['id'];
		$this->DB->initObject($feed);
		return $feed;
	}
	
}

?>