Subversion Repositories SmartDukaan

Rev

Rev 13633 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13579 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * CategoryDiscounts Controller
5
 *
6
 * @property CategoryDiscount $CategoryDiscount
7
 * @property PaginatorComponent $Paginator
8
 */
9
class CategoryDiscountsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
18
/**
19
 * index method
20
 *
21
 * @return void
22
 */
23
	public function index() {
24
		$this->CategoryDiscount->recursive = 0;
25
		$this->set('categoryDiscounts', $this->Paginator->paginate());
26
	}
27
 
28
/**
29
 * view method
30
 *
31
 * @throws NotFoundException
32
 * @param string $id
33
 * @return void
34
 */
35
	public function view($id = null) {
36
		if (!$this->CategoryDiscount->exists($id)) {
37
			throw new NotFoundException(__('Invalid category discount'));
38
		}
39
		$options = array('conditions' => array('CategoryDiscount.' . $this->CategoryDiscount->primaryKey => $id));
40
		$this->set('categoryDiscount', $this->CategoryDiscount->find('first', $options));
41
	}
42
 
43
/**
44
 * add method
45
 *
46
 * @return void
47
 */
48
	public function add() {
49
		if ($this->request->is('post')) {
50
			$this->CategoryDiscount->create();
51
			if ($this->CategoryDiscount->save($this->request->data)) {
52
				$this->Session->setFlash(__('The category discount has been saved.'));
53
				return $this->redirect(array('action' => 'index'));
54
			} else {
55
				$this->Session->setFlash(__('The category discount could not be saved. Please, try again.'));
56
			}
57
		}
58
		$categories = $this->CategoryDiscount->Category->find('list');
59
		$this->set(compact('categories'));
60
	}
61
 
62
/**
63
 * edit method
64
 *
65
 * @throws NotFoundException
66
 * @param string $id
67
 * @return void
68
 */
69
	public function edit($id = null) {
70
		if (!$this->CategoryDiscount->exists($id)) {
71
			throw new NotFoundException(__('Invalid category discount'));
72
		}
73
		if ($this->request->is(array('post', 'put'))) {
74
			if ($this->CategoryDiscount->save($this->request->data)) {
75
				$this->Session->setFlash(__('The category discount has been saved.'));
76
				return $this->redirect(array('action' => 'index'));
77
			} else {
78
				$this->Session->setFlash(__('The category discount could not be saved. Please, try again.'));
79
			}
80
		} else {
81
			$options = array('conditions' => array('CategoryDiscount.' . $this->CategoryDiscount->primaryKey => $id));
82
			$this->request->data = $this->CategoryDiscount->find('first', $options);
83
		}
84
		$categories = $this->CategoryDiscount->Category->find('list');
85
		$this->set(compact('categories'));
86
	}
87
 
88
/**
89
 * delete method
90
 *
91
 * @throws NotFoundException
92
 * @param string $id
93
 * @return void
94
 */
95
	public function delete($id = null) {
96
		$this->CategoryDiscount->id = $id;
97
		if (!$this->CategoryDiscount->exists()) {
98
			throw new NotFoundException(__('Invalid category discount'));
99
		}
100
		$this->request->onlyAllow('post', 'delete');
101
		if ($this->CategoryDiscount->delete()) {
102
			$this->Session->setFlash(__('The category discount has been deleted.'));
103
		} else {
104
			$this->Session->setFlash(__('The category discount could not be deleted. Please, try again.'));
105
		}
106
		return $this->redirect(array('action' => 'index'));
107
	}
108
 
109
/**
110
 * admin_index method
111
 *
112
 * @return void
113
 */
114
	public function admin_index() {
115
		$this->CategoryDiscount->recursive = 0;
116
		$this->set('categoryDiscounts', $this->Paginator->paginate());
117
	}
118
 
119
/**
120
 * admin_view method
121
 *
122
 * @throws NotFoundException
123
 * @param string $id
124
 * @return void
125
 */
126
	public function admin_view($id = null) {
127
		if (!$this->CategoryDiscount->exists($id)) {
128
			throw new NotFoundException(__('Invalid category discount'));
129
		}
130
		$options = array('conditions' => array('CategoryDiscount.' . $this->CategoryDiscount->primaryKey => $id));
131
		$this->set('categoryDiscount', $this->CategoryDiscount->find('first', $options));
132
	}
133
 
134
/**
135
 * admin_add method
136
 *
137
 * @return void
138
 */
139
	public function admin_add() {		
140
		if ($this->request->is('post')) {
141
			$conditions = array('category_id'=>$this->request->data['CategoryDiscount']['category_id'],'brand'=>$this->request->data['CategoryDiscount']['brand']);
142
			$count = $this->CategoryDiscount->find('count',array('conditions'=>$conditions,'recursive'=>-1));
143
			if($count > 0){
144
				$this->Session->setFlash(__('Duplicate category and brand combination.'));
145
			}else{
146
				$this->CategoryDiscount->create();
147
				if ($this->CategoryDiscount->save($this->request->data)) {
148
					$this->Session->setFlash(__('The category discount has been saved.'));
149
					return $this->redirect(array('action' => 'index'));
150
				} else {
151
					$this->Session->setFlash(__('The category discount could not be saved. Please, try again.'));
152
				}
153
			}
154
		}
155
		$this->loadModel('Brand');
156
		$allbrands = $this->Brand->find('list');
157
		foreach ($allbrands as $key => $value) {
158
			$brands[$value] = $value;
159
		}
160
		$categories = $this->CategoryDiscount->Category->find('list',array('conditions'=>array('parent_id !='=>0)));
161
		$this->set(compact('categories','brands'));
162
	}
163
 
164
/**
165
 * admin_edit method
166
 *
167
 * @throws NotFoundException
168
 * @param string $id
169
 * @return void
170
 */
171
	public function admin_edit($id = null) {
172
		if (!$this->CategoryDiscount->exists($id)) {
173
			throw new NotFoundException(__('Invalid category discount'));
174
		}
175
		if ($this->request->is(array('post', 'put'))) {
176
			if ($this->CategoryDiscount->save($this->request->data)) {
177
				$this->Session->setFlash(__('The category discount has been saved.'));
178
				return $this->redirect(array('action' => 'index'));
179
			} else {
180
				$this->Session->setFlash(__('The category discount could not be saved. Please, try again.'));
181
			}
182
		} else {
183
			$options = array('conditions' => array('CategoryDiscount.' . $this->CategoryDiscount->primaryKey => $id));
184
			$this->request->data = $this->CategoryDiscount->find('first', $options);
185
		}
186
		$categories = $this->CategoryDiscount->Category->find('list');
187
		$this->set(compact('categories'));
188
	}
189
 
190
/**
191
 * admin_delete method
192
 *
193
 * @throws NotFoundException
194
 * @param string $id
195
 * @return void
196
 */
197
	public function admin_delete($id = null) {
198
		$this->CategoryDiscount->id = $id;
199
		if (!$this->CategoryDiscount->exists()) {
200
			throw new NotFoundException(__('Invalid category discount'));
201
		}
202
		$this->request->onlyAllow('post', 'delete');
203
		if ($this->CategoryDiscount->delete()) {
204
			$this->Session->setFlash(__('The category discount has been deleted.'));
205
		} else {
206
			$this->Session->setFlash(__('The category discount could not be deleted. Please, try again.'));
207
		}
208
		return $this->redirect(array('action' => 'index'));
209
	}}