Advertisement
Guest User

GraphichAndInfoModule.php

a guest
May 12th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.85 KB | None | 0 0
  1. <?php
  2.  
  3.     class GraphicAndInfoModule extends DataObject
  4.     {
  5.         public static $db = array (
  6.             'GITitle' => 'Text',
  7.             'GISubTitle' => 'Text',
  8.             'GIDescription' => 'HTMLText',
  9.             'GIDescriptionWrap' => 'Boolean',
  10.             'GIDescriptionExtendedHide' => 'Boolean',
  11.             'GIImageSmall' => 'Boolean',
  12.             'GIDescriptionExtended' => 'HTMLText',
  13.             'SortOrder' => 'Text',
  14.             'GIImageLink' => 'Text',
  15.             'Category' => "Enum('Misc, Winter, Spring, Summer, Fall')",
  16.             'ShowPhotoGallery' => 'Boolean',
  17.             'CategoryName' => 'Text',
  18.             'StartDate' => 'Date',
  19.             'EndDate' => 'Date',
  20.             'OpenTableID' => 'Text',
  21.             'ShowOpentableForm' => 'Boolean',
  22.             'OpenTableRightCopy' => 'HTMLText',
  23.             // Links for images that appear in the description, but before the text if present.
  24.             'DescImage1Link' => 'Text',
  25.             'DescImage2Link' => 'Text',
  26.             'DescImage3Link' => 'Text',
  27.             'DescImage4Link' => 'Text',
  28.             'DescImage5Link' => 'Text',
  29.             'DescImage6Link' => 'Text',
  30.  
  31.         );
  32.  
  33.         static $has_one = array (
  34.             'GIImage' => 'Image',
  35.             'PhotoGalleryFirstImage' => 'Image',
  36.             'Page' => 'Page',
  37.             // Images that appear in the description, but before the text if present.
  38.             'DescImage1' => 'Image',
  39.             'DescImage2' => 'Image',
  40.             'DescImage3' => 'Image',
  41.             'DescImage4' => 'Image',
  42.             'DescImage5' => 'Image',
  43.             'DescImage6' => 'Image',
  44.             'OpenTableLogoImg' => 'Image',
  45.         );
  46.  
  47.         // Determine whether this object is active (based on start/end dates).
  48.         function getIsActive() {
  49.            
  50.             // Has this object's start date passed (or no start date)?
  51.             $afterStartDate = true;
  52.             if ($this->StartDate) {
  53.                 $startDate = new Date();
  54.                 $startDate->setValue($this->StartDate);
  55.                 $afterStartDate = $startDate->IsToday() || $startDate->InPast();
  56.             }
  57.            
  58.             // Has this object's end date not come yet (or no end date)?
  59.             $beforeEndDate = true;
  60.             if ($this->EndDate) {
  61.                 $endDate = new Date();
  62.                 $endDate->setValue($this->EndDate);
  63.                 $beforeEndDate = $endDate->IsToday() || $endDate->InFuture();
  64.             }
  65.            
  66.             // Rerturn true if we are within both the start date and end date.
  67.             return $afterStartDate && $beforeEndDate;
  68.         }
  69.  
  70.         public function getCMSFields_forPopup()
  71.         {
  72.        
  73.             $startDateField = new Datefield('StartDate');
  74.             $startDateField->setConfig('dateformat', 'M/d/YYYY');
  75.  
  76.         $endDateField = new Datefield('EndDate');
  77.             $endDateField->setConfig('dateformat', 'M/d/YYYY');
  78.            
  79.             return new FieldSet(
  80.  
  81.                 new NumericField('SortOrder'),
  82.                 new TextField('GITitle'),
  83.                 new TextField('GISubTitle'),
  84.                 new CheckboxField('GIDescriptionWrap'),
  85.                 new SimpleHtmlEditorField('GIDescription'),
  86.                 new CheckboxField('ShowOpentableForm'),
  87.                 new TextField('OpenTableID'),
  88.                 new ImageField('OpenTableLogoImg'),
  89.                 new SimpleHtmlEditorField('OpenTableRightCopy'),
  90.                 new CheckboxField('GIDescriptionExtendedHide'),
  91.                 new SimpleHtmlEditorField('GIDescriptionExtended'),
  92.                 new CheckboxField('GIImageSmall'),
  93.                 new ImageField('GIImage'),
  94.                 new TextField('GIImageLink'),
  95.                 new DropdownField('Category','Category', singleton('GraphicAndInfoModule')->dbObject('Category')->enumValues()),
  96.                 new CheckboxField('ShowPhotoGallery'),
  97.                 new ImageField('PhotoGalleryFirstImage'),
  98.                 new TextField('CategoryName'),
  99.                 $startDateField,
  100.                 $endDateField,
  101.  
  102.                 // Editing controls for images that appear in the description.
  103.                 new ImageField('DescImage1'),
  104.                 new TextField('DescImage1Link'),
  105.                 new ImageField('DescImage2'),
  106.                 new TextField('DescImage2Link'),
  107.                 new ImageField('DescImage3'),
  108.                 new TextField('DescImage3Link'),
  109.                 new ImageField('DescImage4'),
  110.                 new TextField('DescImage4Link'),
  111.                 new ImageField('DescImage5'),
  112.                 new TextField('DescImage5Link'),
  113.                 new ImageField('DescImage6'),
  114.                 new TextField('DescImage6Link')
  115.  
  116.             );
  117.         }
  118.        
  119.         // Add publishing capabilities to this data object.
  120.         static $extensions = array(
  121.            "Versioned('Stage', 'Live')"
  122.         );
  123.        
  124.         /**
  125.          * Publish the objects on Stage to Live.
  126.          *
  127.          * @param int $pageId The id of the page we're publishing.
  128.          */
  129.         public static function publish_on_page($pageId) {
  130.        
  131.             // Publish stage to live.
  132.             self::mirror_on_page($pageId, 'Stage', 'Live');
  133.            
  134.         }
  135.  
  136.         /**
  137.          * Revert the objects on Stage to match Live.
  138.          *
  139.          * @param int $pageId The id of the page we're reverting.
  140.          */
  141.         public static function revert_on_page($pageId) {
  142.        
  143.             // Revert stage to match live.
  144.             self::mirror_on_page($pageId, 'Live', 'Stage');
  145.            
  146.         }
  147.        
  148.         /**
  149.          * Mirror objects from one publishing stage to another.
  150.          *
  151.          * @param int $pageId The id of the page we're working on.
  152.          * @param string $fromStage The publishing stage we are copying objects from (e.g. "Stage").
  153.          * @param string $toStage The publishing stage we are copying objects to (e.g. "Live").
  154.          */
  155.         private static function mirror_on_page($pageId, $fromStage, $toStage) {
  156.        
  157.             // What where clause will restrict us to working with objects on a page?
  158.             $whereOnThisPage = "\"PageID\" = '$pageId'";
  159.            
  160.             // For this page, what are the objects on the stage we're coming from and going to?
  161.             $fromObjects = Versioned::get_by_stage(get_called_class(), $fromStage, $whereOnThisPage);
  162.             $toObjects = Versioned::get_by_stage(get_called_class(), $toStage, $whereOnThisPage);
  163.        
  164.             // Gather the ids of all modules on stage going to live.
  165.             $fromIds = array();
  166.            
  167.             // Publish each object to its new destination.
  168.             if ($fromObjects) {
  169.                 foreach($fromObjects as $fromObject) {
  170.                
  171.                     // Remember every id we're publishing.
  172.                     array_push($fromIds, $fromObject->ID);
  173.  
  174.                     $fromObject->publish($fromStage, $toStage);
  175.                 }
  176.             }
  177.  
  178.             // Remove every object at the destination that didn't exist at the source stage.
  179.             if ($toObjects) {
  180.                 foreach($toObjects as $toObject) {
  181.    
  182.                     // Is there a from version of this object?
  183.                     if ( ! in_array($toObject->ID, $fromIds)) {
  184.                    
  185.                         // This object not in the original source objects.
  186.                         // Remove it since we only want destination objects that exsted in our source stage.
  187.                         $toObject->deleteFromStage($toStage);
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.  
  193.         /**
  194.          * Check if two publishing stages are in the same state.
  195.          *
  196.          * @param int $pageId The id of the page we're working on.
  197.          * @param string $stageA One publishing stage (e.g. "Stage").
  198.          * @param string $stageB Another publishing stage (e.g. "Live").
  199.          */
  200.         public static function stages_differ($pageId, $stageA, $stageB) {
  201.        
  202.             // What where clause will restrict us to working with objects on a page?
  203.             $whereOnThisPage = "\"PageID\" = '$pageId'";
  204.            
  205.             // If the stages are the same, they have to be identical.
  206.             if ($stageA == $stageB) {
  207.                 // The stages do not differ.
  208.                 return false;
  209.             }
  210.            
  211.             // What are the objects from both stages?
  212.             $objectsA = Versioned::get_by_stage(get_called_class(), $stageA, $whereOnThisPage);
  213.             $objectsB = Versioned::get_by_stage(get_called_class(), $stageB, $whereOnThisPage);
  214.        
  215.             // Return whether any differences are found.
  216.             $differenceFound = false;
  217.            
  218.             // Check each object to see if it differs between the stages.
  219.             if ($objectsA) {
  220.                 foreach($objectsA as $objectA) {
  221.  
  222.                     // If there are no differences found, try to find one.
  223.                     if ( ! $differenceFound) {
  224.                         $differenceFound = $objectA->stagesDiffer($stageA, $stageB);
  225.  
  226.                     }
  227.                 }
  228.             }
  229.            
  230.             // Check each object to see if it differs between the stages.
  231.             if ($objectsB) {
  232.                 foreach($objectsB as $objectB) {
  233.  
  234.                     // If there are no differences found, try to find one.
  235.                     if ( ! $differenceFound) {
  236.                         $differenceFound = $objectB->stagesDiffer($stageA, $stageB);
  237.                     }
  238.                 }
  239.             }
  240.  
  241.             // Return whether we found differences.
  242.             return $differenceFound;
  243.         }
  244.  
  245.         /**
  246.          * Duplicate the data objects with the page.
  247.          *
  248.          * @param int $fromPageId The id of the page we're duplicating.
  249.          * @param int $toPageId The id of the page created as the duplicate.
  250.          * @param int $doWrite True, if we want to create new rows in the database.
  251.          */
  252.         public static function duplicate_between_pages($fromPageId, $toPageId, $doWrite) {
  253.        
  254.             // What where clause will restrict us to working with objects on a page?
  255.             $whereOnThisPage = "\"PageID\" = '$fromPageId'";
  256.            
  257.             // What are the objects in the draft mode of the source page?
  258.             $sourceObjects = Versioned::get_by_stage(get_called_class(), "Stage", $whereOnThisPage);
  259.            
  260.             // Duplicate each object to new page.
  261.             if ($sourceObjects) {
  262.                 foreach($sourceObjects as $sourceObject) {
  263.                
  264.                     // Clone the data object (still has the old PageId).
  265.                     $clonedObject = $sourceObject->duplicate($doWrite);
  266.  
  267.                     // Did we actually create a new database row?
  268.                     if ($doWrite) {
  269.  
  270.                         // Give it the new page.
  271.                         $clonedObject->PageID = $toPageId;
  272.                    
  273.                         // Update the database.
  274.                         $clonedObject->write();
  275.                        
  276.                     }
  277.                 }
  278.             }
  279.         }
  280.  
  281.     }
  282.  
  283. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement