10/01/2012 1:04am

PHP | Problem - dataob


<?php

class Menu extends DataObject {

static $db = array(
'Name' => 'Text',
'Preis' => 'Currency',
'Beschreibung' => 'HTMLText',
'Date' => 'Date',
'Menge' => 'Int'
);



static $has_one = array(
'Restaurant' => 'Restaurant'
);

static $belongs_to = array(
'Restaurant' => 'Restaurant'
);

static $defaults = array(
"Name" => '',
"Preis" => '11',
"Menge" => '25',
"Beschreibung" => 'fein und frisch zubereitet'
);

static $default_sort = "Date DESC";


function getCMSFields() {
$fields = parent::getCMSFields();

$dateField = new DateField('Date','Wann ist dieses Menü verfügbar?');
$dateField->setLocale('de_DE');
$dateField->setConfig('dateformat', 'EEEE dd. MMMM YYYY');
$dateField->setConfig('showcalendar', true);
$fields->addFieldToTab('Root.Main',$dateField);

return $fields;
}

function beenBought(Restaurant $restaurant) {
// check if current menu has been ordered already
$DeliveryDay = Restaurant::DeliveryDate();
if (isset($this->record['Date'])) {
$DeliveryDay = $this->record['Date'];

$records = DB::query("SELECT *
FROM Restaurant
LEFT JOIN Product_OrderItem ON Restaurant.ID = Product_OrderItem.ProductID
LEFT JOIN OrderItem ON Product_OrderItem.ID = OrderItem.ID
LEFT JOIN Menu ON Restaurant.ID = Menu.RestaurantID
LEFT JOIN `OrderAttribute` ON OrderAttribute.ID = OrderItem.ID
LEFT JOIN `Order` ON Order.ID = OrderAttribute.OrderID
WHERE Date(Menu.Date) = DATE('".$DeliveryDay."')
AND Date(Order.DeliveryDay) = DATE('".$DeliveryDay."')
AND Restaurant.ID = $restaurant->ID
");
$items = singleton('DataObject')->buildDataObjectSet($records);

if (isset($items))
return true;

}
return false;
}


public function getCMSFields_forPopup()
{
$fields = parent::getCMSFields();
// Get Restaurant for loggend in User
$userid = Member::currentUserID();
$filter = "`OwnerID` = $userid";
$restaurant = DataObject::get_one('Restaurant', $filter);
$todayOrders = RestaurantPage::TodayOrdersForRestaurant($restaurant->ID);
if ($this->beenBought($restaurant)) $fields->addFieldToTab('Root.Main', new LiteralField('BoughtText','<h3>Da das Menu bereits bestellt wurde, kann der Preis und das Datum nicht mehr verändert werden.<br><b><center>Auch wenn Sie ein weiteres, ähnliches Menu anbieten wollen,<br>gehen Sie bitte zurück und legen dieses komplett neu an!</b></center></h3>'));
$dateField = new DateField('Date','Wann ist dieses Menü verfügbar?', 'Preis');
if ($this->beenBought($restaurant)) $dateField->disabled = true;
$dateField->setLocale('de_DE');
$dateField->setConfig('dateformat', 'EEEE dd. MMMM YYYY');
$dateField->setConfig('showcalendar', true);
$fields->addFieldToTab('Root.Main',$dateField);
$fields->addFieldToTab('Root.Main', new TextField('Name','Name des Menüs (Maximal 35 Zeichen)', '', 35));
$priceField = new CurrencyField('Preis','Preis - Bitte ab sofort den Nettopreis einpflegen (Ohne die 3.50 foodpedal.ch Marge)');
if ($this->beenBought($restaurant)) $priceField->disabled = true;
$fields->addFieldToTab('Root.Main', $priceField);
$fields->addFieldToTab('Root.Main', new NumericField('Menge', 'Wieviele Menüs können maximal bestellt werden?'));
$fields->addFieldToTab('Root.Main', new HtmlEditorField('Beschreibung','Zusätzliche Beschreibung des Menüs', 3));
$fields->addFieldToTab('Root.Main', new HiddenField("RestaurantID", "RestaurantID", $restaurant->ID));
return $fields;
}

function canDelete($member) {
return false;
}

public function getFormattedDate() {
setlocale (LC_TIME, 'de_DE');
$date = $this->obj('Date')->Format('l d. F Y');
return $date;
}

public function getRestoName() {
$res = $this->obj('Restaurant')->Name;
return $res;
}

function onBeforeWrite() {
// Only upon on New Creation
if(!$this->ID) {
$restaurant = DataObject::get_by_id("Restaurant", $this->RestaurantID);
$doubleDay = DataObject::get("Menu","`RestaurantID` = ".$restaurant->ID." AND `Date` like '". $this->Date."'");
if($doubleDay) {
user_error('Nur ein Menü pro Tag erlaubt!', E_USER_ERROR);
exit();
}
}
// CAUTION: You are required to call the parent-function, otherwise sapphire will not execute the request.
parent::onBeforeWrite();
}


function FullPrice(){
$price = $this->Preis + 3.50;
return number_format($price ,2);
}
}

class Menu_controller extends Controller {
public function getRestaurantName(){
return $this->Restaurant->Name;
}
}


?>


Post Comment