Skip to content
This repository has been archived by the owner on Jan 10, 2021. It is now read-only.

Commit

Permalink
Cleaned up some class definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Glasl committed Sep 17, 2013
1 parent 783dec7 commit 65e5ce1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 50 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013, NGlasl
Copyright (c) 2013, Nathan Glasl.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -11,7 +11,7 @@ are permitted provided that the following conditions are met:
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of the {organization} nor the names of its
Neither the name of the organisation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
silverstripe-media
silverstripe-mediawesome
==================
5 changes: 3 additions & 2 deletions _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* @author Nathan Glasl <[email protected]>
*/

if(!defined('MEDIA_PATH')) {
define('MEDIA_PATH', rtrim(basename(dirname(__FILE__))));
if(!defined('MEDIAWESOME_PATH')) {
define('MEDIAWESOME_PATH', rtrim(basename(dirname(__FILE__))));
}
MediaType::apply_required_extensions();

/*
* EXAMPLE: Adding default pages and their attributes.
Expand Down
4 changes: 2 additions & 2 deletions code/dataobjects/MediaAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class MediaAttribute extends DataObject {

private static $db = array(
'Title' => 'VARCHAR(255)',
'LinkID' => 'Int',
'Content' => 'HTMLText'
'Content' => 'HTMLTEXT',
'LinkID' => 'INT'
);

private static $has_one = array(
Expand Down
46 changes: 33 additions & 13 deletions code/dataobjects/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@ class MediaType extends DataObject {
'Title' => 'VARCHAR(255)'
);

private static $pageDefaults = array(
private static $page_defaults = array(
'NewsPage',
'Event',
'Publication',
'MediaRelease',
'Speech',
'Blog',
'MediaHolder'
'Blog'
);

private static $customDefaults = array();
private static $custom_defaults = array(
);

public static function addDefaults($objects) {
public static function add_default($type) {

//merge any new media type customisation

if(is_array($objects)) {
self::$customDefaults = array_merge(self::$customDefaults, $objects);
}
self::$custom_defaults[] = $type;
}

public static function apply_required_extensions() {

Object::add_extension('Page', 'PageChildrenExtension');
Config::inst()->update('MediaHolder', 'icon', MEDIAWESOME_PATH . '/images/holder.png');
Config::inst()->update('MediaPage', 'icon', MEDIAWESOME_PATH . '/images/page.png');
}

public function requireDefaultRecords() {
Expand All @@ -33,7 +38,7 @@ public function requireDefaultRecords() {

// News Page, Event, Publication, Media Release, Speech, Blog, Media Holder.

$combinedDefaults = array_unique(array_merge(self::$pageDefaults, self::$customDefaults));
$combinedDefaults = array_unique(array_merge(self::$page_defaults, self::$custom_defaults));
foreach($combinedDefaults as $default) {

// Create the default media page types.
Expand Down Expand Up @@ -65,6 +70,11 @@ public function getCMSFields() {
if($this->canEdit()) {
$fields->addFieldToTab('Root.AdditionalAttributes', $gridfield = GridField::create('AdditionalAttributes', 'Additional Attributes', $output, GridFieldConfig_RecordEditor::create()->removeComponentsByType('GridFieldDeleteAction'))->setModelClass('MediaAttribute'));
}
else {
$fields->addFieldToTab('Root.Main', LiteralField::create('Notification',
'<div>Additional Attributes will be available here once a Media Page has been created</div>'
));
}
}

return $fields;
Expand All @@ -77,10 +87,20 @@ public function validate() {
}

public function canEdit($member = null) {
$objects = MediaAttribute::get()->innerJoin('MediaPage', 'MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where("MediaType.Title = '" . Convert::raw2sql($this->Title) . "'");

$a = $objects->first() ? true : false;
return $a;
$params = Controller::curr()->getRequest()->requestVars();
$url = $params['url'];
$matches = array();
$result = preg_match('#MediaTypes/item/new#', $url, $matches);
if($result && Permission::check('ADMIN', 'any', $member)) {
return true;
}
else {
//only need pages?
$objects = MediaAttribute::get()->innerJoin('MediaPage', 'MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where("MediaType.Title = '" . Convert::raw2sql($this->Title) . "'");
$pages = MediaPage::get()->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where("MediaType.Title = '" . Convert::raw2sql($this->Title) . "'");
$a = ($objects->first() || $pages->first()) ? true : false;
return $a;
}
}

}
14 changes: 14 additions & 0 deletions code/extensions/PageChildrenExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Extension to validate media page and holder placement.
* @author Nathan Glasl <[email protected]>
*/

class PageChildrenExtension extends DataExtension {

private static $allowed_children = array(
'Page'
);

}
4 changes: 1 addition & 3 deletions code/pages/MediaHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

class MediaHolder extends Page {

private static $icon = 'silverstripe-media/images/listing.png';

private static $description = 'HOLDER FOR: News Page, Event, Publication, Media Release, Speech, Blog';
private static $description = '<strong>Holds:</strong> Blogs, Events, Media Releases, News, Publications, Speeches <strong>or Custom Media</strong>';

private static $has_one = array(
'MediaType' => 'MediaType'
Expand Down
55 changes: 28 additions & 27 deletions code/pages/MediaPage.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php

class MediaPage extends Page {
class MediaPage extends SiteTree {

private static $icon = 'silverstripe-media/images/page.png';

private static $description = 'News Page, Event, Publication, Media Release, Speech, Blog';
private static $description = 'Blog, Event, Media Release, News, Publication, Speech <strong>or Custom Media</strong>';

private static $db = array(
'ExternalLink' => 'VARCHAR(255)',
'External' => 'VARCHAR(255)',
'Abstract' => 'TEXT',
'Date' => 'Datetime'
);
Expand All @@ -25,17 +23,17 @@ class MediaPage extends Page {
);

private static $many_many = array(
'Attachments' => 'File',
'Images' => 'Image'
'Images' => 'Image',
'Attachments' => 'File'
);

private static $can_be_root = false;

private static $allowed_children = "none";
private static $allowed_children = 'none';

private static $default_parent = 'MediaHolder';

private static $pageDefaults = array(
private static $page_defaults = array(
'NewsPage' => array(
'Author'
),
Expand All @@ -60,33 +58,36 @@ class MediaPage extends Page {
)
);

private static $customDefaults = array();
private static $custom_defaults = array(
);

public static function addDefaults($objects) {
public static function customise_defaults($objects) {

// merge nested array

if(is_array($objects)) {

// make sure we don't have an invalid entry
foreach($objects as $test) {
if(!is_array($test)) {

foreach($objects as $temporary) {
if(!is_array($temporary)) {
return;
}
}

$pages = array();
$merge = array();
foreach($objects as $page => $attribute) {
if(!in_array($page, $pages) && !array_key_exists($page, self::$customDefaults) && ($page !== 'MediaHolder')) {
$pages[] = $page;
$merge[$page] = $attribute;
// a manual array unique since that doesn't work with nested arrays

$output = array();
foreach($objects as $type => $attribute) {
if(!array_key_exists($type, self::$custom_defaults) && !array_key_exists($type, $output) && ($type !== 'MediaHolder')) {
$output[$type] = $attribute;

// add these new media types too

MediaType::add_default($type);
}
}
self::$customDefaults = array_merge(self::$customDefaults, $merge);

// add these new media types without requiring additional configuration
MediaType::addDefaults($pages);
self::$custom_defaults = array_merge(self::$custom_defaults, $output);
}
}

Expand Down Expand Up @@ -118,12 +119,12 @@ public function onBeforeWrite() {
$type = $type->exists() ? $type->Title : null;

$combinedDefaults = array();
foreach(self::$customDefaults as $key => $default) {
if(!array_key_exists($key, self::$pageDefaults)) {
foreach(self::$custom_defaults as $key => $default) {
if(!array_key_exists($key, self::$page_defaults)) {
$combinedDefaults[$key] = $default;
}
}
$combinedDefaults = array_merge(self::$pageDefaults, $combinedDefaults);
$combinedDefaults = array_merge(self::$page_defaults, $combinedDefaults);

if(!$this->MediaAttributes()->exists()) {

Expand Down Expand Up @@ -164,7 +165,7 @@ public function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Main', ReadonlyField::create('Type', 'Type', $this->MediaType()->Title), 'Title');
$fields->addFieldToTab('Root.Main', TextField::create('ExternalLink'), 'Title');
$fields->addFieldToTab('Root.Main', TextField::create('External', 'External Link'), 'Title');

$fields->addFieldToTab('Root.Main', $dateTimeField = new DatetimeField('Date'), 'Content');
$dateTimeField->getDateField()->setConfig('showcalendar', true);
Expand Down
Binary file renamed images/listing.png → images/holder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 65e5ce1

Please sign in to comment.