Version: current

      1 <?php
      2 /**
      3  * Страница группы объектов
      4  * Собрержит объекты (MapObject), редактирование привязанных объектов в админке
      5  *
      6  * @author menedem
      7  *
      8  */
      9
     10 class MapObjectGroup extends Page {
     11
     12     static $can_be_root = false;
     13     static $default_parent = "MapPage";
     14     static $allowed_children = array('MapObject');
     15
     16     static $show_as_page = false;
     17
     18     static $db = array(
     19         'ShowOnDefault' => 'Boolean',
     20         'IconType' => 'Int',
     21         'YandexMarker' => 'Varchar'
     22     );
     23
     24     static $has_one = array(
     25         'Image' => 'Image'
     26     );
     27
     28     static $defaults = array(
     29         'ShowInMenus' => 0,
     30         'ShowInSearch' => 0,
     31         'ShowInSiteMap' => 0,
     32         'IconType' => 2,
     33         'YandexMarker' => 'blueDotIcon'
     34     );
     35
     36     static $subpage_children = 'MapObject';
     37
     38     static $yandexMarkers = array(
     39         'blueDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotblue.png" alt="" />',
     40         'darkblueDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotdarkblue.png" alt="" />',
     41         'darkgreenDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotdarkgreen.png" alt="" />',
     42         'darkorangeDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotdarkorange.png" alt="" />',
     43         'greenDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotgreen.png" alt="" />',
     44         'greyDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotgrey.png" alt="" />',
     45         'lightblueDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotlightblue.png" alt="" />',
     46         'nightDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotnight.png" alt="" />',
     47         'orangeDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotorange.png" alt="" />',
     48         'pinkDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotpink.png" alt="" />',
     49         'redDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotred.png" alt="" />',
     50         'violetDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotviolet.png" alt="" />',
     51         'whiteDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotwhite.png" alt="" />',
     52         'yellowDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotyellow.png" alt="" />',
     53         'brownDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotbrown.png" alt=""/>',
     54         'blackDotIcon' => '<img class="MapObjectGroup_ymarker" src="/map_objects/img/dotblack.png" alt="" />',
     55     );
     56
     57     static function allow_show($allow = true) {
     58         self::$show_as_page = $allow;
     59     }
     60
     61     function canCreate($member = null) {
     62         $can = parent::canCreate($member);
     63         if (!SiteConfig::current_site_config()->isNeedMapApiKey()) {
     64             $can = false;
     65         }
     66         return $can;
     67     }
     68
     69     function getCMSFields() {
     70         $fields = parent::getCMSFields();
     71
     72         $sc = SiteConfig::current_site_config();
     73         if (!$sc->isNeedMapApiKey()) {
     74             $fields->addFieldToTab('Root.Content.Main', new LiteralField('NoMapApiKey', _t("SiteConfig.NoMapApiKeyMessage")));
     75         }
     76
     77         $fields->addFieldToTab("Root.Content.Main", new CheckboxField('ShowOnDefault', $this->FieldLabel('ShowOnDefault')), 'Content');
     78
     79         $fields->addFieldToTab("Root.Content.Main", new HeaderField('IconHeader', $this->FieldLabel('IconType')), 'Content');
     80         $fields->addFieldToTab('Root.Content.Main', $type = new SelectionGroup('IconType', array(
     81             '1//' . _t('MapObjectGroup.db_IconType_Icon', 'Custom Icon') => new ImageField('Image', $this->FieldLabel('Image')),
     82             '2//' . _t('MapObjectGroup.db_IconType_Marker', 'Standart') => new OptionsetField('YandexMarker','', self::$yandexMarkers)
     83         )), 'Content');
     84
     85         if (!self::$show_as_page) {
     86             $fields->removeByName('Content', true);
     87             $fields->removeByName('Metadata');
     88         }
     89
     90         $fields->findOrMakeTab('Root.Content.tabSubPages', _t('MapObjectGroup.Objects', 'Objects'));
     91
     92         $subClass = self::$subpage_children;
     93         $objectsTable = new SubpageListField('Subpages', $this, $subClass);
     94         $objectsTable->Sortable = true;
     95         $url = '<a href=\"admin/show/$ID\">$value</a>';
     96         Requirements::css('map_objects/css/admin_group.css');
     97         $objectsTable->setFieldFormatting(array_combine(array_keys(singleton($subClass)->summaryFields()), array_fill(0, count(singleton($subClass)->summaryFields()), $url)));
     98         $objectsTable->setPageSize(100);
     99         //$objectsTable->setPermissions(array('add'));
    100         $fields->addFieldToTab('Root.Content.tabSubPages', $objectsTable);
    101
    102         // ??? убрать ненужные поля из админки - надо уточнить
    103
    104         return $fields;
    105     }
    106
    107     /**
    108      * Возвращает список объектов группы
    109      * вообще можно было использовать AllChildren, но почему-то не работает
    110      *
    111      * @return DataObjectSet
    112      */
    113     function Objects() {
    114         // Status = 'Published'  - можно не использовать т.к. на опубликованном сайте других нет, на тестовом будут видны все
    115         // и присваивать переменной если ее потом не обрабатывать нет нужды
    116         return DataObject::get('MapObject', "ParentID = {$this->ID}");
    117     }
    118
    119     function onBeforeWrite() {
    120         if ($this->isChanged('Title')) {
    121             $this->URLSegment = $this->generateURLSegment($this->Title);
    122         }
    123         parent::onBeforeWrite();
    124     }
    125 }
    126
    127 class MapObjectGroup_Controller extends Page_Controller {
    128
    129     function init() {
    130         if(!MapObjectGroup::$show_as_page) {
    131             Director::redirect($this->Parent->Link());
    132         }
    133         parent::init();
    134     }
    135 }

[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. -