コード例 #1
0
 /**
  * Copy Into Description
  * @casted
  * @return Str
  */
 public function getCopyInto()
 {
     $className = $this->CopyIntoClassName;
     if ($className) {
         $obj = $className::get()->byID($this->CopyIntoClassNameID);
         if ($obj) {
             return CopyFactory::title_for_object($obj) . " (" . $className . ")";
         }
     }
     return _t("CopyFactory.N_A", "n/a");
 }
 /**
  * we run the actual copying onAfterWrite
  */
 function onAfterWrite()
 {
     parent::onAfterWrite();
     if (SiteConfig::current_site_config()->AllowCopyingOfRecords) {
         $fieldName = $this->owner->CopyFromFieldName(false);
         $fieldNameWithID = $this->owner->CopyFromFieldName(true);
         if ($this->owner->{$fieldNameWithID}) {
             if ($copyFrom = $this->owner->{$fieldName}()) {
                 $factory = CopyFactory::create($this->owner);
                 $factory->copyObject($copyFrom, $this->owner);
             } else {
                 // a little cleanup: lets reset ...
                 $this->owner->{$fieldNameWithID} = 0;
                 $this->owner->write();
             }
         }
     }
 }
コード例 #3
0
 /**
  * Usage: an object has many children
  * and we want to also copy the children and add them to the
  * copied into parent ...
  *
  * @param DataObject $copyFromParent
  * @param DataObject $newObjectParent
  * @param String $relationalFieldForChildren - this is the field on the parent that provides the children (e.g. Children or Images) WITHOUT the ID part.
  * @param String $relationFieldForParentWithoutID - this is the field on the children that links them back to the parent.
  *
  * @return CopyFactory
  */
 function copyHasManyRelation($copyFromParent, $newObjectParent, $relationalFieldForChildren, $relationFieldForParentWithoutID)
 {
     if ($this->recordSession) {
         self::add_to_session("\n\t\t\t\t====================================\n\t\t\t\tCOPY HAS-MANY RELATION:\n\t\t\t\tCHILDREN METHOD: '{$relationalFieldForChildren}' and\n\t\t\t\tPARENT METHOD: '{$relationFieldForParentWithoutID}'\n\t\t\t\t====================================\n\t\t\t\t", $copyFromParent, $newObjectParent);
     }
     foreach ($copyFromParent->{$relationalFieldForChildren}() as $copyFromChildObject) {
         $className = $copyFromChildObject->ClassName;
         $relationFieldForParentWithID = $relationFieldForParentWithoutID . "ID";
         $childCopyField = $copyFromChildObject->CopyFromFieldName($withID = true);
         if ($this->recordSession) {
             self::add_to_session("Creating a new object '{$className}'; adding parent field ({$relationFieldForParentWithID}) ID: " . $newObjectParent->ID, $copyFromParent, $newObjectParent);
         }
         //create object and set parent ...
         $newObjectChildObject = new $className();
         if ($this->isForReal) {
             $newObjectChildObject->{$relationFieldForParentWithID} = $newObjectParent->ID;
             //does the child also copy ...
             //we copy the data here so that we dont run into validation errors
             $obj = CopyFactory::create($newObjectChildObject);
             $obj->copyObject($copyFromChildObject, $newObjectChildObject);
             //we reset the copy field here so that the copy can run another
             //time and do the has-many and many-many parts
             //$newObjectChildObject->$childCopyField = intval($copyFromChildObject->ID);
             //$newObjectChildObject->write();
             //retrieve it again to set more details ...
             //$newObjectChildObject = $className::get()->byID($newObjectChildObject->ID);
             // setting parent again - just in case ...
             $newObjectChildObject->{$relationFieldForParentWithID} = $newObjectParent->ID;
             $newObjectChildObject->write();
         }
         if ($this->recordSession) {
             if (!$newObjectChildObject) {
                 self::add_to_session("ERROR: did not create object listed above", $copyFromChildObject, $newObjectChildObject);
             } else {
                 self::add_to_session("CREATED object", $copyFromChildObject, $newObjectChildObject);
             }
             if ($newObjectChildObject->{$relationFieldForParentWithID} != $newObjectParent->ID) {
                 self::add_to_session("\n\t\t\t\t\t\tERROR: broken link ...  '" . $newObjectChildObject->{$relationFieldForParentWithID} . "' is not equal to '" . $newObjectParent->ID . "'", $copyFromChildObject, $newObjectChildObject);
                 //hack fix
             } else {
                 self::add_to_session("Saved with correct new parent field ({$relationFieldForParentWithID}) ID: " . $newObjectChildObject->{$relationFieldForParentWithID}, $copyFromChildObject, $newObjectChildObject);
             }
         }
     }
     if ($this->recordSession) {
         self::add_to_session("*** END OF copyHasManyRelation ***", $copyFromParent, $newObjectParent);
     }
     return $this;
 }