Example #1
0
 public function add()
 {
     $argumentarray = Router::$arguments;
     //$id = $argumentarray[0];
     $products = new Product_Model();
     $id = $products->getNextID();
     if (isset($_POST['save'])) {
         $post = new Validation(array_merge($_POST, $_FILES));
         $post->pre_filter('trim', 'productName', 'productSize', 'productDescription', 'productImage');
         $post->add_rules('productName', 'required');
         $post->add_rules('productDescription', 'required');
         if (!$post->validate()) {
             $errors = $post->errors('form_errors');
             foreach ($errors as $error) {
                 echo '<p class="error">' . $error . '</p>';
             }
         } else {
             //$id = $argumentarray[0];
             $products = new Product_Model();
             $product = ORM::factory('product');
             $product->name = $post->productName;
             $product->size = $post->productSize;
             $product->description = $post->productDescription;
             $product->image = $post->productImage;
             if (!empty($_FILES['image']['name'])) {
                 // uses Kohana upload helper
                 $_FILES = Validation::factory($_FILES)->add_rules('image', 'upload::valid', 'upload::type[gif,jpg,jpeg,png]', 'upload::size[2M]');
                 if ($_FILES->validate()) {
                     // Temporary file name
                     $filename = upload::save('image', basename($_FILES['image']['tmp_name']));
                     $file = basename($_FILES['image']['name']);
                     // Resize, sharpen, and save the image
                     Image::factory($filename)->save(DOCROOT . '../../env/product_images/' . $file);
                     // Remove the temporary file
                     unlink($filename);
                     $product->image = $file;
                 } else {
                     $errors = $_FILES->errors('form_user');
                 }
             }
             $product->save();
             url::redirect('/products/edit/' . $product->id);
         }
     }
     $this->_renderView();
 }
}
</script>

<?php 
//$id = $this->uri->segment(3);
$argumentarray = Router::$arguments;
$products = new Product_Model();
$production_times = new Production_Time_Model();
$product_colors = new Product_Color_Model();
$product_flavors = new Product_Flavor_Model();
$product_costs = new Product_Cost_Model();
if (isset($argumentarray[0])) {
    $id = $argumentarray[0];
    $product = $products->getProductByID($id);
} else {
    $id = $products->getNextID();
    $product = ORM::factory('product');
}
$i = 0;
$j = 0;
$times = $production_times->getProductionTimes();
$productcolors = $product_colors->getColorsForProduct($id);
$productflavors = $product_flavors->getFlavorsForProduct($id);
$costs = $product_costs->getCostsForProduct($id);
?>
		


<form action="<?php 
echo url::base() . $this->uri->segment(1) . '/' . $this->uri->segment(2) . '/' . $id;
?>