2009-08-30 07:10:45 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
/**
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
|
|
|
* Copyright (C) 2000-2009 Bharat Mediratta
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Admin_Product_Lines_Controller extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* the index page of the user homes admin
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$view = new Admin_View("admin.html");
|
|
|
|
$view->content = new View("admin_product_lines.html");
|
2010-08-30 01:09:23 +00:00
|
|
|
$view->content->products = ORM::factory("product")->order_by("name")->find_all();
|
2009-08-30 07:10:45 +00:00
|
|
|
|
|
|
|
print $view;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_product_form() {
|
|
|
|
print product::get_add_form_admin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function add_product() {
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
|
|
$form = product::get_add_form_admin();
|
|
|
|
$valid = $form->validate();
|
|
|
|
$name = $form->add_product->inputs["name"]->value;
|
2010-08-30 01:09:23 +00:00
|
|
|
$product = ORM::factory("product")->where("name", "=", $name)->find();
|
|
|
|
if ($product->loaded()) {
|
2009-08-30 07:10:45 +00:00
|
|
|
$form->add_product->inputs["name"]->add_error("in_use", 1);
|
|
|
|
$valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($valid) {
|
|
|
|
$product = product::create(
|
2009-09-29 22:42:17 +00:00
|
|
|
$name,
|
|
|
|
$form->add_product->cost->value,
|
|
|
|
$form->add_product->description->value,
|
|
|
|
$form->add_product->postage_band->value
|
|
|
|
);
|
2009-08-30 07:10:45 +00:00
|
|
|
|
|
|
|
$product->save();
|
|
|
|
message::success(t("Created product %product_name", array(
|
2009-08-31 20:22:21 +00:00
|
|
|
"product_name" => html::clean($product->name))));
|
2010-08-30 01:09:23 +00:00
|
|
|
print json::reply(array("result" => "success"));
|
2009-08-30 07:10:45 +00:00
|
|
|
} else {
|
2010-08-30 01:09:23 +00:00
|
|
|
print $form;
|
2009-08-30 07:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete_product_form($id) {
|
|
|
|
$product = ORM::factory("product", $id);
|
2010-08-30 01:09:23 +00:00
|
|
|
if (!$product->loaded()) {
|
2009-08-30 07:10:45 +00:00
|
|
|
kohana::show_404();
|
|
|
|
}
|
|
|
|
print product::get_delete_form_admin($product);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete_product($id) {
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
|
|
$product = ORM::factory("product", $id);
|
2010-08-30 01:09:23 +00:00
|
|
|
if (!$product->loaded()) {
|
2009-08-30 07:10:45 +00:00
|
|
|
kohana::show_404();
|
|
|
|
}
|
|
|
|
|
2009-09-29 22:42:17 +00:00
|
|
|
$form = product::get_delete_form_admin($product);
|
2009-08-30 07:10:45 +00:00
|
|
|
if($form->validate()) {
|
|
|
|
$name = $product->name;
|
|
|
|
$product->delete();
|
|
|
|
} else {
|
2010-08-30 01:09:23 +00:00
|
|
|
print $form;
|
2009-08-30 07:10:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 20:22:21 +00:00
|
|
|
$message = t("Deleted user %product_name", array("product_name" => html::clean($name)));
|
2009-08-30 07:10:45 +00:00
|
|
|
log::success("user", $message);
|
|
|
|
message::success($message);
|
2010-08-30 01:09:23 +00:00
|
|
|
print json::reply(array("result" => "success"));
|
2009-08-30 07:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function edit_product($id) {
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
|
|
$product = ORM::factory("product", $id);
|
2010-08-30 01:09:23 +00:00
|
|
|
if (!$product->loaded()) {
|
2009-08-30 07:10:45 +00:00
|
|
|
kohana::show_404();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = product::get_edit_form_admin($product);
|
|
|
|
$valid = $form->validate();
|
|
|
|
if ($valid) {
|
|
|
|
$new_name = $form->edit_product->inputs["name"]->value;
|
|
|
|
if ($new_name != $product->name &&
|
|
|
|
ORM::factory("product")
|
2010-08-30 01:09:23 +00:00
|
|
|
->where("name", "=", $new_name)
|
|
|
|
->where("id","!=", $product->id)
|
2009-08-30 07:10:45 +00:00
|
|
|
->find()
|
2010-08-30 01:09:23 +00:00
|
|
|
->loaded()) {
|
2009-08-30 07:10:45 +00:00
|
|
|
$form->edit_product->inputs["name"]->add_error("in_use", 1);
|
|
|
|
$valid = false;
|
|
|
|
} else {
|
|
|
|
$product->name = $new_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($valid) {
|
|
|
|
$product->cost = $form->edit_product->cost->value;
|
|
|
|
$product->description = $form->edit_product->description->value;
|
2009-09-29 22:42:17 +00:00
|
|
|
$product->postage_band_id = $form->edit_product->postage_band->value;
|
2009-08-30 07:10:45 +00:00
|
|
|
$product->save();
|
|
|
|
|
|
|
|
message::success(t("Changed product %product_name",
|
2009-08-31 20:22:21 +00:00
|
|
|
array("product_name" => html::clean($product->name))));
|
2010-08-30 01:09:23 +00:00
|
|
|
print json::reply(array("result" => "success"));
|
2009-08-30 07:10:45 +00:00
|
|
|
} else {
|
2010-08-30 01:09:23 +00:00
|
|
|
print $form;
|
2009-08-30 07:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit_product_form($id) {
|
|
|
|
$product = ORM::factory("product", $id);
|
2010-08-30 01:09:23 +00:00
|
|
|
if (!$product->loaded()) {
|
2009-08-30 07:10:45 +00:00
|
|
|
kohana::show_404();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = product::get_edit_form_admin($product);
|
|
|
|
|
|
|
|
print $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|