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'); }
public static function create_from_object($obj, $discount) { if (!is_a($obj, 'Service') || !is_a($obj, 'EventTicket')) { return false; } $item = new DiscountItem(); $item->item_id = $obj->id; $item->item_class = get_class($obj); $item->discount_id = $discount->id; $result = $item->save(); if (!$result) { return false; } return $item; }