예제 #1
0
파일: IO.php 프로젝트: minger11/Pipeline
 /**
  * Adds a new record to a relationships.
  * @param $record A Dataface_RelatedRecord object to be added.
  */
 function addRelatedRecord(&$record, $secure = false)
 {
     if ($secure && !$record->_record->checkPermission('add new related record', array('relationship' => $record->_relationshipName))) {
         // Use security to check to see if we are allowed to delete this
         // record.
         return Dataface_Error::permissionDenied(df_translate('scripts.Dataface.IO.addRelatedRecord.PERMISSION_DENIED', 'Could not add record "' . $record->getTitle() . '" to relationship "' . $record->_relationshipName . '" of record "' . $record->_record->getTitle() . '" because you have insufficient permissions.', array('title' => $record->getTitle(), 'relationship' => $record->_relationshipName, 'parent' => $record->_record->getTitle())));
     }
     $queryBuilder = new Dataface_QueryBuilder($this->_table->tablename);
     // Fire the "before events"
     if ($this->fireTriggers) {
         $res = $this->fireBeforeAddRelatedRecord($record);
         if (PEAR::isError($res)) {
             return $res;
         }
     }
     if ($this->fireTriggers) {
         $res = $this->fireBeforeAddNewRelatedRecord($record);
         if (PEAR::isError($res)) {
             return $res;
         }
     }
     // It makes sense for us to fire beforeSave, afterSave, beforeInsert, and afterInsert
     // events here for the records that are being inserted.  To do this we will need to extract
     // Dataface_Record objects for all of the tables that will have records inserted.
     $drecords = $record->toRecords();
     // $drecords is an array of Dataface_Record objects
     foreach (array_keys($drecords) as $recordIndex) {
         $rio = new Dataface_IO($drecords[$recordIndex]->_table->tablename);
         $drec_snapshot = $drecords[$recordIndex]->strvals();
         $res = $rio->fireBeforeSave($drecords[$recordIndex]);
         if (PEAR::isError($res)) {
             return $res;
         }
         $res = $rio->fireBeforeInsert($drecords[$recordIndex]);
         if (PEAR::isError($res)) {
             return $res;
         }
         $drec_post_snapshot = $drecords[$recordIndex]->strvals();
         foreach ($drec_snapshot as $ss_key => $ss_val) {
             if ($drec_post_snapshot[$ss_key] != $ss_val) {
                 $record->setValue($ss_key, $drec_post_snapshot[$ss_key]);
             }
         }
         unset($drec_snapshot);
         unset($drec_post_snapshot);
         unset($rio);
     }
     //$sql = Dataface_QueryBuilder::addRelatedRecord($record);
     $sql = $queryBuilder->addRelatedRecord($record);
     if (PEAR::isError($sql)) {
         $sql->addUserInfo(df_translate('scripts.Dataface.IO.addRelatedRecord.ERROR_GENERATING_SQL', "Error generating sql in ShortRelatedRecordForm::save()", array('line' => 0, 'file' => "_")));
         return $sql;
     }
     // Actually add the record
     $res = $this->performSQL($sql);
     if (PEAR::isError($res)) {
         return $res;
     }
     $rfields = array_keys($record->vals());
     // Just for completeness we will fire afterSave and afterInsert events for
     // all records being inserted.
     foreach (array_keys($drecords) as $recordIndex) {
         $currentRecord =& $drecords[$recordIndex];
         if (isset($this->insertids[$currentRecord->_table->tablename])) {
             $idfield = $currentRecord->_table->getAutoIncrementField();
             if ($idfield) {
                 $currentRecord->setValue($idfield, $this->insertids[$currentRecord->_table->tablename]);
                 if (in_array($idfield, $rfields)) {
                     $record->setValue($idfield, $this->insertids[$currentRecord->_table->tablename]);
                 }
             }
             unset($idfield);
         }
         unset($currentRecord);
         $rio = new Dataface_IO($drecords[$recordIndex]->_table->tablename);
         $res = $rio->saveTransients($drecords[$recordIndex], null, null, true);
         if (PEAR::isError($res)) {
             return $res;
         }
         $res = $rio->fireAfterInsert($drecords[$recordIndex]);
         if (PEAR::isError($res)) {
             return $res;
         }
         $res = $rio->fireAfterSave($drecords[$recordIndex]);
         if (PEAR::isError($res)) {
             return $res;
         }
         unset($rio);
     }
     // Fire the "after" events
     if ($this->fireTriggers) {
         $res2 = $this->fireAfterAddNewRelatedRecord($record);
         if (PEAR::isError($res2)) {
             return $res2;
         }
         $res2 = $this->fireAfterAddRelatedRecord($record);
         if (PEAR::isError($res2)) {
             return $res2;
         }
     }
     return $res;
 }