/**
  * Loads the parent record based on POST and GET parameters.
  */
 function &getRecord()
 {
     if (Dataface_ExistingRelatedRecordForm::formSubmitted()) {
         $record = new Dataface_Record($_POST['-table'], array());
         $io = new Dataface_IO($_POST['-table']);
         $query = $_POST['__keys__'];
         if (is_array($query)) {
             foreach (array_keys($query) as $postKey) {
                 if ($query[$postKey][0] != '=') {
                     $query[$postKey] = '=' . $query[$postKey];
                 }
             }
         }
         $io->read($query, $record);
         return $record;
     } else {
         $app =& Dataface_Application::getInstance();
         $qt =& Dataface_QueryTool::loadResult($app->_currentTable);
         return $qt->loadCurrent();
     }
 }
 function handle(&$params)
 {
     import('Dataface/ExistingRelatedRecordForm.php');
     $app =& Dataface_Application::getInstance();
     $query =& $app->getQuery();
     $resultSet =& $app->getResultSet();
     //$record =& $app->getRecord();	// loads the current record
     if (!isset($query['-relationship'])) {
         return PEAR::raiseError(Dataface_LanguageTool::translate('Error: No relationship specified', 'Error.  No relationship was specified when trying to add existing related record.'), DATAFACE_E_NOTICE);
     }
     $record = null;
     $form = new Dataface_ExistingRelatedRecordForm($record, $query['-relationship']);
     $res = $form->_build();
     if (PEAR::isError($res)) {
         return Dataface_Error::permissionDenied($res->getMessage());
     }
     /*
      *
      * 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 ($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' => $query['-action'], '-query' => $_SERVER['QUERY_STRING']));
     if (!$form->_record || !is_a($form->_record, 'Dataface_Record')) {
         trigger_error(Dataface_LanguageTool::translate('Fatal Error', 'Fatal Error: Form should have loaded record but the record was null. ' . Dataface_Error::printStackTrace(), array('stack_trace' => Dataface_Error::printStackTrace(), 'msg' => 'Form should have loaded record but the record was null.')), E_USER_ERROR);
     }
     if (!Dataface_PermissionsTool::checkPermission('add existing related record', $form->_record)) {
         return Dataface_Error::permissionDenied(Dataface_LanguageTool::translate('Error: Permission denied adding existing related record', 'Permission Denied.  You do not have sufficient permissions to add an existing related record.  Required permission: "add existing related record", but you have only been granted permissions: "' . implode(',', $form->_record->getPermissions()) . '".', array('required_permission' => 'add existing related record', 'granted_permissions' => implode(',', $form->_record->getPermissions()))));
     }
     if ($form->validate()) {
         $res = $form->process(array(&$form, 'save'), true);
         $response =& Dataface_Application::getResponse();
         if (PEAR::isError($res) && !Dataface_Error::isNotice($res)) {
             return $res;
         } else {
             if (Dataface_Error::isNotice($res)) {
                 //$response['--msg'] = @$response['--msg'] . "\n".$res->getMessage();
                 $app->addError(PEAR::raiseError(df_translate('Failed to add record because of errors', 'Failed to add record to relationship because of the following errors:'), DATAFACE_E_NOTICE));
                 $app->addError($res);
                 $success = false;
             } else {
                 $success = true;
             }
         }
         if ($success) {
             import('Dataface/Utilities.php');
             Dataface_Utilities::fireEvent('after_action_existing_related_record');
             $fquery = array('-action' => 'browse');
             $msg = Dataface_LanguageTool::translate('Record successfully added to relationship', "The record has been successfully added to the " . $query['-relationship'] . " relationship.\n", array('relationship' => $query['-relationship']));
             $msg = urlencode(trim(($success ? $msg : '') . @$response['--msg']));
             $vals = $form->exportValues();
             if (isset($vals['--redirect'])) {
                 $qmark = strpos($vals['--redirect'], '?') !== false ? '&' : '?';
                 header('Location: ' . $vals['--redirect'] . $qmark . '--msg=' . $msg);
                 exit;
             }
             foreach ($vals['__keys__'] as $key => $value) {
                 $fquery[$key] = "=" . $value;
             }
             $link = Dataface_LinkTool::buildLink($fquery);
             header("Location: {$link}" . "&--msg=" . $msg);
             exit;
         }
     }
     ob_start();
     $form->display();
     $out = ob_get_contents();
     ob_end_clean();
     $context = array('form' => $out);
     if (isset($query['-template'])) {
         $template = $query['-template'];
     } else {
         if (isset($params['action']['template'])) {
             $template = $params['action']['template'];
         } else {
             $template = 'Dataface_Add_Existing_Related_Record.html';
         }
     }
     df_display($context, $template, true);
 }
Exemple #3
0
 /**
  * Handles initialization and control for the existing related record form.
  */
 function _existing_related_record_init()
 {
     import('Dataface/ExistingRelatedRecordForm.php');
     if (!isset($this->_params['relationship'])) {
         $this->_vars['error'] = "<div class=\"error\">Error. no relationship was specified.<!-- At line " . __LINE__ . " of file " . __FILE__ . " --></div>";
         return;
     }
     $record = null;
     // let the form handle getting the current record.
     $form = new Dataface_ExistingRelatedRecordForm($record, $this->_params['relationship']);
     $form->_build();
     $this->_vars['form'] =& $form;
     if (!Dataface_PermissionsTool::edit($form->_record)) {
         $this->_vars['error'] = "<div class=\"error\">Error.  Permission Denied.<!-- At line " . __LINE__ . " of file " . __FILE__ . " --></div>";
         return;
     }
     if ($form->validate()) {
         $res = $form->process(array(&$form, 'save'), true);
         $response =& Dataface_Application::getResponse();
         if (PEAR::isError($res) && !Dataface_Error::isNotice($res)) {
             $this->_vars['error'] = "<div class=\"error\">Error.  " . $res->toString() . "<!-- At line " . __LINE__ . " of file " . __FILE__ . " --></div>";
             return;
         } else {
             if (Dataface_Error::isNotice($res)) {
                 $response['--msg'] = @$response['--msg'] . "\n" . $res->getMessage();
                 $success = false;
             } else {
                 $success = true;
             }
         }
         $query = array('-action' => 'browse');
         $msg = urlencode(trim(($success ? "The record has been successfully added to the " . $this->_params['relationship'] . " relationship.\n" : '') . @$response['--msg']));
         $vals = $form->exportValues();
         foreach ($vals['__keys__'] as $key => $value) {
             $query[$key] = "=" . $value;
         }
         $link = Dataface_LinkTool::buildLink($query);
         header("Location: {$link}" . "&--msg=" . $msg);
         exit;
     }
 }