cmc-sales/controllers/industries_controller.php
2009-07-09 17:09:59 +10:00

103 lines
2.7 KiB
PHP

<?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));
}
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) {
$this->Industry->create();
$this->data['Industry']['parent_id'] = $newid;
$this->data['Industry']['name'] = $new_industry['Industry']['name']." - General";
$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'));
}
}
}
?>