<?php
class IndustriesController extends AppController {

	var $name = 'Industries';
	var $helpers = array('Html', 'Form');

	var $paginate = array(
			'Industry' => array('order' => array('ParentIndustry.name' => 'asc', 'Industry.name' => 'asc'),
								'limit' => 200
								));
			
	function index() {
		$this->Industry->recursive = 0;
		$industries = $this->Industry->find('all', array('order'=>'Industry.name ASC'));
		$this->set('industries', $this->paginate());
		

		
	foreach ($industries as $industry) {
		if(!$industry['Industry']['parent_id']) {
			$parents[] = $industry['Industry'];
		}
	}
		
	$this->set('parents', $parents);

	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Industry.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('industry', $this->Industry->read(null, $id));
		$subCustomers = $this->Industry->findAllByParentId($id);
		$this->set('sub_customers', $subCustomers);

		$catCust = "";
		foreach ($subCustomers as $indust) {
			foreach ($indust['Customer'] as $subcust) {
				$myid = $subcust['id'];
				$catCust[$myid] = $subcust;
			}

		}
		$this->set('category_customers', $catCust);


	}

	function add() {
		if (!empty($this->data)) {
			
			if ($this->Industry->save($this->data)) {

				$newid = $this->Industry->id;
				$new_industry = $this->Industry->findById($newid);

				if($new_industry['Industry']['parent_id'] == 0) {
					
					$newsub = $this->data['Industry']['sub_category'];
					$this->Industry->create();
					$this->data['Industry']['parent_id'] = $newid;
					$this->data['Industry']['name'] = $newsub;
					$this->Industry->save($this->data);
				}
					$this->Session->setFlash(__('The Industry has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Industry could not be saved. Please, try again.', true));
			}
		}

			$this->Industry->create();	
			if(isset($this->params['named']['parentid'])) {
				$parent = $this->Industry->find('first', array('conditions' => array('Industry.id' => $this->params['named']['parentid']), 
																		'fields' => 'Industry.id, Industry.name', 'recursive' => 0));	
				$this->set('parent', $parent);
			}
			
			

		
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Industry', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Industry->save($this->data)) {
				$this->Session->setFlash(__('The Industry has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Industry could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Industry->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Industry', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Industry->del($id)) {
			$this->Session->setFlash(__('Industry deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}


}
?>
