<?php
class StocksController extends AppController {

	var $name = 'Stocks';
	var $components = array('Session','SlideHandler','ImageHandler');
	var $uses=array('Stock','Slide');
	var $helpers = array('Form', 'Html', 'Text','Javascript', 'Time', 'Number', 'Session', 'Paginator', 'Ajax', 'Cache');

	
	
	function add() {
		if (!empty($this->data)) {
		
			$this->data['Stock']['date'] = date('Y-m-d');
		if($this->data['Stock']['image']['size'] != 0){
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($this->data['Stock']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$this->data['Stock']['image'] = $image;
		}
		//print_r($this->data);exit;
			if ($this->Stock->save($this->data)) {
				$this->Session->setFlash('Saved Successfully.');
				$this->redirect(array('action' => 'published'));
			}
		}
	}
	
		
	
	function delete($id) {
		$this->set('menuTab', 'Welcome Messages');
	if ($this->Stock->delete($id)) {
		$this->Session->setFlash('Deleted Succesfully.');
		$this->redirect($this->referer());
								  }
	}
	
	function edit($id = null) {
	$this->Stock->id = $id;
	if (empty($this->data)) {
		$this->data = $this->Stock->read();
	} else {
	$this->data['Stock']['date'] = date('Y-m-d');
	if($this->data['Stock']['image']['size'] != 0){
			$image_path = $this->ImageHandler->upload_image_and_thumbnail($this->data['Stock']['image']);
			$image = $image_path['dir'].$image_path['img_folder'].$image_path['img_file'].".". $image_path['type'];
			$this->data['Stock']['image'] = $image;
		}
		else{
		$img=$this->Stock->find('all',array('conditions'=>array('id'=>$this->data['Stock']['id']),'fields'=>array('image')));
		$this->data['Stock']['image'] = $img[0]['Stock']['image'];
		}
		if ($this->Stock->save($this->data)) {
			$this->Session->setFlash('Updated Successfully.');
			$this->redirect(array('action' => 'published'));
			}
		}
	}
	
	function published() {
		$this->paginate['limit'] = 50;
		$this->paginate['conditions'] = array('status' => 1);
		$this->paginate['order'] = array('id' => 'desc');
		$this->set('listitems', $this->paginate('Stock'));	
	}
	
	function unpublished() {
		$this->paginate['limit'] = 50;
		$this->paginate['conditions'] = array('status' => 0);
		$this->paginate['order'] = array('id' => 'desc');
		$this->set('listitems', $this->paginate('Stock'));	
	}
	
	function publish($id){
		if($id){
		  $this->Stock->id = $id;
		  $this->Stock->saveField('status', 1);
		  $this->redirect($this->referer());
		    }
		else {
		$this->Session->setFlash('Invalid Operation.');
		$this->redirect($this->referer());	
			}
		
				
	}
	
	function unpublish($id){
		$this->set('menuTab', 'Welcome Messages');
		if($id){
		  $this->Stock->id = $id;
		  $this->Stock->saveField('status', 0);
		  $this->redirect($this->referer());
		    }
		else {
		$this->Session->setFlash('Invalid Operation.');
		$this->redirect($this->referer());	
			}
		
				
	}
	

}
?>
