<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
error_reporting(0);
@ini_set(display_errors, 0);
  if (!defined("_VALID_PHP"))
      die('Direct access to this location is not allowed.');
  
  class Content
  {
      
	  public $pageid = null;
	  public $id = null;
	  public $postid = null;
	  public $modid = null;
	  private $menutree = array();
	  private $menulist = array();
	  
      /**
       * Content::__construct()
       * 
       * @return
       */
      function __construct()
      {
          $this->getPageId();
		  $this->getId();
		  $this->getPostId();
		  $this->getModId();
		  $this->menutree = $this->getMenuTree();
		  $this->getPageSettings();

      }

	  /**
	   * Content::getPageId()
	   * 
	   * @return
	   */
	  private function getPageId()
	  {
	  	  global $core, $ZAZAVIDebug;
		  if (isset($_GET['pageid'])) {
			  $_GET['pageid'] = sanitize($_GET['pageid'],6,true);
			  $pageid = (is_numeric($_GET['pageid']) && $_GET['pageid'] > -1) ? intval($_GET['pageid']) : false;
		  
			  if ($pageid) {
				  return $this->pageid = $pageid;
			  } else {
				  if($ZAZAVIDebug)
				  	$core->error("You have selected an Invalid Id","Content::getPageId()");
			  }
		  }
	  }

	  /**
	   * Content::getPostId()
	   * 
	   * @return
	   */
	  private function getPostId()
	  {
	  	  global $core;
		  if (isset($_GET['postid'])) {
			  $postid = (is_numeric($_GET['postid']) && $_GET['postid'] > -1) ? intval($_GET['postid']) : false;
			  $postid = sanitize($postid,8,true);
			  
			  if ($postid == false) {
				  $core->error("You have selected an Invalid PostId","Content::getPostId()");
			  } else
				  return $this->postid = $postid;
		  }
	  }

	  /**
	   * Content::getId()
	   * 
	   * @return
	   */
	  private function getId()
	  {
	  	  global $core;
		  if (isset($_GET['id'])) {
			  $id = (is_numeric($_GET['id']) && $_GET['id'] > -1) ? intval($_GET['id']) : false;
			  $id = sanitize($id,8,true);
			  
			  if ($id == false) {
				  $core->error("You have selected an Invalid Id","Content::getId()");
			  } else
				  return $this->id = $id;
		  }
	  }
	  
	  /**
	   * Content::getModId()
	   * 
	   * @return
	   */
	  private function getModId()
	  {
	  	  global $core;
		  if (isset($_GET['modid'])) {
			  $modid = (is_numeric($_GET['modid']) && $_GET['modid'] > -1) ? intval($_GET['modid']) : false;
			  $modid = sanitize($modid,8,true);
			  
			  if ($modid == false) {
				  $core->error("You have selected an Invalid ModId","Content::getModId()");
			  } else
				  return $this->modid = $modid;
		  }
	  }
	  
      /**
       * Content::getPageSettings()
       *
       * @return
       */
      private function getPageSettings()
      {
          global $db, $core;
          $sql = "SELECT * FROM pages WHERE id = '".$this->pageid."'";
          $row = $db->first($sql);
          
		  $this->title = $row['title'.$core->dblang];
		  $this->slug = $row['slug'.$core->dblang];
		  $this->contact_form = $row['contact_form'];
		  $this->cform = $row['cform'];
		  $this->gallery_id = $row['gallery_id'];
		  $this->comments = $row['comments'];
		  $this->membership_id = $row['membership_id'];
		  $this->access = $row['access'];
		  $this->keywords = $row['keywords'.$core->dblang];
		  $this->description = $row['description'.$core->dblang];
		  $this->created = $row['created'];
		  $this->active = $row['active'];

      }
	  
	  /**
	   * Content::getHomePage()
	   * 
	   * @return
	   */
		private function getHomePage()
		{
			global $db, $core;
			
			$sql = "SELECT p.title{$core->dblang}, p.body{$core->dblang}, p.show_title, m.home_page AS home" 
			. "\n FROM pages AS pg" 
			. "\n LEFT JOIN menus AS m ON pg.id = m.page_id" 
			. "\n LEFT JOIN posts AS p ON p.page_id = pg.id" 
			. "\n WHERE m.home_page = '1'";
			$result = $db->fetch_all($sql);
			
			if ($result) {
			  foreach ($result as $row) {
				  if ($row['show_title'] == 1)
					  print "<h1 class=\"home-header\">" . $row['title'.$core->dblang] . "</h1>\n";
					  print "<div class=\"home-body\">" . cleanOut($row['body'.$core->dblang]) . "</div>\n";
			  }
			} else
				print _CONTENT_NOT_FOUND;
		}

	  /**
	   * Content::displayPage()
	   * 
	   * @return
	   */
	  public function displayPage()
	  {
		  if ($this->pageid) {
			  $this->getPagePosts();
		  } elseif (isset($_GET['mode'])) {
			  $this->getSitemap();
		  } else {
			  $this->getHomePage();
		  }
	  }

	  /**
	   * Content::getPagePosts()
	   * 
	   * @return
	   */
	  private function getPagePosts()
	  {
		  global $db, $core, $user, $pg;
		  
		  require_once(ZAZAVILITE . "lib/class_paginate.php");
		  $pg = new Paginator();
		  
		  $counter = countEntries("posts", "page_id", $this->pageid . "' AND active = '1");
		  $pg->items_total = $counter;
		  $pg->default_ipp = $core->perpage;
		  $pg->paginate();
		  
		  if ($counter == 0) {
			  $pg->limit = null;
		  }
		  
		  $sql = "SELECT * FROM posts" 
		  . "\n WHERE page_id = '" . $this->pageid . "'" 
		  . "\n AND active = '1'" 
		  . "\n ORDER BY position" . $pg->limit;
		  $result = $db->fetch_all($sql);
		  
		  if ($result) {
			  if($this->getAccess()) {
				  foreach ($result as $row) {
					  if ($row['show_title'] == 1)
						  print "<h1 class=\"post-header\">" . $row['title' . $core->dblang] . "</h1>\n";
					  print "<div class=\"post-body\">" . cleanOut($row['body' . $core->dblang]) . "</div>\n";
				  }
				  
				  if ($pg->items_total >= $pg->items_per_page) {
					  print "<div class=\"pagination\" style=\"margin-top:10px;\">" . $pg->display_pages() . "</div>";
				  }
				  
				  if ($this->comments <> 0)
					  include(MODDIR . "/comments/main.php");
				  
				  if ($this->contact_form <> 0)
					  include("contact_form.php");

				  if ($this->cform <> 0)
					  include(ZAZAVILITE . "master_form.php");
				  
				  if ($this->gallery_id <> 0)
					  include(MODDIR . "/gallery/main.php");
			  }
		  } else
			  print _CONTENT_NOT_FOUND;
	  }
	  
	  /**
	   * Content::getBreadcrumbs()
	   * 
	   * @return
	   */
	  public function getBreadcrumbs()
	  {
		  global $db, $core;
		  
			$sql = "SELECT pg.title{$core->dblang} as title, m.name{$core->dblang} as name" 
			. "\n FROM pages AS pg" 
			. "\n LEFT JOIN menus AS m ON pg.id = m.page_id" 
			. "\n WHERE pg.id = '".$this->pageid."'";
          $row = $db->first($sql);
          
		  return ($row) ? $row['name'].' &rsaquo; '.$row['title'] : null;
	  }

	  /**
	   * Content::getAccess()
	   * 
	   * @return
	   */
	  public function getAccess()
	  {
		  global $db, $user, $core;
		  $m_arr = explode(",", $this->membership_id);
		  reset($m_arr);
		  
		  switch ($this->access) {
			  case "Registered":
				  if (!$user->logged_in) {
					  $core->msgError(_UA_ACC_ERR1, false);
					  return false;
				  } else
					  return true;
				  break;
				  
			  case "Membership":
				  if ($user->logged_in and $user->validateMembership() and in_array($user->membership_id, $m_arr)) {
					  return true;
				  } else
					  $core->msgError(_UA_ACC_ERR2, false);
					  return false;
				  break;
				  
			  case "Public":
				  return true;
				  break;
				  
			  default:
				  return true;
				  break;
		  }
	  }
	  	  	  
	  /**
	   * Content::getPages()
	   * 
	   * @return
	   */
	  public function getPages()
	  {
		  global $db, $core;
		  
		  $sql = "SELECT *,"
		  . "\n DATE_FORMAT(created, '" . $core->long_date . "') as date"
		  . "\n FROM pages"
		  . "\n ORDER BY title{$core->dblang}";
          $row = $db->fetch_all($sql);
          
		  return ($row) ? $row : 0;
	  }

	  /**
	   * Content::getSitemap()
	   * 
	   * @return
	   */
	  private function getSitemap()
	  {
		  global $db, $core;
		  $pagerow = $this->getPagePost();
		  print "<h1>".$core->site_name."  - "._SM_SITE_MAP."</h1>";
		  print "The ".$core->site_name." "._SM_SITE_MAP_TITLE;
		  print "<div class=\"sitemap\">";
		  foreach ($pagerow as $row) {
			  print "<div class=\"inner\">";
			  print "<h3><a href=\"".createPageLink($row['pageid'])."\">".$row['pagetitle']."</a></h3>";
			  print "<h4>".$row['title'.$core->dblang]."</h4>";
			  $body = cleanOut($row['body'.$core->dblang]);
			  print "<p>".sanitize($body,200)."</p>";
			  print "</div>";
			  print "<hr />";
		  }
		  print "</div>";
	  }
	  
	  /**
	   * Content::processPage()
	   * 
	   * @return
	   */
	  public function processPage()
	  {
		  global $db, $core, $zazavisec;
		  if (empty($_POST['title'.$core->dblang]))
			  $core->msgs['title'] = _PG_TITLE_R;

		  if ($_POST['access'] == "Membership" && !isset($_POST['membership_id']))
			  $core->msgs['access'] = _PG_MEMBERSHIP_R;
			  
		  if (empty($core->msgs)) {
			  $data = array(
				  'title'.$core->dblang => sanitize($_POST['title'.$core->dblang]), 
				  'keywords'.$core->dblang => sanitize($_POST['keywords'.$core->dblang]),
				  'description'.$core->dblang => sanitize($_POST['description'.$core->dblang]),
				  'slug'.$core->dblang => (empty($_POST['slug'.$core->dblang])) ? paranoia($_POST['title'.$core->dblang]) : paranoia($_POST['slug'.$core->dblang]),
				  'contact_form' => intval($_POST['contact_form']),
				  'cform' => intval($_POST['cform']),
				  'gallery_id' => intval($_POST['gallery_id']),
				  'comments' => intval($_POST['comments']),
				  'access' => sanitize($_POST['access']),
				  'active' => intval($_POST['active'])
			  );

			  if (isset($_POST['membership_id'])) {
				  $mids = $_POST['membership_id'];
				  $total = count($mids);
				  $i = 1;
				  if (is_array($mids)) {
					  $midata = '';
					  foreach ($mids as $mid) {
						  if ($i == $total) {
							  $midata .= $mid;
						  } else
							  $midata .= $mid . ",";
						  $i++;
					  }
				  }
				  $data['membership_id'] = $midata;
			  } else
				  $data['membership_id'] = 0;
				  
			  if ($data['contact_form'] == 1) {
				  $contactform['contact_form'] = "DEFAULT(contact_form)";
				  $db->update("pages", $contactform);
			  }
			  
			  if (!$this->pageid) {
				  $data['created'] = "NOW()";
			  }
			  
			  ($this->pageid) ? $db->update("pages", $data, "id='" . (int)$this->pageid . "'") : $db->insert("pages", $data);
			  $message = ($this->pageid) ? _PG_UPDATED : _PG_ADDED;
			  
			  ($db->affected()) ? $zazavisec->writeLog($message, "", "no", "content") . $core->msgOk($message) :  $core->msgAlert(_SYSTEM_PROCCESS);
		  } else
			  print $core->msgStatus();
	  }

	  /**
	   * Content::getPosts()
	   * 
	   * @return
	   */
	  public function getPosts()
	  {
		  global $db;
		  
		  $where = ($this->pageid) ? "WHERE page_id = '".$this->pageid."'" : null ;
		  $sql = "SELECT * FROM posts"
		  . "\n {$where}"
		  . "\n ORDER BY position";
          $row = $db->fetch_all($sql);
          
		  return ($row) ? $row : 0;
	  }

	  /**
	   * Content::getPagePost()
	   * 
	   * @return
	   */
	  public function getPagePost()
	  {
		  global $db, $core;
		  
		  $where = ($this->pageid) ? "WHERE page_id = '".$this->pageid."'" : NULL ;
		  $sql = "SELECT pt.*, pt.id as id, pg.id as pageid, pg.title".$core->dblang." as pagetitle"
		  . "\n FROM posts AS pt"
		  . "\n LEFT JOIN pages AS pg ON pg.id = pt.page_id"
		  . "\n $where"
		  . "\n ORDER BY pt.page_id, pt.position";
          $row = $db->fetch_all($sql);
          
		  return ($row) ? $row : 0;
	  }

	  /**
	   * Content::processPost()
	   * 
	   * @return
	   */
	  public function processPost()
	  {
		  global $db, $core, $zazavisec;
		  
		  if (empty($_POST['title'.$core->dblang]))
			  $core->msgs['title'] =  _PO_TITLE_R;
		  
		  if (empty($core->msgs)) {
				  $data = array(
				  'title'.$core->dblang => sanitize($_POST['title'.$core->dblang]), 
				  'page_id' => intval($_POST['page_id']), 
				  'show_title' => intval($_POST['show_title']),
				  'body'.$core->dblang => $_POST['body'.$core->dblang],
				  'active' => intval($_POST['active'])
			  );
			  
			  ($this->postid) ? $db->update("posts", $data, "id='" . (int)$this->postid . "'") : $db->insert("posts", $data);
			  $message = ($this->postid) ? _PO_UPDATED : _PO_ADDED;
			  
			  ($db->affected()) ? $zazavisec->writeLog($message, "", "no", "content") . $core->msgOk($message) : $core->msgAlert(_SYSTEM_PROCCESS);
		  } else
			  print $core->msgStatus();
	  }

	  /**
	   * Content::getPageModules()
	   * 
	   * @return
	   */
	  public function getPageModules()
	  {
		  global $db, $core, $pager;
		  
		  require_once(ZAZAVILITE . "lib/class_paginate.php");
          $pager = new Paginator();

          $counter = countEntries("modules");
          $pager->items_total = $counter;
          $pager->default_ipp = $core->perpage;
          $pager->paginate();
          
          if ($counter == 0) {
              $pager->limit = null;
          }
		  
		  $sql = "SELECT *, DATE_FORMAT(created, '" . $core->long_date . "') as date"
		  . "\n FROM modules"
		  . "\n ORDER BY system DESC, title".$core->dblang . $pager->limit;;
          $row = $db->fetch_all($sql);
          
		  return ($row) ? $row : 0;
	  }	 

	  /**
	   * Content::processModule()
	   * 
	   * @return
	   */
	  public function processModule()
	  {
		  global $db, $core, $zazavisec;
		  if (empty($_POST['title'.$core->dblang]))
			  $core->msgs['title'] = _MO_TITLE_R;
		  
		  if (empty($core->msgs)) {
			  $data = array(
				  'title'.$core->dblang => sanitize($_POST['title'.$core->dblang]), 
				  'show_title' => intval($_POST['show_title']),
				  'alt_class' => sanitize($_POST['alt_class']),
				  'body'.$core->dblang => $_POST['body'.$core->dblang],
				  'info'.$core->dblang => sanitize($_POST['info'.$core->dblang]),
				  'active' => intval($_POST['active'])
			  );
			  
			  if (!$this->modid) {
				  $data['created'] = "NOW()";
			  }
			  
			  ($this->modid) ? $db->update("modules", $data, "id='" . (int)$this->modid . "'") : $db->insert("modules", $data);
			  $message = ($this->modid) ? _MO_UPDATED : _MO_ADDED;
			  
			  ($db->affected()) ? $zazavisec->writeLog($message, "", "no", "module") . $core->msgOk($message) :  $core->msgAlert(_SYSTEM_PROCCESS);
		  } else
			  print $core->msgStatus();
	  }

      /**
       * Content::getAvailableModules()
       * 
       * @return
       */
      public function getAvailableModules()
	  {
          global $db;
		  $id = ($this->pageid) ? $this->pageid : 1;
          $sql = "SELECT * FROM modules" 
		  . "\n WHERE id NOT IN (SELECT module_id FROM layout WHERE page_id='" . $id . "') AND sidebar = '1'";
          $row = $db->fetch_all($sql);
          
		  if ($row) {
              return $row;
          } else
              return 0;
      }

      /**
       * Content::getModuleName()
       * 
       * @return
       */
      public function getModuleName($name)
	  {
          global $db;
		  $name = sanitize($name);
          $sql = "SELECT title FROM modules" 
		  . "\n WHERE modalias = '" . $db->escape($name) . "'";
          $row = $db->first($sql);
          
		  if ($row) {
              return $row['title'];
          } else
              return "NA";
      }
	    	  	  	  
      /**
       * Content::getLayoutOptions()
       * 
       * @return
       */
      public function getLayoutOptions()
      {
          global $db, $core;
		  $id = ($this->pageid) ? $this->pageid : 1;
		  
          $sql = "SELECT l.*, m.id as modid, m.title{$core->dblang}" 
		  . "\n FROM layout AS l" 
		  . "\n INNER JOIN modules AS m ON m.id = l.module_id" 
		  . "\n WHERE l.page_id = '".$id."'"
		  . "\n AND m.sidebar = '1'"
		  . "\n ORDER BY l.position ASC, m.title ASC";
          $row = $db->fetch_all($sql);
          
		  return ($row) ? $row : 0;
      }

      /**
       * Content::getModuleLayout()
       * 
       * @param mixed $place
       * @return
       */
      public function getModuleLayout($place)
      {
          global $db, $core;
		  $id = ($this->pageid) ? $this->pageid : 1;
		  
          $sql = "SELECT l.*, m.id as modid, m.title{$core->dblang}, m.body{$core->dblang}, m.modalias, m.hasconfig, m.system, m.show_title, m.alt_class" 
		  . "\n FROM layout AS l" 
		  . "\n LEFT JOIN modules AS m ON m.id = l.module_id" 
		  . "\n WHERE l.place = '".$place."'"
		  . "\n AND l.page_id = '".$id."'"
		  . "\n AND m.active = '1'"
		  . "\n AND m.sidebar = '1'"
		  . "\n ORDER BY l.position ASC";
          $row = $db->fetch_all($sql);
          
		  return ($row) ? $row : null;

      }

      /**
       * Content::getModuleAssets()
       * 
       * @return
       */
      public function getModuleAssets()
      {
          global $db;
		  $id = ($this->pageid) ? $this->pageid : 1;
		  
          $sql = "SELECT l.*,  m.modalias" 
		  . "\n FROM layout AS l" 
		  . "\n LEFT JOIN modules AS m ON m.id = l.module_id" 
		  . "\n WHERE l.page_id = '".$id."'"
		  . "\n AND m.system = '1'"
		  . "\n AND m.sidebar = '1'"
		  . "\n AND m.active = '1'";
          $result = $db->fetch_all($sql);
          
		  if ($result) {
			  foreach ($result as $row) {
			  $cssfile = MODDIR .$row['modalias']."/style.css";
			  $jsfile = MODDIR .$row['modalias']."/script.js";
			  
			  if(file_exists($cssfile))
			  	print "<link href=\"".SITEURL."/modules/".$row['modalias']."/style.css\" rel=\"stylesheet\" type=\"text/css\" />\n";

			  if(file_exists($jsfile))
			  	print "<script type=\"text/javascript\" src=\"".SITEURL."/modules/".$row['modalias']."/script.js\"></script>\n";

			  }
          } 
      }
	  	  	  	  	  	  	   	  	  
      /**
       * Content::getMenuTree()
       * 
       * @return
       */
      protected function getMenuTree()
	  {
		  global $db, $core;
		  $query = $db->query('SELECT * FROM menus ORDER BY parent_id, position');
		  
		  while ($row = $db->fetch($query)) {
			  $this->menutree[$row['id']] = array(
			        'id' => $row['id'],
					'name'.$core->dblang => $row['name'.$core->dblang], 
					'parent_id' => $row['parent_id']
			  );
		  }
		  return $this->menutree;
	  }

      /**
       * Content::getMenuList()
       * 
       * @return
       */
      public function getMenuList()
	  {
		  global $db, $core;
		  $query = $db->query("SELECT m.*, p.id as pageid, p.slug{$core->dblang} as pslug"
		  . "\n FROM menus as m" 
		  . "\n LEFT JOIN pages AS p ON p.id = m.page_id" 
		  . "\n WHERE m.active = '1'"
		  . "\n ORDER BY m.parent_id, m.position");

		  while ($row = $db->fetch($query)) {
			  $menulist[$row['id']] = array(
			        'id' => $row['id'],
					'name'.$core->dblang => $row['name'.$core->dblang], 
					'parent_id' => $row['parent_id'],
					'page_id' => $row['page_id'],
					'content_type' => $row['content_type'],
					'link' => $row['link'],
					'home_page' => $row['home_page'],
					'active' => $row['active'],
					'target' => $row['target'],
					'pslug' => $row['pslug']
			  );
		  }
		  return $menulist;
	  }
	    
      /**
       * Content::getSortMenuList()
       * 
	   * @param integer $parent_id
       * @return
       */
      public function getSortMenuList($parent_id = 0)
	  {
		  global $core;
		  
		  $submenu = false;
		  $class = ($parent_id == 0) ? "parent" : "child";

		  foreach ($this->menutree as $key => $row) {
			  if ($row['parent_id'] == $parent_id) {
				  if ($submenu === false) {
					  $submenu = true;
					  print "<ul>\n";
				  }
				  
				  print '<li id="list_' . $row['id'] . '">'
				  .'<div><a href="javascript:void(0)" id="item_'.$row['id'].'" rel="' . $row['name'.$core->dblang] . '" class="delete">'
				  .'<img src="images/delete.png" alt="" title="'._DELETE.'"/></a>'
				  .'<a href="loader.php?do=menus&amp;action=edit&amp;id=' . $row['id'] . '" class="'.$class.'">' . $row['name'.$core->dblang] . '</a></div>';
				  $this->getSortMenuList($key);
				  print "</li>\n";
			  }
		  }
		  unset($row);
		  
		  if ($submenu === true)
			  print "</ul>\n";
	  }

      /**
       * Content::getMenu()
       * 
	   * @param mixed $array
	   * @param integer $parent_id
       * @return
       */
	  public function getMenu($array, $parent_id = 0)
	  {
		  global $core;
		  $submenu = false;
		  
		  foreach ($array as $key => $row) {
			  
			  if ($row['parent_id'] == $parent_id) {
				  if ($submenu === false) {
					  $submenu = true;
					  
					  print "<ul>\n";
				  }

				  if ($core->seo == 1) {
					  $url = $core->site_url . '/' . intval($row['page_id']) . '/' . sanitize($row['pslug']) . '.html';
				  } else
					  $url = $core->site_url . '/content.php?pageid=' . intval($row['page_id']);
	  
				  $active = ($row['page_id'] == $this->pageid) ? " class=\"active\"" : "";
				  $homeact = (preg_match('/index.php/', $_SERVER['PHP_SELF'])) ? " class=\"active\"" : "";
				  
				  if ($row['home_page'] == 1) {
					  $link = '<a href="' . SITEURL . '"' . $homeact . '><span>' . $row['name'.$core->dblang] . '</span></a>';
				  } elseif (!$row['link']) {
					  $link = '<a href="'.$url.'"' . $active . '><span>' . $row['name'.$core->dblang] . '</span></a>';
				  } else {
					  $link = '<a href="' . $row['link'] . '" target="'.$row['target'].'"><span>' . $row['name'.$core->dblang] . '</span></a>';
				  }
				  
				  print '<li>';
				  print $link;
				  $this->getMenu($array, $key);
				  print "</li>\n";
			  }
		  }
		  unset($row);
		  
		  if ($submenu === true)
			  print "</ul>\n";
	  }
	  
      /**
       * Content::getMenuDropList()
       * 
	   * @param mixed $parent_id
	   * @param integer $level
	   * @param mixed $spacer
	   * @param bool $selected
       * @return
       */
	  public function getMenuDropList($parent_id, $level = 0, $spacer, $selected = false)
	  {
		  global $core;
		  foreach ($this->menutree as $key => $row) {
			  $sel = ($row['id'] == $selected) ? " selected=\"selected\"" : "" ;
			  if ($parent_id == $row['parent_id']) {
				  print "<option value=\"" . $row['id'] . "\"".$sel.">";
				  
				  for ($i = 0; $i < $level; $i++)
					  print $spacer;
				  
				  print $row['name'.$core->dblang] . "</option>\n";
				  $level++;
				  $this->getMenuDropList($key, $level, $spacer, $selected);
				  $level--;
			  }
		  }
		  unset($row);
	  }

	  /**
	   * Content::processMenu()
	   * 
	   * @return
	   */
	  public function processMenu()
	  {
		  global $db, $core, $zazavisec;
		  if (empty($_POST['name'.$core->dblang]))
			  $core->msgs['name'] = _MU_NAME_R;
		  
		  if ($_POST['content_type'] == "NA")
			  $core->msgs['content_type'] = _MU_TYPE_R;
		  
		  if (empty($core->msgs)) {
			  $data = array(
				  'name'.$core->dblang => sanitize($_POST['name'.$core->dblang]), 
				  'parent_id' => intval($_POST['parent_id']), 
				  'page_id' => intval($_POST['page_id']),
				  'slug' => paranoia($_POST['name'.$core->dblang]),
				  'content_type' => sanitize($_POST['content_type']),
				  'link' => (isset($_POST['web'])) ? sanitize($_POST['web']) : "NULL",
				  'target' => (isset($_POST['target'])) ? sanitize($_POST['target']) : "DEFAULT(target)",
				  'home_page' => intval($_POST['home_page']),
				  'active' => intval($_POST['active'])
			  );

			  if ($data['home_page'] == 1) {
				  $home['home_page'] = "DEFAULT(home_page)";
				  $db->update("menus", $home);
			  }
			  
			  ($this->id) ? $db->update("menus", $data, "id='" . (int)$this->id . "'") : $db->insert("menus", $data);
			  $message = ($this->id) ? _MU_UPDATED : _MU_ADDED;
			  
			  ($db->affected()) ? $zazavisec->writeLog($message, "", "no", "content") . $core->msgOk($message) :  $core->msgAlert(_SYSTEM_PROCCESS);
			
		  } else
			  print $core->msgStatus();
	  }

      /**
       * Content::getContentType()
       * 
	   * @param bool $selected
       * @return
       */ 	  
      public function getContentType($selected = false)
	  {
		  $arr = array(
				'page' => _CON_PAGE,
				'web' => _EXT_LINK
		  );
		  
		  $contenttype = '';
		  foreach ($arr as $key => $val) {
              if ($key == $selected) {
                  $contenttype .= "<option selected=\"selected\" value=\"" . $key . "\">" . $val . "</option>\n";
              } else
                  $contenttype .= "<option value=\"" . $key . "\">" . $val . "</option>\n";
          }
          unset($val);
          return $contenttype;
      }

	  /**
	   * Content::getHomePageMeta()
	   * 
	   * @return
	   */
		private function getHomePageMeta()
		{
			global $db, $core;
			
			$sql = "SELECT p.title{$core->dblang}, p.description{$core->dblang}, p.keywords{$core->dblang}" 
			. "\n FROM pages AS p" 
			. "\n LEFT JOIN menus AS m ON p.id = m.page_id" 
			. "\n WHERE m.home_page = '1'";
			$row = $db->first($sql);
			
			return $row;
		}
			  
      /**
       * Content::getContentType()
       * 
	   * @param bool $selected
       * @return
       */ 
	  function getMeta()
	  {
		  global $core;
		  
		  $home = $this->getHomePageMeta();
		  $meta = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
		  $meta .= "<title>" . $core->site_name . "  |  ";
		  if ($this->pageid) {
			  $meta .= $this->title;
		  } else {
			  if (isset($_GET['mode'])) {
				  $meta .= "Sitemap of " . $core->site_name;
			  } else
				  $meta .= $home['title'.$core->dblang];
		  }
		  $meta .= "</title>\n";
		  $meta .= "<meta name=\"description\" content=\"";
		  if ($this->pageid) {
			  if ($this->description) {
				  $meta .= $this->description;
			  } else
				  $meta .= $core->metadesc;
		  } else {
			  $meta .= $home['description'.$core->dblang];
		  }
		  $meta .= "\" />\n";
		  $meta .= "<meta name=\"keywords\" content=\"";
		  if ($this->pageid) {
			  if ($this->keywords) {
				  $meta .= $this->keywords;
			  } else
				  $meta .= $core->metadesc;
		  } else {
			  $meta .= $home['keywords'.$core->dblang];
		  }
		  $meta .= "\" />\n";
		  $meta .= "<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"" .SITEURL ."/assets/favicon.ico\" />\n";
		  $meta .= "<meta name=\"publisher\" content=\"" . $core->company . "\" />\n";
		  $meta .= "<meta name=\"copyright\" content=\"" . $core->company . " &copy; All Rights Reserved\" />\n";
		  $meta .= "<meta name=\"language\" content=\"English\" />\n";
		  $meta .= "<meta name=\"robots\" content=\"index\" />\n";
		  $meta .= "<meta name=\"robots\" content=\"follow\" />\n";
		  $meta .= "<meta name=\"revisit-after\" content=\"1 day\" />\n";
		  $meta .= "<meta name=\"generator\" content=\"Powered by Zazavi\" />\n";
		  return $meta;
	  }
  }
?>