> > Topic

submit page function

Dec 14, 2006 / pixtur
 

Attached files

No files uploaded
 
For understanding submitting of form data, let's look at an example:

from pages/effort.inc (v0.0493)

function effortEditSubmit()
{
    global $PH;

If form has a cancel-button, we renderthe original page. If there was no originating page, we render project view.

   ### cancel ###        
   if(get('form_do_cancel')) {
       if(!$PH->showFromPage()) {
           $PH->show('projView',array('prj'=>$effort->project));
       }
       exit;
   }

First we need the id of the edditted object. There has to be one and only one id. If we can not get it, the getOnePassedId() will automatically abort to the orignating page.

   ### get effort ####
   $id= getOnePassedId('effort');

Creating new objects can tricky. We don't want to add anything until the form is been validated and submitted. New, temporary objects have a zero id. For this we create a new temporary Effort object. Otherwise we query the Effort object, validating the the current user may edit it.

   if($id == 0) {
      $effort= new Effort(array('id'=>0));
   }
   else {
       $effort= Effort::getEditableById($id);
       if(!$effort) {
          $PH->abortWarning(__("Could not get effort"));
          return;
       }
   }

No project item without a project. If we constructed a new Effort, the project attribute has already be initialized by the constructor. So if we can't get the project, something went wrong and we best go back to the originating page.

   ### get project ###
   $effort->project=get('effort_project');
   if(!$project = Project::getVisibleById($effort->project)) {
       $PH->abortWarning(__("Could not get project of effort"));
   }

The following code is tricky. Although it really saves alot of programming effort, it querries all object's fields for which "visible_in_forms" was true. There are a lot of issues with this approach and sophisticated forms like the personEdit no longer use it but querry all fields with get('effort_???')

   foreach($effort->fields as $f) {
       $name=$f->name;
       $f->parseForm(&$effort);
   }


If the effort is linked to a task, we have to make sure this task is valid and visible. We do this by querying the database for the id.

   ### link to task ###
   if($task_id = get('effort_task')) {
       if($task= Task::getVisibleById($task_id)) {
           $effort->task = $task->id;
       }
   }


Now let's do some form validation. If a validation didn't work, we directly render the edit-page. Since we have not rendered any html-output yet, we just render the editPage again.

   ### go back to from if validation fails ###
   $failure= false;
   if(!$effort->name) {
       $failure= true;
       $PH->messages[]=__("Name required");
   }
   if(strtotime($effort->time_end) - strtotime($effort->time_start) < 0) {
       $failure= true;
       $PH->messages[]=__("Cannot start before end.");
   }
   if($failure) {
       $PH->show('effortEdit',NULL,$effort);
       exit;
   }


If things went well we add the new object to the database.

   ### write to db ###
   if($effort->id == 0) {
       $effort->insert();
   }
   else {
       $effort->update();
   }

And on success we return to the orignating page, if it has been defined. Otherwise to the project view.

   ### display taskView ####
   if(!$PH->showFromPage()) {
       $PH->show('projView',array('prj'=>$effort->project));
   }
}

No Comments