public function create($discount_id = null, $item = null) { if (!$discount_id) { $discount_id = $this->GetData('discount_id'); } $discount = DiscountCode::find_by_id($discount_id); if (!$discount) { throw new Error404('Unable to find DiscountCode'); } if (!$item) { $item = $this->GetData('item'); } $items = array('ticket' => 'EventTicket', 'service' => 'Service'); if (!isset($items[$item])) { $item = 'ticket'; } $class = $items[$item]; $ditem = new DiscountItem(); $ditem->discount_id = $discount->id; $ditem->item_class = $class; if ($this->post) { $ditem->item_id = $this->PostData('item_id'); if ($ditem->save()) { Site::Flash('notice', 'The discount item has been added'); Redirect("admin/discounts/{$discount->id}"); } } $allItems = call_user_func(array($class, 'find_all')); $allItems = array_reverse($allItems); $items = array(); foreach ($allItems as $obj) { $items[$obj->id] = "[{$obj}] {$obj->event->name} - {$obj->name}"; } $this->assign('ditem', $ditem); $this->assign('item', $item); $this->assign('discount', $discount); $this->assign('items', $items); $this->title = "Discounts :: {$discount->code} :: Items"; $this->render('discount_item/create.tpl'); }
protected function load_discount($id = null) { if ($this->GetData('id')) { $id = $this->GetData('id'); } $discount = DiscountCode::find_by_id($id); if ($discount) { return $discount; } else { throw new Error404(); } }