Advertisement
Guest User

Untitled

a guest
Jul 8th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. <?php
  2. class Page extends SiteTree {
  3.  
  4.     private static $db = array(
  5.     );
  6.  
  7.     private static $has_one = array(
  8.         'Photo' => 'Image'
  9.     );
  10.  
  11.     public function getCMSFields() {
  12.         $fields = parent::getCMSFields();
  13.  
  14.         $fields->addFieldToTab('Root.Images', $photo = UploadField::create('Photo'));
  15.  
  16.         $photo->getValidator()->setAllowedExtensions(['png','gif','jpg','jpeg']);
  17.         $photo->setFolderName('page-headers');
  18.  
  19.         return $fields;
  20.     }
  21.  
  22.     public function HeaderImage(){
  23.         if($this->Photo()->exists()){
  24.             echo "I have an image<br>";
  25.             return $this->Photo();
  26.         }else{
  27.             echo "I don't have an image.<br>";
  28.             return $this->getParentImage( $this );
  29.         }
  30.     }
  31.     private function getParentImage( Page $page ){
  32.         $parent = $page->Parent;
  33.         if($parent->Photo()->exists()){
  34.             return $parent->Photo();
  35.         }elseif( $parent ){
  36.             return $this->getParentImage( $parent );
  37.         }
  38.         return null;
  39.     }
  40.  
  41. }
  42. class Page_Controller extends ContentController {
  43.  
  44.     /**
  45.      * An array of actions that can be accessed via a request. Each array element should be an action name, and the
  46.      * permissions or conditions required to allow the user to access it.
  47.      *
  48.      * <code>
  49.      * array (
  50.      *     'action', // anyone can access this action
  51.      *     'action' => true, // same as above
  52.      *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
  53.      *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
  54.      * );
  55.      * </code>
  56.      *
  57.      * @var array
  58.      */
  59.     private static $allowed_actions = array (
  60.     );
  61.  
  62.     public function init() {
  63.         parent::init();
  64.         // You can include any CSS or JS required by your project here.
  65.         // See: http://doc.silverstripe.org/framework/en/reference/requirements
  66.     }
  67.  
  68. }
  69. =======================
  70. <div style="width: 100%; margin: 0px auto;">
  71.     <div style="margin: 0px auto;">
  72.         <% if $Photo %>
  73.         <h2>This is my photo. It's 1900X300</h2>
  74.        $Photo.CroppedImage(1900,300)
  75.        <% else %>
  76.        <h2>I do not have an image.</h2>
  77.        <% end_if %>
  78.        <% if $HeaderImage %>
  79.        <h2>This is my header image. It's 1900X300</h2>
  80.         $HeaderImage.CroppedImage(1900,300)
  81.         <% else %>
  82.         <h2>I do not have an image.</h2>
  83.         <% end_if %>
  84.     </div>
  85. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement