Example #1
0
File: IO.php Project: promoso/HVAC
 /**
  * Removes the given related record from its relationship.
  *
  * @param Dataface_RelatedRecord &$related_record The related record to be removed.
  * @param boolean $delete If true then the record will also be deleted from 
  * 	the database.
  * @since 0.6.1
  */
 function removeRelatedRecord(&$related_record, $delete = false, $secure = false)
 {
     if ($secure && !$related_record->_record->checkPermission('remove related record', array('relationship' => $related_record->_relationshipName))) {
         // Use security to check to see if we are allowed to delete this
         // record.
         //echo $related_record->_record->_table->getDelegate()->getRoles(array('relationship'=>$related_record->_relationshipName));exit;
         return Dataface_Error::permissionDenied(df_translate('scripts.Dataface.IO.removeRelatedRecord.PERMISSION_DENIED', 'Could not remove record "' . $related_record->getTitle() . '" from relationship "' . $related_record->_relationshipName . '" of record "' . $related_record->_record->getTitle() . '" because you have insufficient permissions.', array('title' => $related_record->getTitle(), 'relationship' => $related_record->_relationshipName, 'parent' => $related_record->_record->getTitle())));
     }
     $res = $this->fireEvent('beforeRemoveRelatedRecord', $related_record);
     if (PEAR::isError($res)) {
         return $res;
     }
     /*
      * First we need to find out which table is the domain table.  The domain table
      * is the table that actually contains the records of interest.  The rest of
      * the tables are referred to as 'join' tables.
      */
     $domainTable = $related_record->_relationship->getDomainTable();
     if (PEAR::isError($domainTable)) {
         /*
          * Dataface_Relationship::getDomainTable() throws an error if there are 
          * no join tables.  We account for that by explicitly setting the domain
          * table to the first table in the list.
          */
         $domainTable = $related_record->_relationship->_schema['selected_tables'][0];
     }
     /*
      * Next we construct an IO object to write to the domain table.
      */
     $domainIO = new Dataface_IO($domainTable);
     $domainTable =& Dataface_Table::loadTable($domainTable);
     // reference to the Domain table Dataface_Table object.
     /*
      * Begin building queries.
      */
     $query = array();
     // query array to build the query to delete the record.
     $absVals = array();
     // same as query array except the keys are absolute field names (ie: Tablename.Fieldname)
     $currKeyNames = array_keys($domainTable->keys());
     // Names of key fields in the domain table
     foreach ($currKeyNames as $keyName) {
         $query[$keyName] = $related_record->val($keyName);
         $absVals[$domainTable->tablename . '.' . $keyName] = $query[$keyName];
     }
     $fkeys = $related_record->_relationship->getForeignKeyValues($absVals, null, $related_record->_record);
     $warnings = array();
     $confirmations = array();
     foreach (array_keys($fkeys) as $currTable) {
         // For each table in the relationship we go through and delete its record.
         $io = new Dataface_IO($currTable);
         $record = new Dataface_Record($currTable, array());
         $res = $io->read($fkeys[$currTable], $record);
         //patch for Innodb foreign keys with ON DELELE CASCADE
         // Contributed by Optik
         if (!$io->recordExists($record, null, $currTable)) {
             $warnings[] = df_translate('scripts.Dataface.IO.removeRelatedRecord.ERROR_RECORD_DOESNT_EXIST', "Failed to delete entry for record '" . $record->getTitle() . "' in table '{$currTable}' because record doesn't exist.", array('title' => $record->getTitle(), 'currTable' => $currTable));
             unset($record);
             unset($io);
             continue;
         }
         // -- end patch for Innodb foreign keys
         if ($currTable == $domainTable->tablename and !$delete) {
             // Unless we have specified that we want the domain table record
             // deleted, we leave it alone!
             unset($record);
             unset($io);
             continue;
         }
         // Let's figure out whether we need to use security for deleting this
         // record.
         // If security is on, and it is the domain table, and the user doesn't
         // have the 'delete related record' permission  then we need to use
         // security
         if ($currTable == $domainTable->tablename and $secure and !$related_record->_record->checkPermission('delete related record', array('relationship' => $related_record->_relationshipName))) {
             $useSecurity = true;
         } else {
             $useSecurity = false;
         }
         $res = $io->delete($record, $useSecurity);
         if (PEAR::isError($res) && Dataface_Error::isError($res)) {
             //$this->logError($res);
             return $res;
         } else {
             if (PEAR::isError($res)) {
                 $warnings[] = $res;
             } else {
                 $confirmations[] = df_translate('Successfully deleted record', "Successfully deleted entry for record '" . $record->getTitle() . "' in table '{$currTable}'", array('title' => $record->getTitle(), 'table' => $currTable));
             }
         }
         $record->__destruct();
         unset($record);
         unset($b);
         unset($io);
     }
     $res = $this->fireEvent('afterRemoveRelatedRecord', $related_record);
     if (PEAR::isError($res)) {
         return $res;
     }
     if (count($warnings) > 0) {
         return PEAR::raiseError(@implode("\n", $warnings), DATAFACE_E_WARNING);
     }
     if (count($confirmations) == 0) {
         return false;
     }
     return true;
 }
Example #2
0
 function delete($values)
 {
     require_once 'Dataface/IO.php';
     $query = $this->_buildDeleteQuery($values);
     if (PEAR::isError($query)) {
         return $query;
     }
     $io = new Dataface_IO($this->_tablename);
     $it =& df_get_records($this->_tablename, $query);
     $warnings = array();
     while ($it->hasNext()) {
         $record =& $it->next();
         $res = $io->delete($record);
         if (PEAR::isError($res) && Dataface_Error::isError($res)) {
             // this is a serious error... kill it
             return $res;
         } else {
             if (PEAR::isError($res)) {
                 // this is a warning or a notice
                 $warnings[] = $res;
             }
         }
         unset($record);
     }
     if (count($warnings) > 0) {
         return $warnings;
     }
     return true;
 }