<?php 

namespace app\models;

use app\models\Entry; 
use \phpQuery;
use \Archive\Tar
use app\utils\Gzip.php;
class contentList extends \lithium\data\Model {
		public function update()
		{
			
		}

		public function add($entity, $data)
		{
			$updateData = array('$push' => array(''
			$conditions =  array('_id' => $user['_id']);
			$result = User::update($updateData, $conditions, array('atomic' => false));
		}

		public function importManga() {
			
		}

		public static function importAnimeXML{$user, $file}
		{
			$xml = ungzip($file);
			$list = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

			foreach ($list->animelist as $entry)
			{
				$user->animelist[] = Entry::create($entry);
			}

			return $user->save(null, array('validate' => false));
		}

	/*	public function importAnime($user, $malun, $malpass) {
			//This method assumes we are importing from mal at the moment.

			//1. Parse Mal for the download link
			//1.1 Login to MAL
			$cmd = "curl -c cookie.txt -d \"username=$malun&password=$malpass\" http://myanimelist.net/login.php";

			$result = shell_exec($cmd);

			//If the request went ok
			if (empty($result)) {
				//1.2 Request a list export
				$cmd_getList = "curl -b cookie.txt -d \"value=1&subexport=Export My List\" http://myanimelist.net/panel.php?go=export";
				
				$malreturn = shell_exec($cmd_getList);

				//Initate a new document for phpQuery.
				$doc = phpQuery::newDocument($malreturn);

				//Grab the div with the content in it, (should be goodresult actually <div class=goodresult>)
				$mal = pq('#content');

				//If everything goes to plan, the only link the the body will be the one we want,
				$link = $mal->find('a')->attr('href');
				
				$cmd = "curl -b cookie.txt $link";

				$data = shell_exec($cmd);

				$xml_source = gzdecode($data);

				$list = simplexml_load_string($xml_source, 'SimpleXMLElement', LIBXML_NOCDATA);

				foreach($list as $entry) {
					print_r($entry);
				}

			}
			
				

			//2. Download the linked file

			//3. Extract and unzip it

			//4. Take the XML and parse it 
			$xml_source; //<-- the XML code
			$xml = simplexml_load_string($xml_source, 'SimpleXMLElement', LIBXML_NOCDATA);


		   if (!isset($user->animelist))
		   {
		   		$user->animelist = array();
		   }

		   //Create entries
		   foreach($xml->anime as $entry) 
		   {
		   		$user->animelist[] = Entry::create($entry);
		   }

		   //Store the entries to the database
		   return $user->save(null, array('validate' => false));

		}*/
}