<?php
class ArticleGroupsController extends AppController {

	var $name = 'ArticleGroups';
	var $uses = array('ArticleGroup');
	var $components = array('RequestHandler');

	function  beforeFilter() {
		
		parent::beforeFilter();
		
		$l_tip = $this->get_item('select value from settings where name = "tipprog" ');
		
		if($l_tip == 'u'){
		}
		if($l_tip == 'm'){
			$this->Auth->allow('index', 'add', 'edit', 'delete', 'view');
		}
	}
	
	function index() {
		$this->ArticleGroup->recursive = 0;
		$this->set('items', $this->paginate());
	}
	
	function view($id = null) {
		if (!$id  || !($item = $this->ArticleGroup->read(null, $id))) {
			$this->Session->setFlash(__('Invalid ArticleGroup', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('item', $item);
	}
	
	function add() {
		if (!empty($this->data)) {
			$this->ArticleGroup->create();
			if ($this->ArticleGroup->save($this->data)) {
				$this->Session->setFlash(__('The Article group has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Article group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
	}
	
	function edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->ArticleGroup->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid ArticleGroup', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->ArticleGroup->data = null;	// unset model data so modified field can be updated automagically
			if ($this->ArticleGroup->save($this->data)) {
				$this->Session->setFlash(__('The Article group has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Article group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
	}
	
	function delete($id = null) {
		if (!$id || !$this->ArticleGroup->read(null, $id)) {
			$this->Session->setFlash(__('Invalid ArticleGroup', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'));
		}
		
		$x = $this->get_item('select id from articles where article_group_id = '.intval($id));
		if($x > 0){
			$this->Session->setFlash(__('ArticleGroup delete failed, because it is used in articles.', true), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		
		if ($this->ArticleGroup->delete($id)) {
			$this->Session->setFlash(__('ArticleGroup deleted', true), 'default', array('class' => 'success'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('ArticleGroup delete failed. Please, try again.', true), 'default', array('class' => 'error'));
		$this->redirect(array('action' => 'index'));
	}
	
}
?>