<?php

class links_admin_categories
{

	var $Links;
	var $mydirname;
	var $controller_name;
	var $constprefMI;

	var $image_path;

	/** Constructor
	 *
	**/
	function __construct($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = new links_Base( $mydirname ) ; // GIJ
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();
		$this->constprefMI = '_MI_'. $this->mydirname;

	}

	function index($edit_cid = 0)
	{
	
		//edit category
		$editcat = NULL;
		if (!empty($edit_cid)) {
			$editcat = $this->Links->Categories->getCategory($edit_cid);
		}
		$selected_id = isset($editcat['pid']) ? $editcat['pid']: 0;
		$categories = $this->Links->Categories->getChildTreeArray(0, 'corder,cid');
		foreach ($categories as $key => $cat) {
			$categories[$key]['prefix']  = str_replace('.', '&nbsp;--', substr($cat['prefix'], 1));
			$categories[$key]['imgurl']  = links_urlCheckReplace($cat['imgurl']);
			$categories[$key]['itemnum'] = $this->Links->Categories->getItemsNum($cat['cid'], 1);//���F�ς݂��J�E���g
			$categories[$key]['title']   = htmlspecialchars($cat['title'], ENT_QUOTES, _CHARSET);
		}
		
		$data = array(
			'categories'  => $categories,
			'editcat'     => $editcat,
			'mod_name'    => htmlspecialchars($this->Links->module_name, ENT_QUOTES, _CHARSET),
			'menu_name'   => htmlspecialchars(constant($this->constprefMI . '_ADMENU1'), ENT_QUOTES, _CHARSET),
			'myurl'       => $this->Links->myurl,
			'pidselector' => $this->Links->Categories->makeMySelBox('corder,cid', $selected_id, TRUE, 'pid', '', 'pid', 0),
			'imgfiles'    => $this->_get_category_icons(),
			'imgpath'     => htmlspecialchars($this->image_path, ENT_QUOTES, _CHARSET) ,
			'gticket'     => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_admin_categories'),
		);

		$template = $this->mydirname ."_admin_categories.html";
		$this->Links->adminView($template, $data);
	}

	function post()
	{
		if (!empty($_POST['title'])) {
			$this->Links->basicPostCheck('links_admin_categories', FALSE);

			if (isset($_POST['cid']) && $_POST['cid'] > 0) {
				if ($_POST['cid'] == $_POST['pid']) {
					$msg = "<span style='color:red;'>". _MD_CID_EQUAL_PID ."</span>" ;//TODO
				} else {
					if ($this->Links->Categories->update(intval($_POST['cid']))) {
						$msg = _MD_A_LINKS_CAT_INSERTED ;
					} else {
						$msg = "<span style='color:red;'>". _MD_A_LINKS_CAT_INSERT_ERR ."</span>" ;//TODO
					}
				}
			} else {
				if ($this->Links->Categories->insert()) {
					$msg = _MD_A_LINKS_CAT_INSERTED ;
				} else {
					$msg = "<span style='color:red;'>". _MD_A_LINKS_CAT_INSERT_ERR ."</span>" ;//TODO
				}
			}
		} else {
			$msg = _MD_A_LINKS_CAT_NO_TITLE;
		}
		$this->Links->linksRedirectHeader($this->Links->myurl ."/admin/index.php?c=categories", 2, $msg);
	}

	function delete()
	{
		$this->Links->basicPostCheck('links_admin_categories');

		$cid = isset($_POST['cid']) ? intval($_POST['cid']): 0;
		if ($cid > 0) {
			//CHECK links in cid
			if ($this->_check_links_in_cid($cid) === FALSE) {
				$this->Links->linksRedirectHeader($this->Links->myurl ."/admin/index.php?c=categories&m=check&cid={$cid}", 6, _MD_A_LINKS_CAT_ONLY_CID_LINK_EXISTS);
				exit;
			} else {
				if ($this->Links->Categories->delete($cid)) {
					$msg = _MD_A_LINKS_CAT_DELETED ;
				} else {
					$msg = "<span style='color:red;'>". _MD_A_LINKS_CAT_DELETED_ERR ."</span>" ;//TODO
				}
				$this->Links->linksRedirectHeader($this->Links->myurl ."/admin/index.php?c=categories", 2, $msg);
				exit;
			}
		} else {
			$this->Links->linksRedirectHeader($this->Links->myurl ."/admin/index.php?c=categories", 2, _MD_NO_CATEGORY);
			exit;
		}
	}

	function _check_links_in_cid($cid)
	{
		$cids = $this->Links->Categories->getAllChildId($cid);
		$cids[] = $cid;

		$result = TRUE;
		foreach ($cids as $c) {
			$this->Links->clearWhere();
			$this->Links->setWhere('cid', "%[$c]%", 'LIKE');
			$datas = $this->Links->getDatas();
			foreach ($datas as $d) {
				if (count($d['cid']) == 1) {
				//	$this->Links->linksRedirectHeader($this->Links->myurl ."/admin/index.php?c=categories&m=check&cid={$cid}", 6, _MD_A_LINKS_CAT_ONLY_CID_LINK_EXISTS);
				//	exit;
					$result = FALSE;
					break 2;
				}
			}
		}

		return $result;
	}

	function check($cid = 0)
	{
		if ($cid == 0) {
			$this->Links->linksRedirectHeader($this->Links->myurl ."/admin/index.php?c=categories", 2, _MD_NO_CATEGORY);
			exit;
		}

		$children = $this->Links->Categories->getAllChildId($cid);
		$cids = $children;
		$cids[] = $cid;

		$check_data = array();
		$count = 0;
		foreach ($cids as $c) {
			$this->Links->clearWhere();
			$this->Links->setWhere('cid', "%[$c]%", 'LIKE');
			$datas = $this->Links->getDatas();
			foreach ($datas as $d) {
				if (count($d['cid']) == 1) {
					$check_data[$count] = $d;
					$count++;
					if ($count >= 50) break 2;
				}
			}
		}

		$data = array(
			'datas'        => $check_data,
			'mod_name'     => htmlspecialchars($this->Links->module_name, ENT_QUOTES, _CHARSET),
			'menu_name'    => htmlspecialchars(constant($this->constprefMI .'_ADMENU1'), ENT_QUOTES, _CHARSET),
			'del_cid'      => $cid,
			'del_children' => $children,
		);

		$template = $this->mydirname . '_admin_categories_check.html';
		$this->Links->adminView($template, $data);
	}

	/** category icon
	 *
	**/
	function _get_category_icons()
	{
//		require_once XOOPS_ROOT_PATH.'/class/xoopslists.php';

		if (!empty($this->Links->module_config['categoryicon_path'])) {
			$imagepath = links_urlCheckReplace($this->Links->module_config['categoryicon_path']);
			$imagepath = preg_replace(array("|^(.+)/$|"), array("$1"), $imagepath);
			$images_array = array();
			if (is_dir(XOOPS_ROOT_PATH ."/". $imagepath)) {
				$this->image_path = $imagepath;
				$images_array = icms_core_Filesystem::getFileList(XOOPS_ROOT_PATH ."/". $this->image_path);
			}
			foreach($images_array as $img){
				$images[] = htmlspecialchars(links_urlCheckReplace($img), ENT_QUOTES, _CHARSET);
			}
		}

		return $images;
	}


}
