Skip to content

Commit

Permalink
Name toolbar field
Browse files Browse the repository at this point in the history
  • Loading branch information
KINKCreative committed Feb 21, 2017
1 parent 8286606 commit e6cb374
Showing 1 changed file with 61 additions and 61 deletions.
122 changes: 61 additions & 61 deletions code/DataObjects/DataObjectAsPage.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?php
/*
* Base class for DataObjects that behave like pages
*
*
*/
class DataObjectAsPage extends DataObject {

/**
* @var defind the listing page class name
*/
private static $listing_page_class = 'DataObjectAsPageHolder';

private static $db = array (
'URLSegment' => 'Varchar(100)',
'Title' => 'Varchar(255)',
'MetaTitle' => 'Varchar(255)',
'MetaDescription' => 'Varchar(255)',
'Content' => 'HTMLText'
);

private static $defaults = array(
'Title'=>'New Item',
'URLSegment' => 'new-item'
);

private static $summary_fields = array(
'Title' => 'Title',
'URLSegment' => 'URLSegment'
Expand All @@ -34,7 +34,7 @@ class DataObjectAsPage extends DataObject {
'value' => 'URLSegment'
)
);

private static $default_sort = 'Created DESC';

/**
Expand Down Expand Up @@ -91,11 +91,11 @@ public function canView($member = null)
if($extended !==null) return $extended;

//If this is draft check for permissions to view draft content
//getSearchResultItem is needed to ensure unpublished items don't show up in search results
//getSearchResultItem is needed to ensure unpublished items don't show up in search results
if($this->isVersioned && Versioned::current_stage() == 'Stage' && $this->Status == 'Draft')
{
return Permission::checkMember($member,'VIEW_DRAFT_CONTENT');
}
}
elseif(Controller::curr()->hasMethod("canView"))
{
//Otherwise return the parent listing pages view permission
Expand Down Expand Up @@ -147,48 +147,48 @@ public function canDeleteFromLive($member = null)
*
* @return FieldList The list of CMS Fields
*/
public function getCMSFields()
public function getCMSFields()
{
$fields = parent::getCMSFields();

//Add the status/view link
if($this->ID)
{
if($this->isVersioned)
{
$status = $this->getStatus();
$status = $this->getStatus();

$color = '#E88F31';
$links = sprintf(
"<a target=\"_blank\" class=\"ss-ui-button\" data-icon=\"preview\" href=\"%s\">%s</a>", $this->Link() . '?stage=Stage', 'Draft'
);

if($status == 'Published')
{
$color = '#000';
$links .= sprintf(
"<a target=\"_blank\" class=\"ss-ui-button\" data-icon=\"preview\" href=\"%s\">%s</a>", $this->Link() . '?stage=Live', 'Published'
);

if($this->hasChangesOnStage())
{
$status .= ' (changed)';
$color = '#428620';
}
}

$statusPill = '<h3 class="doapTitle" style="background: '.$color.';">'. $status . '</h3>';
}
else
{
$links = sprintf(
"<a target=\"_blank\" class=\"ss-ui-button\" data-icon=\"preview\" href=\"%s\">%s</a>", $this->Link() . '?stage=Stage', 'View'
);
);

$statusPill = "";
}

$fields->addFieldToTab('Root.Main', new LiteralField('',
$fields->addFieldToTab('Root.Main', new LiteralField('DOAPToolbar',
'<div class="doapToolbar">
' . $statusPill . '
<p class="doapViewLinks">
Expand All @@ -205,37 +205,37 @@ public function getCMSFields()
$fields->removeFieldFromTab('Root.Main', 'MetaTitle');
$fields->removeFieldFromTab('Root.Main', 'MetaDescription');
$fields->removeByName('Versions');
$fields->addFieldToTab('Root.Main', new TextField('Title'));

$fields->addFieldToTab('Root.Main', new TextField('Title'));

if($this->ID)
{
$urlsegment = new SiteTreeURLSegmentField("URLSegment", $this->fieldLabel('URLSegment'));

if($this->getListingPage()) {
$prefix = $this->getListingPage()->AbsoluteLink('show').'/';
} else {
$prefix = Director::absoluteBaseURL() . 'listing-page/show/';
}
$urlsegment->setURLPrefix($prefix);

$helpText = _t('SiteTreeURLSegmentField.HelpChars', ' Special characters are automatically converted or removed.');
$urlsegment->setHelpText($helpText);
$fields->addFieldToTab('Root.Main', $urlsegment);
}

$fields->addFieldToTab('Root.Main', new HTMLEditorField('Content'));
$fields->addFieldToTab('Root.Main', new HTMLEditorField('Content'));

$fields->addFieldToTab('Root.Main',new ToggleCompositeField('Metadata', 'Metadata',
array(
new TextField("MetaTitle", $this->fieldLabel('MetaTitle')),
new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription'))
)
));

//$fields->push(new HiddenField('PreviewURL', 'Preview URL', $this->StageLink()));
//$fields->push(new TextField('CMSEditURL', 'Preview URL', $this->CMSEditLink()));

return $fields;
}

Expand All @@ -247,7 +247,7 @@ public static function enable_versioning()
DataObject::add_extension('DataObjectAsPage','VersionedDataObjectAsPage');
DataObject::add_extension('DataObjectAsPage',"Versioned('Stage', 'Live')");
}

/**
* Check if the DOAP is versioned
*
Expand All @@ -261,36 +261,36 @@ public function getisVersioned()
/**
* Produce the correct breadcrumb trail for use on the DataObject Item Page
*/
public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false)
public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false)
{
$page = Controller::curr();
$pages = array();

$pages[] = $this;

while(
$page
&& (!$maxDepth || count($pages) < $maxDepth)
$page
&& (!$maxDepth || count($pages) < $maxDepth)
&& (!$stopAtPageType || $page->ClassName != $stopAtPageType)
) {
if($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) {
if($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) {
$pages[] = $page;
}

$page = $page->Parent;
}

$template = new SSViewer('BreadcrumbsTemplate');

return $template->process($this->customise(new ArrayData(array(
'Pages' => new ArrayList(array_reverse($pages))
))));
}

/**
* Generate custom metatags to display on the DataObject Item page
*/
public function MetaTags($includeTitle = true)
*/
public function MetaTags($includeTitle = true)
{
$tags = "";
if($includeTitle === true || $includeTitle == 'true') {
Expand All @@ -317,7 +317,7 @@ public function getStatus()
{
if($this->isVersioned)
{
return $this->isPublished() ? "Published" : "Draft";
return $this->isPublished() ? "Published" : "Draft";
}
else
{
Expand All @@ -330,29 +330,29 @@ public function getStatus()
*
* @return boolean True if this page has been published.
*/
public function isPublished()
public function isPublished()
{
return (DB::query("SELECT \"ID\" FROM \"DataObjectAsPage_Live\" WHERE \"ID\" = $this->ID")->value())
? true
: false;
}

/**
* Check whether this DO has changes which are not published
*/
public function hasChangesOnStage()
{
$latestPublishedVersion = $this->get_versionnumber_by_stage('DataObjectAsPage', 'Live', $this->ID);
$latestVersion = $this->get_versionnumber_by_stage('DataObjectAsPage', 'Stage', $this->ID);

return ($latestPublishedVersion < $latestVersion);
}

/**
* Get the listing page to view this Event on (used in Link functions below)
*/
public function getListingPage(){

$listingClass = $this->stat('listing_page_class');
$controllerClass = $listingClass . "_Controller";

Expand All @@ -364,10 +364,10 @@ public function getListingPage(){
{
$listingPage = $listingClass::get()->First();
}
return $listingPage;

return $listingPage;
}

/**
* Generate the link to this DataObject Item page
*/
Expand All @@ -380,10 +380,10 @@ public function Link($action = null)
if($listingPage = $item->getListingPage())
{
return Controller::join_links($listingPage->Link(), 'show', $item->URLSegment, $action);
}
}
}
}

/**
* Create an absolute link to the DOAP
*
Expand All @@ -397,7 +397,7 @@ public function AbsoluteLink($action = null)
return Controller::join_links($listingPage->AbsoluteLink(), 'show', $this->URLSegment, $action);
}
}

/**
* Return the correct linking mode, for use in menus
*/
Expand All @@ -421,15 +421,15 @@ public function LinkingMode()
public function onBeforeWrite()
{
parent::onBeforeWrite();

$defaults = $this->config()->defaults;

// If there is no URLSegment set, generate one from Title
if((!$this->URLSegment || $this->URLSegment == $defaults['URLSegment']) && $this->Title != $defaults['Title'])
{
$this->URLSegment = $this->generateURLSegment($this->Title);
}
else if($this->isChanged('URLSegment'))
}
else if($this->isChanged('URLSegment'))
{
// Make sure the URLSegment is valid for use in a URL
$segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
Expand All @@ -448,8 +448,8 @@ public function onBeforeWrite()
$URLSegment = $this->URLSegment;
$ID = $this->ID;

while($this->LookForExistingURLSegment($URLSegment, $ID))
{
while($this->LookForExistingURLSegment($URLSegment, $ID))
{
$URLSegment = preg_replace('/-[0-9]+$/', null, $URLSegment) . '-' . $count;
$count++;
}
Expand All @@ -467,29 +467,29 @@ public function LookForExistingURLSegment($URLSegment, $ID)
'URLSegment',$URLSegment
)->exclude('ID', $ID)->exists();
}

/**
* Generate a URL segment based on the title provided.
*
*
* If {@link Extension}s wish to alter URL segment generation, they can do so by defining
* updateURLSegment(&$url, $title). $url will be passed by reference and should be modified.
* $title will contain the title that was originally used as the source of this generated URL.
* This lets extensions either start from scratch, or incrementally modify the generated URL.
*
*
* @param string $title Page title.
* @return string Generated url segment
*/
public function generateURLSegment($title)
{
$filter = URLSegmentFilter::create();
$t = $filter->filter($title);

// Fallback to generic page name if path is empty (= no valid, convertable characters)
if(!$t || $t == '-' || $t == '-1') $t = "page-$this->ID";

// Hook for extensions
$this->extend('updateURLSegment', $t, $title);

return $t;
}
}

0 comments on commit e6cb374

Please sign in to comment.