Source of file PublishWithMe.php
Size: 4,899 Bytes - Last Modified: 2021-12-24T06:40:27+00:00
/var/www/docs.ssmods.com/process/src/src/PublishWithMe.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 | <?php namespace ChristopherBolt\PublishWithMe; use SilverStripe\ORM\DataExtension; use SilverStripe\Forms\FieldList; use SilverStripe\Versioned\Versioned; use SilverStripe\Core\Config\Config; /** * PublishWithMe * Publishes/Unpublishes versioned DataObjects that are attached to a Page or parent DataObject when the parent object is published. * DataObjects must have the "Versioned" extension. * Your DataObjects can also have optional versioned functions beginning with "do" that will be called instead of the normal versioning functions. e.g. doPublish * The parent must have a $owns config variable listing the relationships to be published/unpublished etc. * * @package publishobjectswithme * @license BSD License http://www.silverstripe.org/bsd-license * @author <chris@christopherbolt.com> **/ class PublishWithMe extends DataExtension { private static $owns = array(); /** * Builds an array of objects to manage * * @return array */ private function getItemsToPublish() { //$this->owner->flushCache();// Ensure that results are not cached $objects = array(); // list of items to manage $item = $this->owner; $fields = $item->config()->get('owns'); if ($fields && is_array($fields) && count($fields)) { $has_one = $item->config()->get('has_one'); $has_many = $item->config()->get('has_many'); $many_many = $item->config()->get('many_many'); foreach ($fields as $f) { if (isset($has_one[$f])) { $object = $item->obj($f); if ($object && $object->exists()) $objects[] = $object; } else if (isset($has_many[$f]) || isset($many_many[$f])) { if ($item::has_extension('TranslatableUtility')) { if ($item->Master()->hasMethod($f)) $set = $item->Master()->$f(); } else { if ($item->hasMethod($f)) $set = $item->$f(); } if ($set) { foreach ($set as $object) { if (!$object->exists()) continue; $objects[] = $object; } } } } } return $objects; } /** * @see SiteTree::getIsModifiedOnStage * @param boolean $isModified * * @return boolean */ public function getIsModifiedOnStage($isModified) { if(!$isModified) { foreach($this->getItemsToPublish() as $field) { if(self::isObjectModifiedOnStage($field)) { $isModified = true; break; } } // repeat with Live data so we can catch data objects deleted from Stage $oldMode = Versioned::get_reading_mode(); Versioned::set_reading_mode('Stage.Live'); foreach($this->getItemsToPublish() as $field) { if(self::isObjectModifiedOnStage($field)) { $isModified = true; break; } } Versioned::set_reading_mode($oldMode); } return $isModified; } private static function isNew($object) { if(empty($object->ID)) return true; if(is_numeric($object->ID)) return false; return stripos($object->ID, 'new') === 0; } private static function isObjectModifiedOnStage($object) { // new unsaved fields could be never be published if(self::isNew($object)) return false; $stageVersion = Versioned::get_versionnumber_by_stage($object->ClassName, 'Stage', $object->ID); $liveVersion = Versioned::get_versionnumber_by_stage($object->ClassName, 'Live', $object->ID); return (($stageVersion && $stageVersion != $liveVersion) || (($object->hasMethod('getIsModifiedOnStage')) && $object->getIsModifiedOnStage(false))); } /** * @param FieldList */ public function updateCMSActions(FieldList $fields) { // Update state of publish button if there are unpublished changes in objects if ($this->getIsModifiedOnStage(false) && ($publish = $fields->fieldByName('MajorActions.action_publish'))) { $publish->addExtraClass('btn-primary font-icon-rocket'); $publish->removeExtraClass('btn-outline-primary'); $publish->setTitle(_t('SilverStripe\CMS\Model\SiteTree.BUTTONSAVEPUBLISH', 'Save & publish')); } } /** * @param FieldList */ public function updateCMSFields(FieldList $fields) { // Better buttons support, since items should be published with the page we remove BetterButtons versioning buttons? // This needs to be move to the object itself I think ?? if (class_exists('UncleCheese\BetterButtons\Actions\BetterButtonAction') && !$this->owner->config()->dont_hide_publish_buttons) { $create = Config::inst()->get("BetterButtonsActions", "create"); $edit = Config::inst()->get("BetterButtonsActions", "edit"); $create['BetterButton_SaveDraft'] = false; $create['BetterButton_Publish'] = false; $edit['BetterButton_SaveDraft'] = false; $edit['BetterButton_Publish'] = false; $edit['Group_Versioning'] = false; //$edit['BetterButton_Delete'] = false; $edit['BetterButtonFrontendLinksAction'] = false; Config::modify()->set("BetterButtonsActions", "versioned_create", $create); Config::modify()->set("BetterButtonsActions", "versioned_edit", $edit); } } } |