public function updateCMSFields(FieldList $fields) { parent::updateCMSFields($fields); $className = $this->owner->ClassName; $uncompletedField = $this->owner->CopyFromFieldName(); $uncompletedFieldWithID = $uncompletedField . "ID"; $completedField = $this->owner->CopiedFromFieldName(); $completedFieldWithID = $completedField . "ID"; //remove by default $fields->removeByName($uncompletedFieldWithID); $fields->removeByName($completedFieldWithID); if ($this->owner->exists() && SiteConfig::current_site_config()->AllowCopyingOfRecords) { $changeMessage = "<p class=\"message good\">" . _t("CopyFactory.CHANGE_SETTINGS", "You can change the settings for copying in") . " <a href=\"/admin/settings/\">" . _t("CopyFactory.SITE_CONFIG", "The Site Config (see Copy Tab)") . "</a>. " . _t("CopyFactory.TURN_OFF_WHEN_NOT_IN_USE", "It is recommended you turn off the copy facility when not in use, as it will slow down the CMS.") . "\n\t\t\t\t</p>"; //reload goes here ... @todo /* if($this->owner->ID && Session::get("CopyFactoryReload") == $this->owner->ID) { Session::set("CopyFactoryReload", 0); return Controller::curr()->redirectBack(); } */ if ($this->owner->{$completedFieldWithID}) { if ($obj = $this->owner->{$completedField}()) { $fields->addFieldToTab("Root.Copy", $copyQuestionIDField = new ReadonlyField($completedField . "_EXPLANATION", _t("CopyFactory.COPIED_FROM", "This record has been copied from: "), $this->owner->CopyFactoryTitleMaker($obj))); } } elseif ($situation = SiteConfig::current_site_config()->AllowCopyingOfRecords) { if ($situation == 1) { $message = _t('CopyFactory.DRY_RUN_ONLY', "Dry run only --- any changes below will be tested once your press 'SAVE' but no actual changes will be made. You will find a log of intended changes below for review."); } if ($situation == 2) { $message = _t('CopyFactory.THIS_IS_FOR_REAL', "Any changes below will be actioned once you press 'SAVE' - please use with care."); } $fields->addFieldToTab("Root.Copy", $copyField = new LiteralField($uncompletedFieldWithID . "_WARNING", "<p class=\"warning message\">" . $message . "</p>" . $changeMessage)); $copyableObjects = $className::get()->exclude(array("ID" => intval($this->owner->ID) - 0))->filter(array("ClassName" => $this->owner->ClassName)); if ($this->owner->hasMethod("additionalFiltersForCopyableObjects")) { $copyAbleObjects = $this->owner->additionalFiltersForCopyableObjects($copyableObjects); } //there are objects to copy from if ($copyableObjects->count() > 0) { $fields->addFieldToTab("Root.Copy", $copyField = new DropdownField($uncompletedFieldWithID, _t('CopyFactory.COPY_EXPLANATION', "Copy from {name}. CAREFUL - this will replace everything in the current {name} with the one copied from ...", 'Explanation on how copying works', array('name' => $this->owner->i18n_singular_name())), $copyableObjects->map("ID", CopyFactory::preferred_title_field($this->owner)))); $copyField->setEmptyString(_t("CopyFactory.SELECT_ONE", "--- Select One ---")); } else { $fields->addFieldToTab("Root.Copy", $copyField = new LiteralField($uncompletedFieldWithID . "_EXPLANATION", "<h2>" . _t('CopyFactory.COPY_FACTORY_HELP_NO_RECORDS', "There are no records to copy from.") . "</h2>")); } } else { $fields->addFieldToTab("Root.Copy", $copyField = new LiteralField("CopyFactoryNotTurnedOn", "<h2>" . _t('CopyFactory.COPY_FACTORY_TURNED_OFF', "Copying of records is currently turned off.") . "</h2>" . $changeMessage)); } if (Config::inst()->get("CopyFactory", "debug")) { $source = CopyFactoryLog::get()->filter(array("CopyCausingClassName" => $this->owner->ClassName, "CopyCausingClassNameID" => $this->owner->ID))->exclude(array("CopyIntoClassName" => $this->owner->ClassName, "CopyIntoClassNameID" => $this->owner->ID))->exclude(array("CopyIntoClassName" => $this->owner->ClassName, "CopyFromClassNameID" => $this->owner->ID)); if ($source->count()) { $name = "COPY_CAUSING_GRIDFIELD"; $title = _t("CopyFactory.COPY_CAUSING_TITLE", "Copy actions originated from this record."); $fields->addFieldToTab("Root.Copy", $this->gridFieldMaker($name, $title, $source)); } $source = CopyFactoryLog::get()->filter(array("CopyIntoClassName" => $this->owner->ClassName, "CopyIntoClassNameID" => $this->owner->ID))->exclude(array("CopyIntoClassName" => $this->owner->ClassName, "CopyFromClassNameID" => $this->owner->ID)); if ($source->count()) { $name = "COPY_INTO_GRIDFIELD"; $title = _t("CopyFactory.COPY_INTO_TITLE", "Copy actioned into this record."); $fields->addFieldToTab("Root.Copy", $this->gridFieldMaker($name, $title, $source)); } $source = CopyFactoryLog::get()->filter(array("CopyIntoClassName" => $this->owner->ClassName, "CopyFromClassNameID" => $this->owner->ID))->exclude(array("CopyIntoClassName" => $this->owner->ClassName, "CopyIntoClassNameID" => $this->owner->ID))->exclude(array("CopyCausingClassName" => $this->owner->ClassName, "CopyCausingClassNameID" => $this->owner->ID)); if ($source->count()) { $name = "COPY_FROM_GRIDFIELD"; $title = _t("CopyFactory.COPY_FROM_TITLE", "Copy actions from this record into another record."); $fields->addFieldToTab("Root.Copy", $this->gridFieldMaker($name, $title, $source)); } } } else { } }
/** * adds additional info to current session * @param String $action * @param DataObject $copyFrom * @param DataObject $copyInto */ protected static function add_to_session($action, $copyFrom = null, $copyInto = null) { $obj = new CopyFactoryLog(); $obj->Type = Config::inst()->get("CopyFactory", "for_real") ? "Real" : "Fake"; $obj->StartTime = Config::inst()->get("CopyFactory", "start_time"); $obj->CopyCausingClassName = Config::inst()->get("CopyFactory", "dry_run_for_class_name"); $obj->CopyCausingClassNameID = Config::inst()->get("CopyFactory", "dry_run_for_id"); if ($copyFrom) { $obj->CopyFromClassNameID = $copyFrom->ID; } if ($copyInto) { $obj->CopyIntoClassName = $copyInto->ClassName; $obj->CopyIntoClassNameID = $copyInto->ID; } $obj->Action = $action; $obj->write(); if (Config::inst()->get("CopyFactory", "debug")) { $copyFromLine = ""; if ($copyFrom && $copyFrom->exists()) { $copyFromLine = "FROM: " . self::title_for_object($copyFrom) . " - " . $copyFrom->ClassName . "." . $copyFrom->ID . "\n"; } $copyIntoLine = ""; if ($copyInto && $copyInto->exists()) { $copyIntoLine = "INTO: " . self::title_for_object($copyInto) . " - " . $copyInto->ClassName . "." . $copyInto->ID . "\n"; } debug::log($copyFromLine . $copyIntoLine . $action); } }