Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GraphicAndInfoModule extends DataObject
- {
- public static $db = array (
- 'GITitle' => 'Text',
- 'GISubTitle' => 'Text',
- 'GIDescription' => 'HTMLText',
- 'GIDescriptionWrap' => 'Boolean',
- 'GIDescriptionExtendedHide' => 'Boolean',
- 'GIImageSmall' => 'Boolean',
- 'GIDescriptionExtended' => 'HTMLText',
- 'SortOrder' => 'Text',
- 'GIImageLink' => 'Text',
- 'Category' => "Enum('Misc, Winter, Spring, Summer, Fall')",
- 'ShowPhotoGallery' => 'Boolean',
- 'CategoryName' => 'Text',
- 'StartDate' => 'Date',
- 'EndDate' => 'Date',
- 'OpenTableID' => 'Text',
- 'ShowOpentableForm' => 'Boolean',
- 'OpenTableRightCopy' => 'HTMLText',
- // Links for images that appear in the description, but before the text if present.
- 'DescImage1Link' => 'Text',
- 'DescImage2Link' => 'Text',
- 'DescImage3Link' => 'Text',
- 'DescImage4Link' => 'Text',
- 'DescImage5Link' => 'Text',
- 'DescImage6Link' => 'Text',
- );
- static $has_one = array (
- 'GIImage' => 'Image',
- 'PhotoGalleryFirstImage' => 'Image',
- 'Page' => 'Page',
- // Images that appear in the description, but before the text if present.
- 'DescImage1' => 'Image',
- 'DescImage2' => 'Image',
- 'DescImage3' => 'Image',
- 'DescImage4' => 'Image',
- 'DescImage5' => 'Image',
- 'DescImage6' => 'Image',
- 'OpenTableLogoImg' => 'Image',
- );
- // Determine whether this object is active (based on start/end dates).
- function getIsActive() {
- // Has this object's start date passed (or no start date)?
- $afterStartDate = true;
- if ($this->StartDate) {
- $startDate = new Date();
- $startDate->setValue($this->StartDate);
- $afterStartDate = $startDate->IsToday() || $startDate->InPast();
- }
- // Has this object's end date not come yet (or no end date)?
- $beforeEndDate = true;
- if ($this->EndDate) {
- $endDate = new Date();
- $endDate->setValue($this->EndDate);
- $beforeEndDate = $endDate->IsToday() || $endDate->InFuture();
- }
- // Rerturn true if we are within both the start date and end date.
- return $afterStartDate && $beforeEndDate;
- }
- public function getCMSFields_forPopup()
- {
- $startDateField = new Datefield('StartDate');
- $startDateField->setConfig('dateformat', 'M/d/YYYY');
- $endDateField = new Datefield('EndDate');
- $endDateField->setConfig('dateformat', 'M/d/YYYY');
- return new FieldSet(
- new NumericField('SortOrder'),
- new TextField('GITitle'),
- new TextField('GISubTitle'),
- new CheckboxField('GIDescriptionWrap'),
- new SimpleHtmlEditorField('GIDescription'),
- new CheckboxField('ShowOpentableForm'),
- new TextField('OpenTableID'),
- new ImageField('OpenTableLogoImg'),
- new SimpleHtmlEditorField('OpenTableRightCopy'),
- new CheckboxField('GIDescriptionExtendedHide'),
- new SimpleHtmlEditorField('GIDescriptionExtended'),
- new CheckboxField('GIImageSmall'),
- new ImageField('GIImage'),
- new TextField('GIImageLink'),
- new DropdownField('Category','Category', singleton('GraphicAndInfoModule')->dbObject('Category')->enumValues()),
- new CheckboxField('ShowPhotoGallery'),
- new ImageField('PhotoGalleryFirstImage'),
- new TextField('CategoryName'),
- $startDateField,
- $endDateField,
- // Editing controls for images that appear in the description.
- new ImageField('DescImage1'),
- new TextField('DescImage1Link'),
- new ImageField('DescImage2'),
- new TextField('DescImage2Link'),
- new ImageField('DescImage3'),
- new TextField('DescImage3Link'),
- new ImageField('DescImage4'),
- new TextField('DescImage4Link'),
- new ImageField('DescImage5'),
- new TextField('DescImage5Link'),
- new ImageField('DescImage6'),
- new TextField('DescImage6Link')
- );
- }
- // Add publishing capabilities to this data object.
- static $extensions = array(
- "Versioned('Stage', 'Live')"
- );
- /**
- * Publish the objects on Stage to Live.
- *
- * @param int $pageId The id of the page we're publishing.
- */
- public static function publish_on_page($pageId) {
- // Publish stage to live.
- self::mirror_on_page($pageId, 'Stage', 'Live');
- }
- /**
- * Revert the objects on Stage to match Live.
- *
- * @param int $pageId The id of the page we're reverting.
- */
- public static function revert_on_page($pageId) {
- // Revert stage to match live.
- self::mirror_on_page($pageId, 'Live', 'Stage');
- }
- /**
- * Mirror objects from one publishing stage to another.
- *
- * @param int $pageId The id of the page we're working on.
- * @param string $fromStage The publishing stage we are copying objects from (e.g. "Stage").
- * @param string $toStage The publishing stage we are copying objects to (e.g. "Live").
- */
- private static function mirror_on_page($pageId, $fromStage, $toStage) {
- // What where clause will restrict us to working with objects on a page?
- $whereOnThisPage = "\"PageID\" = '$pageId'";
- // For this page, what are the objects on the stage we're coming from and going to?
- $fromObjects = Versioned::get_by_stage(get_called_class(), $fromStage, $whereOnThisPage);
- $toObjects = Versioned::get_by_stage(get_called_class(), $toStage, $whereOnThisPage);
- // Gather the ids of all modules on stage going to live.
- $fromIds = array();
- // Publish each object to its new destination.
- if ($fromObjects) {
- foreach($fromObjects as $fromObject) {
- // Remember every id we're publishing.
- array_push($fromIds, $fromObject->ID);
- $fromObject->publish($fromStage, $toStage);
- }
- }
- // Remove every object at the destination that didn't exist at the source stage.
- if ($toObjects) {
- foreach($toObjects as $toObject) {
- // Is there a from version of this object?
- if ( ! in_array($toObject->ID, $fromIds)) {
- // This object not in the original source objects.
- // Remove it since we only want destination objects that exsted in our source stage.
- $toObject->deleteFromStage($toStage);
- }
- }
- }
- }
- /**
- * Check if two publishing stages are in the same state.
- *
- * @param int $pageId The id of the page we're working on.
- * @param string $stageA One publishing stage (e.g. "Stage").
- * @param string $stageB Another publishing stage (e.g. "Live").
- */
- public static function stages_differ($pageId, $stageA, $stageB) {
- // What where clause will restrict us to working with objects on a page?
- $whereOnThisPage = "\"PageID\" = '$pageId'";
- // If the stages are the same, they have to be identical.
- if ($stageA == $stageB) {
- // The stages do not differ.
- return false;
- }
- // What are the objects from both stages?
- $objectsA = Versioned::get_by_stage(get_called_class(), $stageA, $whereOnThisPage);
- $objectsB = Versioned::get_by_stage(get_called_class(), $stageB, $whereOnThisPage);
- // Return whether any differences are found.
- $differenceFound = false;
- // Check each object to see if it differs between the stages.
- if ($objectsA) {
- foreach($objectsA as $objectA) {
- // If there are no differences found, try to find one.
- if ( ! $differenceFound) {
- $differenceFound = $objectA->stagesDiffer($stageA, $stageB);
- }
- }
- }
- // Check each object to see if it differs between the stages.
- if ($objectsB) {
- foreach($objectsB as $objectB) {
- // If there are no differences found, try to find one.
- if ( ! $differenceFound) {
- $differenceFound = $objectB->stagesDiffer($stageA, $stageB);
- }
- }
- }
- // Return whether we found differences.
- return $differenceFound;
- }
- /**
- * Duplicate the data objects with the page.
- *
- * @param int $fromPageId The id of the page we're duplicating.
- * @param int $toPageId The id of the page created as the duplicate.
- * @param int $doWrite True, if we want to create new rows in the database.
- */
- public static function duplicate_between_pages($fromPageId, $toPageId, $doWrite) {
- // What where clause will restrict us to working with objects on a page?
- $whereOnThisPage = "\"PageID\" = '$fromPageId'";
- // What are the objects in the draft mode of the source page?
- $sourceObjects = Versioned::get_by_stage(get_called_class(), "Stage", $whereOnThisPage);
- // Duplicate each object to new page.
- if ($sourceObjects) {
- foreach($sourceObjects as $sourceObject) {
- // Clone the data object (still has the old PageId).
- $clonedObject = $sourceObject->duplicate($doWrite);
- // Did we actually create a new database row?
- if ($doWrite) {
- // Give it the new page.
- $clonedObject->PageID = $toPageId;
- // Update the database.
- $clonedObject->write();
- }
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement