<?php

if (!class_exists("wpdreamsPageParents")) {
	/**
	 * Class wpdreamsPageParents
	 *
	 * Displays the pages that have children.
	 *
	 * @package  WPDreams/OptionsFramework/Classes
	 * @category Class
	 * @author Ernest Marcinko <ernest.marcinko@wp-dreams.com>
	 * @link http://wp-dreams.com, http://codecanyon.net/user/anago/portfolio
	 * @copyright Copyright (c) 2014, Ernest Marcinko
	 */
	class wpdreamsPageParents extends wpdreamsType {
		public function getType() {
			parent::getType();
			$this->processData();
			$this->types = $this->get_pages_witch_children_list();
			echo "
      <div class='wpdreamsPageParents' id='wpdreamsPageParents-" . self::$_instancenumber . "'>
        <fieldset>
          <legend>" . $this->label . "</legend>";
			echo '<div class="sortablecontainer" id="sortablecontainer' . self::$_instancenumber . '">
            <div class="arrow-all-left"></div>
            <div class="arrow-all-right"></div>
            <p>Available parent pages</p><ul id="sortable' . self::$_instancenumber . '" class="connectedSortable">';
			if ($this->types != null && is_array($this->types)) {
				foreach ($this->types as $k => $v) {
					if ($this->selected == null || !in_array($k, $this->selected)) {
						echo '<li class="ui-state-default" bid="'.$k.'">' . $v . '</li>';
					}
				}
			}
			echo "</ul></div>";
			echo '<div class="sortablecontainer"><p>&nbsp;</p><ul id="sortable_conn' . self::$_instancenumber . '" class="connectedSortable">';
			if ($this->selected != null && is_array($this->selected)) {
				foreach ($this->selected as $k) {
					echo '<li class="ui-state-default" bid="'.$k.'">' . $this->types[$k] . '</li>';
				}
			}
			echo "</ul></div>";
			echo "
         <input isparam=1 type='hidden' value='" . $this->data . "' name='" . $this->name . "'>";
			echo "
         <input type='hidden' value='wpdreamsPageParents' name='classname-" . $this->name . "'>";
			echo "
        </fieldset>
      </div>";
		}

		public function processData() {
			$this->data = str_replace("\n", "", $this->data);
			if ($this->data != "")
				$this->selected = explode("|", $this->data);
			else
				$this->selected = null;
			//$this->css = "border-radius:".$this->topleft."px ".$this->topright."px ".$this->bottomright."px ".$this->bottomleft."px;";
		}

		public final function getData() {
			return $this->data;
		}

		public final function getSelected() {
			return $this->selected;
		}

		public final function get_pages_witch_children_list() {
			global $wpdb;
			$ret = array();
			$types = get_pages(array(
				"parent" => 0,
				"number" => 100,
				"post_status" => 'publish,private'
			));
			if (!is_wp_error($types) && is_array($types)) {
				foreach ($types as $k => $v) {
					$ret[$v->ID] = $v->post_title;
				}
			}
			return $ret;
		}
	}
}