Ejemplo n.º 1
0
 /**
  *
  * Initialization for the browse action.  This method is called before any html is output
  * to the browser.  It handles form creation and validation.
  *
  */
 function _browse_init()
 {
     import('Dataface/QuickForm.php');
     /*
      *
      * If we are not creating a new record, then we'll record this as the last
      * valid page visited.  This will be useful for forwarding to the last page
      * visited when the form is validated.
      *
      */
     if (!isset($this->_params['new'])) {
         setcookie('dataface_lastpage', $_SERVER['QUERY_STRING']);
     }
     /*
      *
      * Default functionality ('-relationship' flag is not set) is to show or validate
      * the quickform.  If the -new flag is specified, it overrides the -relationship flag.
      *
      */
     if (!isset($this->_params['relationship']) or isset($this->_params['new'])) {
         $new = (isset($this->_params['new']) and $this->_params['new']);
         /*
          *
          * Create the quickform for the current record.
          *
          */
         $form = new Dataface_QuickForm($this->_tablename, $this->_db, $this->_query, '', $new);
         if ($this->_resultSet->found() > 0 or $new) {
             /*
              * There is either a result to edit, or we are creating a new record.
              *
              */
             $res = $form->_build();
             if (PEAR::isError($res)) {
                 trigger_error($res->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR);
             }
             /*
              *
              * We need to add the current GET parameter flags (the GET vars starting with '-') so
              * that the controller knows to pass control to this method again upon form submission.
              *
              */
             foreach ($this->_query as $key => $value) {
                 if (strpos($key, '-') === 0) {
                     $form->addElement('hidden', $key);
                     $form->setDefaults(array($key => $value));
                 }
             }
             /*
              * Store the current query string (the portion after the '?') in the form, so we 
              * can retrieve it after and redirect back to our original location.
              */
             $form->addElement('hidden', '-query');
             $form->setDefaults(array('-action' => $this->_currentAction, '-query' => $_SERVER['QUERY_STRING']));
             /*
              * 
              * We have to deal with 3 cases.
              * 	1) The form has not been submitted.
              *	2) The form was submitted but didn't validate (ie: it had some bad input)
              * 	3) The form was submitted and was validated.
              *
              * We deal with Case 3 first...
              *
              */
             if ($form->validate()) {
                 /*
                  *
                  * The form was submitted and it validated ok.  We now process it (ie: save its contents).
                  *
                  */
                 $result = $form->process(array(&$form, 'save'));
                 $success = true;
                 $response =& Dataface_Application::getResponse();
                 if (!$result) {
                     trigger_error("Error occurred in save: " . mysql_error($this->_db) . Dataface_Error::printStackTrace(), E_USER_ERROR);
                     exit;
                 } else {
                     if (PEAR::isError($result) && !Dataface_Error::isNotice($result)) {
                         //echo "Error..";
                         if (Dataface_Error::isDuplicateEntry($result)) {
                             //echo "dup entry"; exit;
                             $query = array('-action' => 'error');
                             $response =& Dataface_Application::getResponse();
                             $msg = @$response['--msg'];
                             $msg = urlencode(trim("Failed to save record because another record with the same keys already exists.\n" . $msg));
                             $link = Dataface_LinkTool::buildLink($query, false) . '&--msg=' . $msg;
                             header('Location: ' . $link);
                             exit;
                         } else {
                             //echo "not dup entry"; exit;
                             trigger_error($result->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR);
                             exit;
                         }
                     } else {
                         if (Dataface_Error::isNotice($result)) {
                             $response['--msg'] = @$response['--msg'] . "\n" . $result->getMessage();
                             $success = false;
                         }
                     }
                 }
                 if ($new) {
                     /*
                      *
                      * If the form created a new record, then it makes more sense to redirect to this newly
                      * created record than to the old record.  We used the 'keys' of the new record to generate
                      * a redirect link.
                      *
                      */
                     $query = $form->_record->getValues(array_keys($form->_record->_table->keys()));
                     $msg = @$response['--msg'];
                     $msg = urlencode(trim(($success ? "Record successfully saved.\n" : '') . $msg));
                     $link = Dataface_LinkTool::buildLink($query, false) . '&--msg=' . $msg;
                 } else {
                     /*
                      *
                      * The original query string will have the -new flag set.  We need to remove this 
                      * flag so that we don't redirect the user to create another new record.
                      *
                      */
                     $vals = $form->exportValues();
                     $vals['-query'] = preg_replace('/[&\\?]-new=[^&]+/i', '', $vals['-query']);
                     $msg = @$response['--msg'];
                     $msg = urlencode(trim(($success ? "Record successfully saved.\n" : '') . $msg));
                     $link = $_SERVER['HOST_URI'] . DATAFACE_SITE_HREF . '?' . $vals['-query'] . '&--msg=' . $msg;
                 }
                 /*
                  *
                  * Redirect the user to the appropriate record.
                  *
                  */
                 header("Location: {$link}");
                 exit;
             }
         }
         $this->_vars['form'] =& $form;
     }
 }