Permalink
Browse files

Add image upload

  • Loading branch information...
1 parent 4fd8cda commit 07ef0451497ae38acbcc1014589a04decee92904 @sandermarechal committed Jun 9, 2011
@@ -1,6 +1,6 @@
<?php
/* SVN FILE: $Id$ */
-/* App schema generated on: 2010-12-08 23:12:48 : 1291845828*/
+/* App schema generated on: 2010-12-08 23:12:31 : 1291847611*/
class AppSchema extends CakeSchema {
var $name = 'App';
@@ -66,6 +66,15 @@ function after($event = array()) {
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'group_id' => array('column' => 'group_id', 'unique' => 0), 'user_id' => array('column' => 'user_id', 'unique' => 0)),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM')
);
+ var $images = array(
+ 'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'),
+ 'user_id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'index'),
+ 'filename' => array('type' => 'string', 'null' => false, 'default' => NULL),
+ 'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
+ 'updated' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
+ 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'user_id' => array('column' => 'user_id', 'unique' => 0)),
+ 'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
+ );
var $permissions = array(
'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'),
'group_id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'index'),
@@ -0,0 +1,87 @@
+<?php
+/**
+ * Full Metal Jewelry website
+ * Copyright (C) 2009 Stichting Lone Wolves
+ * Written by Sander Marechal <s.marechal@jejik.com>
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ */
+
+/**
+ * The Images controller
+ */
+class ImagesController extends AppController {
+
+ /** @var array The view helpers */
+ public $helpers = array('Header', 'Button');
+
+ /** @var array The components for this controller */
+ public $components = array('Auth');
+
+ /**
+ * Nothing to see here, move along...
+ *
+ * @return void
+ */
+ public function index()
+ {
+ $this->redirect('/');
+ }
+
+ public function admin_index()
+ {
+ $this->pagination = array(
+ 'conditions' => array('Image.user_id' => $this->Auth->user('id')),
+ 'order' => array('Image.filename' => 'ASC'),
+ );
+
+ $this->Image->recursive = 0;
+ $images = $this->paginate();
+ foreach ($images as &$image) {
+ $this->Image->id = $image['Image']['id'];
+ $this->Image->thumb(170);
+ $image['Image']['thumb'] = '/img/products/' . $this->Image->getPath(170);
+ }
+
+ $this->set(compact('images'));
+ }
+
+ public function admin_delete($id = null)
+ {
+ if (!$id) {
+ $this->Session->setFlash(__('Invalid id for image', true));
+ $this->redirect(array('action'=>'index'));
+ }
+ if ($this->Image->delete($id)) {
+ $this->Session->setFlash(__('Image deleted', true));
+ $this->redirect(array('action'=>'index'));
+ }
+ $this->Session->setFlash(__('Image was not deleted', true));
+ $this->redirect(array('action' => 'index'));
+ }
+
+ /**
+ * Upload an image
+ */
+ function admin_upload()
+ {
+ if (empty($this->data)) {
+ $this->redirect(array('action' => 'admin_index'));
+ }
+
+ $this->Image->create();
+ $this->Image->save(array('user_id' => $this->Auth->user('id')));
+
+ $errors = array();
+ if (!$this->Image->upload($this->data['image'], $errors)) {
+ $this->Session->setFlash(implode('<br />', $errors));
+ $this->Image->delete();
+ }
+
+ $this->Session->setFlash('Image uploaded succesfully');
+ $this->redirect(array('action' => 'admin_index'));
+ }
+}
+
+?>
@@ -14,10 +14,13 @@
class ProductsController extends AppController
{
/** @var array The view helpers */
- var $helpers = array('Html', 'Form', 'Header', 'Button', 'Javascript');
+ public $helpers = array('Html', 'Form', 'Header', 'Button', 'Javascript');
/** @var array The components for this controller */
- var $components = array('Auth');
+ public $components = array('Auth');
+
+ /* @var array Models to use */
+ public $uses = array('ImagesProduct', 'Product');
/**
* Set the auth permissions for this controller
@@ -60,22 +63,29 @@ public function admin_view($id = null)
$this->redirect(array('action'=>'index'));
}
- $this->Product->contain(array('Category', 'User'));
- $product = $this->Product->read(null, $id);
- $this->set('product', $product);
- }
-
- private function _getImages()
- {
- //TODO: Fix this when photo uploading has been ported
- return array();
+ $this->Product->contain(array(
+ 'Category',
+ 'Image' => array('order' => 'ImagesProduct.order ASC'),
+ 'User'
+ ));
- $images = glob(WWW_ROOT . 'img/products/*.*');
- foreach ($images as &$image) {
- $image = basename($image);
- }
-
- return array_combine($images, $images);
+ $product = $this->Product->read(null, $id);
+ foreach ($product['Image'] as &$image) {
+ $this->Product->Image->id = $image['id'];
+ $this->Product->Image->thumb(170);
+ $image['thumb'] = '/img/products/' . $this->Product->Image->getPath(170);
+ }
+
+ $ids = Set::extract('/Image/id', $product);
+ $images = $this->Product->Image->find('list', array(
+ 'conditions' => array(
+ 'user_id' => $this->Auth->user('id'),
+ 'not' => array('id' => $ids),
+ ),
+ 'recursive' => -1,
+ ));
+
+ $this->set(compact('product', 'images'));
}
public function admin_add()
@@ -92,7 +102,6 @@ public function admin_add()
}
$categories = $this->Product->Category->find('list');
- $images = $this->_getImages();
$this->set(compact('categories', 'images'));
$this->render('admin_edit');
@@ -136,7 +145,6 @@ public function admin_edit($id = null)
}
$categories = $this->Product->Category->find('list');
- $images = $this->_getImages();
$this->set(compact('categories','images'));
}
@@ -157,6 +165,62 @@ public function admin_delete($id = null)
$this->redirect(array('action'=>'index'));
}
}
+
+ public function admin_attach()
+ {
+ if (empty($this->data)) {
+ $this->redirect(array('action'=>'index'));
+ }
+
+ $data = $this->data['ImagesProduct'];
+ if (!$this->_checkAccess($data['product_id'])) {
+ $this->Session->setFlash(__('You are not allowed to edit that product', true));
+ $this->redirect(array('action' => 'view', $data['product_id']));
+ }
+
+ $this->ImagesProduct->create();
+ $this->ImagesProduct->save($data);
+
+ $this->Session->setFlash(__('The image has been added', true));
+ $this->redirect(array('action' => 'view', $data['product_id']));
+ }
+
+ public function admin_detach($id)
+ {
+ $link = $this->ImagesProduct->read(null, $id);
+ if (!$this->_checkAccess($link['ImagesProduct']['product_id'])) {
+ $this->Session->setFlash(__('You are not allowed to edit that product', true));
+ } else {
+ $this->Session->setFlash(__('The image has been removed from this product', true));
+ $this->ImagesProduct->delete($id);
+ }
+
+ $this->redirect(array('action' => 'view', $link['ImagesProduct']['product_id']));
+ }
+
+ public function admin_moveup($id)
+ {
+ $link = $this->ImagesProduct->read(null, $id);
+ if (!$this->_checkAccess($link['ImagesProduct']['product_id'])) {
+ $this->Session->setFlash(__('You are not allowed to edit that product', true));
+ } else {
+ $this->ImagesProduct->moveup($id);
+ }
+
+ $this->redirect(array('action' => 'view', $link['ImagesProduct']['product_id']));
+ }
+
+ public function admin_movedown($id)
+ {
+ $link = $this->ImagesProduct->read(null, $id);
+ if (!$this->_checkAccess($link['ImagesProduct']['product_id'])) {
+ $this->Session->setFlash(__('You are not allowed to edit that product', true));
+ } else {
+ $this->ImagesProduct->movedown($id);
+ }
+
+ $this->redirect(array('action' => 'view', $link['ImagesProduct']['product_id']));
+ }
}
?>
Oops, something went wrong.

0 comments on commit 07ef045

Please sign in to comment.