Version: current

     1 <?php
     2
     3 class SeoOpenGraphPageDecorator extends DataObjectDecorator {
     4     function extraStatics() {
     5         return array(
     6             'db' => array(
     7                 'OgpCustomSetup' => 'Boolean', // Индивидуальные параметры для соц. сетей
     8                 'OgpTitle' => 'Varchar(200)', // Заголовок для соц. сетей
     9                 'OgpDescription' => 'Text', // Описание для соц. сетей]
    10                 'OgpAllImages' => 'Boolean', // Передавать в соц. сети все изображения
    11             ),
    12             'has_one' => array(
    13                 'SocialsImage' => 'Image', // Изображение для социальных сетей
    14             )
    15         );
    16     }
    17
    18     private static $ogpAllImagesClasses = array('Product');
    19
    20     static function getOgpAllImagesClasses() {
    21         return self::$ogpAllImagesClasses;
    22     }
    23
    24     static function setOgpAllImagesClasses($classes) {
    25         self::$ogpAllImagesClasses = $classes;
    26     }
    27
    28     function updateCMSFields(& $fields) {
    29         Requirements::javascript('seo_open_graph/javascript/open_graph_cms.js');
    30         Requirements::css('seo_open_graph/css/open_graph_cms.css');
    31
    32         $tab = $fields->findOrMakeTab('Root.Content.Metadata');
    33         $tab->push(new LiteralField('OgpSetup', _t('SeoOpenGraph.Setup')));
    34         $tab->push(new CheckboxField('OgpCustomSetup', $this->owner->FieldLabel('OgpCustomSetup')));
    35         $extraClass = 'seo_cms_field';
    36         if (!$this->owner->OgpCustomSetup) {
    37             $extraClass .= ' seo_cms_field_hide';
    38         }
    39         $tab->push($f = new TextField('OgpTitle', $this->owner->FieldLabel('OgpTitle')));
    40         $f->addExtraClass($extraClass);
    41         $tab->push($f = new TextAreaField('OgpDescription', $this->owner->FieldLabel('OgpDescription')));
    42         $f->addExtraClass($extraClass);
    43         $tab->push($f = new ImageField('SocialsImage', $this->owner->FieldLabel('SocialsImage')));
    44         $f->addExtraClass($extraClass);
    45
    46         if (in_array($this->owner->ClassName, self::getOgpAllImagesClasses())) {
    47             $tab->push($f = new CheckboxField('OgpAllImages', $this->owner->FieldLabel('OgpAllImages')));
    48             $f->addExtraClass($extraClass);
    49         }
    50     }
    51
    52     function MetaTags(& $tags) {
    53         if ($this->owner->ClassName == 'Product') {
    54             $tags .= "<meta property=\"og:type\" content=\"product\" />\n";
    55         } else {
    56             $tags .= "<meta property=\"og:type\" content=\"website\" />\n";
    57         }
    58
    59         if ($this->owner->OgpCustomSetup && $this->owner->OgpTitle) {
    60             $title = $this->owner->OgpTitle;
    61         } else {
    62             $title = (($this->owner->MetaTitle) ? $this->owner->MetaTitle : $this->owner->Title);
    63         }
    64         $tags .= "<meta property=\"og:title\" content=\"" . $title . "\" />\n";
    65
    66
    67         if ($this->owner->OgpCustomSetup && $this->owner->OgpDescription) {
    68             $tags .= "<meta property=\"og:description\" content=\"" . Convert::raw2att($this->owner->OgpDescription) . "\" />\n";
    69         } else {
    70             $tags .= "<meta property=\"og:description\" content=\"" . Convert::raw2att($this->owner->MetaDescription) . "\" />\n";
    71         }
    72
    73         if ($this->owner->OgpCustomSetup && $this->owner->SocialsImageID && ($image = $this->owner->SocialsImage())) {
    74             $tags .= "<meta property=\"og:image\" content=\"" . Convert::raw2att($image->Link()) . "\" />\n";
    75         } elseif ($this->owner->PhotoID && ($image = $this->owner->Photo())) {
    76             $tags .= "<meta property=\"og:image\" content=\"" . Convert::raw2att($image->Link()) . "\" />\n";
    77         } elseif (($this->owner->ClassName == 'PhotoAlbumPage') && ($image = $this->owner->AlbumCover()) && $image->ID) {
    78             $tags .= "<meta property=\"og:image\" content=\"" . Convert::raw2att($image->Link()) . "\" />\n";
    79         } elseif (SiteConfig::current_site_config()->SocialsImageID && ($image = SiteConfig::current_site_config()->SocialsImage())) {
    80             $tags .= "<meta property=\"og:image\" content=\"" . Convert::raw2att($image->Link()) . "\" />\n";
    81         }
    82
    83         if ($this->owner->OgpCustomSetup && $this->owner->OgpAllImages) {
    84             if ($this->owner->Photos()->Count()) {
    85                 foreach($this->owner->Photos() as $photo) {
    86                     $tags .= "<meta property=\"og:image\" content=\"" . Convert::raw2att($photo->Photo()->Link()) . "\" />\n";
    87                 }
    88             }
    89         }
    90
    91         if (isset($_SERVER['REQUEST_URI'])) {
    92             $link = Director::absoluteURL($_SERVER['REQUEST_URI']);
    93         } else {
    94             $link = $this->owner->AbsoluteLink();
    95         }
    96         $tags .= "<meta property=\"og:url\" content=\"" . $link . "\" />\n"; //Convert::raw2att(
    97     }
    98 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -