18/05/2011 8:43pm

PHP | Problem - Form issue


	function habitDoneForm($habit) {




$fields = new FieldSet(

new HiddenField('ID','',$habit instanceof Habit ? $habit->ID : null),

new CheckBoxField('Done', '',$habit instanceof Habit ? $habit->Done : null)

);

$actions = new FieldSet(

new FormAction('updateDone', 'Update')

);



return new Form ($this, 'habitDoneForm', $fields, $actions);

}



function updateDone($data,$form) {

$habit = DataObject::get_by_id('Habit',$data['ID']);

$habit->Done = $_POST['Done'];

$habit->write();



$this->redirectBack();

}



/*

* FILTER HABITS BY STAFF MEMBER, DUE ETC

*/



function habitFilterForm() {



$fields = new FieldSet(

new DropdownField('Priority', 'Priority:', array(

'NULL' => 'All', '3' => 'High', '2' => 'Medium', '1' => 'Low'

)),

new DropdownField('Deadline', 'Coming up (within):', array(

'NULL' => 'All', 'today' => 'Today', 'tomorrow' => 'Tomorrow', '+1 week' => 'A week', '+1 month' => 'A month', '+3 month' => 'A quarter', '+1 year' => 'A year'

)),

new DropdownField('MemberID', 'Assigned to:', array(

'NULL' => 'Unassigned', '1' => 'Alan', '2' => 'Barry', '3' => 'Colin', '4' => 'Dave', '4' => 'Ed', '6' => 'Freddie'

)),

new DropdownField('Sort', 'Sort by:', array(

'Priority ASC' => 'Highest priority', 'Deadline ASC' => 'Most urgent', 'Assigned' => 'Staff member', 'Name' => 'Description', 'Frequency DESC' => 'Most frequent', 'Frequency ASC' => 'Least frequent', 'Deadline DESC' => 'Least urgent', 'Priority DESC' => 'Lowest priority'

)));



$actions = new FieldSet(

new FormAction('filterHabits', 'Filter Habits')

);



return new Form($this, 'habitFilterForm', $fields, $actions);

}





function filterHabits($data, $form) {

$habitDisplaySet = new DataObjectSet();



$priority = $data['Priority'];

$deadline = $data['Deadline'];

$assigned = $data['MemberID'];

$sortby = $data['Sort'];

$deadline = date('Y-m-d', strtotime("$deadline"));



if ($priority != NULL) {

if ($deadline != NULL) {

if ($assigned != NULL) {

$filters = "Priority = '$priority' AND Deadline <= '$deadline' AND MemberID = '$assigned'";

}

else {

$filters = "Priority = '$priority' AND Deadline <= '$deadline'";

}

}

else if ($assigned != NULL) {

$filters = "Priority = '$priority' AND MemberID = '$assigned'";

}

else {

$filters = "Priority = '$priority'";

}

}

else if ($deadline != NULL) {

if ($assigned != NULL) {

$filters = "Deadline <= '$deadline' AND MemberID = '$assigned'";

}

else {

$filters = "Deadline <= '$deadline'";

}

}

else if ($assigned != NULL) {

$filters = "MemberID = '$assigned'";

}

else {

$filters = "";

}



$habits = DataObject::get('Habit', $filters, $sortby);


if (!$habits) {

return false;

}

else {


foreach($habits as $habit) {



$doneForm = $this->habitDoneForm($habit);



$record = array(

'Habit' => $habit->Name,

'Deadline' => DBField::create('Date', $habit->Deadline),

'Form' => $doneForm

);

//ALL GOOD UP TO HERE



$habitDisplaySet->push(new ArrayData($record));

}

Debug::Show($habitDisplaySet);



return $habitDisplaySet;

}

}


Post Comment