Skip to content

Commit

Permalink
Work on templates
Browse files Browse the repository at this point in the history
  • Loading branch information
masom committed May 10, 2011
1 parent e85a19b commit d54399a
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
72 changes: 72 additions & 0 deletions controllers/OfferTemplates.php
@@ -0,0 +1,72 @@
<?php
/**
* Chowly Pick. Eat. Save!
* @copyright Copyright 2011, Martin Samson
* @copyright Copyright 2011, Chowly Corporation
*/

namespace chowly\controllers;

use chowly\models\OfferTemplates;
use li3_flash_message\extensions\storage\FlashMessage;

class OfferTemplatesController extends \chowly\extensions\action\Controller{
public function admin_index(){
$limit = 20;
$page = $this->request->page ?: 1;
$order = array('created' => 'DESC');

$total = Offers::count();
$offers = Offers::all(compact('order','limit','page'));

return compact('offers', 'total', 'page', 'limit');
}

public function admin_view(){
if (!$this->request->id){
FlashMessage::set("Missing data.");
return $this->redirect(array("OfferTemplates::index"));
}

$conditions['_id'] = $this->request->id;

$template = OfferTemplates::first(compact('conditions'));
if (!$template){
FlashMessage::set("The specified template does not exists.");
return $this->redirect(array("OfferTemplates::index"));
}
return compact('template');
}

public function admin_add(){
$template = OfferTemplates::create();

if ($this->request->data){
$template->set($this->request->data);
if ($template->save()){
FlashMessage::set("Template created.");
return $this->redirect(array('OfferTemplates::view', 'id' => $template->_id, 'admin'=>true));
}
}

$this->_render['template'] = 'admin_edit';
return compact('template');
}

public function admin_edit(){
$conditions = array(
'_id' => $this->request->id
);

$template = OfferTemplates::first(compact('conditions'));
if ($this->request->data){
if ($template->save($this->request->data)){
FlashMessage::set("Template modified.");
return $this->redirect(array('OfferTemplates::view', 'id' => $venue->_id));
}
}
return compact('templates');
}
}

?>
65 changes: 65 additions & 0 deletions models/OfferTemplates.php
@@ -0,0 +1,65 @@
<?php
/**
* Chowly Pick. Eat. Save!
* @copyright Copyright 2011, Martin Samson
* @copyright Copyright 2011, Chowly Corporation
*/

namespace chowly\models;

class OfferTemplates extends \chowly\extensions\data\Model{
protected static $_states = array('published'=>'published', 'unpublished'=>'unpublished');
public $validates = array(
'name' => array(
array('notEmpty','message'=>'Please enter a name'),
array('lengthBetween', 'min'=>1,'max'=>255,
'message' => 'Please enter a name that is between 1 and 255')
),
'cost' => array(
array('numeric','message'=>'Must be a monetary amount (ex: 33.00)')
)
);

protected $_schema = array(
'_id' => array('type' => 'id'),
'name' => array('type' => 'string','null'=>false), // Name of the coupon
'description'=>array('type'=>'string'), // Description (if any) of the coupon
'limitations'=>array('type'=>'string'), // Limitations regarding usage of the coupon
'created'=>array('type'=>'date'),
'updated'=>array('type'=>'date')
);

public function getErrors(){
return $this->_errors;
}

public function save($entity, $data = null, array $options = array()) {
$files = array();
if (isset($data['image'])){
$files['image'] = $data['image'];
unset($data['image']);
}

if (!$entity->_id){
$entity->_id = new \MongoId();
}

$this->_errors = array();
foreach ($files as $key => $file){
if (!$file['tmp_name'] || empty($file['tmp_name'])){
continue;
}

$image = Images::create();
$imageData = array('file'=> $file, 'parent_id'=> $entity->_id, 'parent_type'=>'offer');
if ($image->save($imageData)){
$data[$key] = $image->_id;
}else{
$this->_errors[] = "Image {$key} could not be saved.";
}
}
return parent::save($entity, $data, $options);
}
}

?>
1 change: 1 addition & 0 deletions models/Offers.php
Expand Up @@ -38,6 +38,7 @@ class Offers extends \chowly\extensions\data\Model{

protected $_schema = array(
'_id' => array('type' => 'id'),
'template_id' => array('type' => 'id'),
'venue_id' => array('type'=>'id'), // The venue
'state' => array('type'=>'string', 'default' => 'unpublished'),
'name' => array('type' => 'string','null'=>false), // Name of the coupon
Expand Down

0 comments on commit d54399a

Please sign in to comment.