<?php

/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 */
class Bip_Controller_Admin_MenuController extends Eppe_Backend_Controller_Action {

	public function indexAction() {
		
	}

	public function ajaxLoadCategoriesAction() {
		header('Content-type: application/json');
		$response = array();
		foreach (R::findAll('category', 'WHERE true ORDER BY `order` ASC') as $category) {

			$categoryObj = $this->_buildSubCategoryObj($category);

			if ($category->category_id) {
				$this->_attachSubCategory($categoryObj, $response);
			} else {
				$response[$category->id - 1] = $categoryObj;
			}
		}

		//var_dump($response);
		echo Zend_Json::encode($response);
		die;
	}

	protected function _buildSubCategoryObj($category) {
		$a_attr = new stdClass();
		$a_attr->class = $category->active ? 'active' : 'inactive';

		$categoryObj = new stdClass();
		$categoryObj->bean_id = $category->id;
		$categoryObj->text = $category->name;
		$categoryObj->id = $category->category_id ? 'category_' . $category->id : 'root_' . $category->id;
		$categoryObj->type = $category->category_id ? 'category' : 'root';
		$categoryObj->data = array('id' => $category->id);
		$categoryObj->category_id = $category->category_id;
		$categoryObj->a_attr = $a_attr;
		$categoryObj->children = array();

		return $categoryObj;
	}

	protected function _attachSubCategory($categoryObj, &$response) {
		foreach ($response as $id => $rootCat) {
			if ($id == $categoryObj->category_id - 1) {
				$response[$id]->children[] = $categoryObj;
			} else {
				foreach ($rootCat->children as $key => $subCat) {
					if ($subCat->bean_id == $categoryObj->category_id) {
						$response[$id]->children[$key]->children[] = $categoryObj;
					} else {
						foreach ($subCat->children as $k => $subCat2) {
							if ($subCat2->bean_id == $categoryObj->category_id) {
								$response[$id]->children[$key]->children[$k]->children[] = $categoryObj;
							} else {
								foreach ($subCat2->children as $j => $subCat3){
									if ($subCat3->bean_id == $categoryObj->category_id) {
										$response[$id]->children[$key]->children[$k]->children[$j]->children[] = $categoryObj;
									}
								}
							}
						}
					}
				}
			}
		}
	}

	public function ajaxSaveTreeAction() {
		if (NULL !== $tree = $this->getParam('tree')) {
			$tree = Zend_Json::decode($tree);
			$affectedBeans = array();
			$level = 0;

			$this->_iterateTree($tree, $affectedBeans, $level);
			R::exec('DELETE FROM category WHERE id NOT IN (' . join(',', $affectedBeans) . ')');

			file_put_contents(Bip_Module_Module::getInstance()->getResourcesPath() . 'categories', sha1(microtime(true)));
			echo Zend_Json::encode(array('success' => true));
		}
		die;
	}

	protected function _iterateTree($tree, &$affectedBeans, $level, $parentBranch = NULL) {

		foreach ($tree as $branch) {
			$categoryBean = $this->_loadBranchBean($branch);
			$affectedBeans[] = $categoryBean->id;

			$categoryBean->name = $branch['text'];
			$categoryBean->level = $level;
			$categoryBean->order = count($affectedBeans);
			if (isset($branch['a_attr']['class'])) {
				$categoryBean->active = $branch['a_attr']['class'] == 'inactive' ? 0 : 1;
			} else {
				$categoryBean->active = 1;
			}

			if ($parentBranch) {
				$parentBranch->ownCategory[] = $categoryBean;
			}

			if (count($branch['children'])) {
				$this->_iterateTree($branch['children'], $affectedBeans, $level + 1, $categoryBean);
			}

			R::store($categoryBean);
		}
	}

	protected function _loadBranchBean($branch) {
		if (is_array($branch['data'])) {
			if (array_key_exists('id', $branch['data'])) {
				$categoryBean = R::load('category', $branch['data']['id']);
			}
		} else {
			$categoryBean = R::dispense('category');
			R::store($categoryBean);
		}

		return $categoryBean;
	}

}
