Skip to content

Commit

Permalink
Cleanup of Blueprints Utilities, add Delegates for the creation/updat…
Browse files Browse the repository at this point in the history
…e/deletion
  • Loading branch information
brendo committed Dec 23, 2010
1 parent c1eb2ea commit 79e8ad4
Showing 1 changed file with 152 additions and 92 deletions.
244 changes: 152 additions & 92 deletions symphony/content/content.blueprintsutilities.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
<?php

/**
* @package content
*/
/**
* The Utilities page allows Developers to create Utilities through the
* Symphony backend. Although most Developers will actually do this through
* an IDE, this allows a quick way for users to build Symphony within Symphony
*/
require_once(TOOLKIT . '/class.administrationpage.php');
require_once(TOOLKIT . '/class.xsltprocess.php');
require_once(TOOLKIT . '/class.xsltprocess.php');

Class contentBlueprintsUtilities extends AdministrationPage{

var $_existing_file;
public $_existing_file;

function __construct(&$parent){
public function __construct(&$parent){
parent::__construct($parent);
$this->setPageType('form');
}

## Overload the parent 'view' function since we dont need the switchboard logic
function view(){
public function view(){

$this->_existing_file = (isset($this->_context[1]) ? $this->_context[1] . '.xsl' : NULL);

## Handle unknown context
if(!in_array($this->_context[0], array('new', 'edit'))) $this->_Parent->errorPageNotFound();
if(!in_array($this->_context[0], array('new', 'edit'))) Administration::instance()->errorPageNotFound();

## Edit Utility context
if($this->_context[0] == 'edit'){

$file_abs = UTILITIES . '/' . $this->_existing_file;
$filename = $this->_existing_file;

if(!is_file($file_abs)) redirect(URL . '/symphony/blueprints/utilities/new/');
$fields['name'] = $filename;
if(!is_file($file_abs)) redirect(SYMPHONY_URL . '/blueprints/utilities/new/');

$fields['name'] = $filename;
$fields['body'] = @file_get_contents($file_abs);
$this->Form->setAttribute('action', URL . '/symphony/blueprints/utilities/edit/' . $this->_context[1] . '/');

$this->Form->setAttribute('action', SYMPHONY_URL . '/blueprints/utilities/edit/' . $this->_context[1] . '/');
}

else{

$fields['body'] = '<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Expand All @@ -45,45 +52,44 @@ function view(){
</xsl:template>
</xsl:stylesheet>';

}


$formHasErrors = (is_array($this->_errors) && !empty($this->_errors));
$formHasErrors = (is_array($this->_errors) && !empty($this->_errors));
if($formHasErrors) $this->pageAlert(__('An error occurred while processing this form. <a href="#error">See below for details.</a>'), Alert::ERROR);

if(isset($this->_context[2])){
switch($this->_context[2]){

case 'saved':
$this->pageAlert(
__(
'Utility updated at %1$s. <a href="%2$s">Create another?</a> <a href="%3$s">View all Utilities</a>',
'Utility updated at %1$s. <a href="%2$s">Create another?</a> <a href="%3$s">View all Utilities</a>',
array(
DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__),
URL . '/symphony/blueprints/utilities/new/',
URL . '/symphony/blueprints/components/'
DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__),
SYMPHONY_URL . '/blueprints/utilities/new/',
SYMPHONY_URL . '/blueprints/components/'
)
),
),
Alert::SUCCESS);
break;

case 'created':
$this->pageAlert(
__(
'Utility created at %1$s. <a href="%2$s">Create another?</a> <a href="%3$s">View all Utilities</a>',
'Utility created at %1$s. <a href="%2$s">Create another?</a> <a href="%3$s">View all Utilities</a>',
array(
DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__),
URL . '/symphony/blueprints/utilities/new/',
URL . '/symphony/blueprints/components/'
DateTimeObj::getTimeAgo(__SYM_TIME_FORMAT__),
SYMPHONY_URL . '/blueprints/utilities/new/',
SYMPHONY_URL . '/blueprints/components/'
)
),
),
Alert::SUCCESS);
break;

}
}

$this->setTitle(__(($this->_context[0] == 'new' ? '%1$s &ndash; %2$s' : '%1$s &ndash; %2$s &ndash; %3$s'), array(__('Symphony'), __('Utilities'), $filename)));
$this->appendSubheading(($this->_context[0] == 'new' ? __('Untitled') : $filename));

Expand All @@ -93,29 +99,29 @@ function view(){

$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'primary');

$label = Widget::Label(__('Name'));
$label->appendChild(Widget::Input('fields[name]', $fields['name']));
$fieldset->appendChild((isset($this->_errors['name']) ? $this->wrapFormElementWithError($label, $this->_errors['name']) : $label));
$fieldset->appendChild((isset($this->_errors['name']) ? Widget::wrapFormElementWithError($label, $this->_errors['name']) : $label));

$label = Widget::Label(__('Body'));
$label->appendChild(Widget::Textarea('fields[body]', 30, 80, $fields['body'], array('class' => 'code')));
$fieldset->appendChild((isset($this->_errors['body']) ? $this->wrapFormElementWithError($label, $this->_errors['body']) : $label));
$fieldset->appendChild((isset($this->_errors['body']) ? Widget::wrapFormElementWithError($label, $this->_errors['body']) : $label));

$this->Form->appendChild($fieldset);

$utilities = General::listStructure(UTILITIES, array('xsl'), false, 'asc', UTILITIES);
$utilities = $utilities['filelist'];
$utilities = $utilities['filelist'];

if(is_array($utilities) && !empty($utilities)){

$div = new XMLElement('div');
$div->setAttribute('class', 'secondary');

$h3 = new XMLElement('h3', __('Utilities'));
$h3->setAttribute('class', 'label');
$div->appendChild($h3);

$ul = new XMLElement('ul');
$ul->setAttribute('id', 'utilities');

Expand All @@ -127,96 +133,150 @@ function view(){
$li->setAttribute('class', 'odd');
}

$li->appendChild(Widget::Anchor($util, URL . '/symphony/blueprints/utilities/edit/' . str_replace('.xsl', '', $util) . '/', NULL));
$li->appendChild(Widget::Anchor($util, SYMPHONY_URL . '/blueprints/utilities/edit/' . str_replace('.xsl', '', $util) . '/', NULL));
$ul->appendChild($li);
}

$div->appendChild($ul);

$this->Form->appendChild($div);

}

$div = new XMLElement('div');
$div->setAttribute('class', 'actions');
$div->appendChild(Widget::Input('action[save]', ($this->_context[0] == 'edit' ? __('Save Changes') : __('Create Utility')), 'submit', array('accesskey' => 's')));

if($this->_context[0] == 'edit'){
$button = new XMLElement('button', __('Delete'));
$button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'confirm delete', 'title' => __('Delete this utility'), 'type' => 'submit'));
$div->appendChild($button);
}

$this->Form->appendChild($div);




}
function action(){

public function action(){

$this->_existing_file = (isset($this->_context[1]) ? $this->_context[1] . '.xsl' : NULL);

if(array_key_exists('save', $_POST['action']) || array_key_exists('done', $_POST['action'])){

$fields = $_POST['fields'];

$this->_errors = array();

if(!isset($fields['name']) || trim($fields['name']) == '') $this->_errors['name'] = __('Name is a required field.');

if(!isset($fields['body']) || trim($fields['body']) == '') $this->_errors['body'] = __('Body is a required field.');
elseif(!General::validateXML($fields['body'], $errors, false, new XSLTProcess())) $this->_errors['body'] = __('This document is not well formed. The following error was returned: <code>%s</code>', array($errors[0]['message']));


$fields['name'] = Lang::createFilename($fields['name']);
if(General::right($fields['name'], 4) != '.xsl') $fields['name'] .= '.xsl';

$file = UTILITIES . '/' . $fields['name'];

##Duplicate
if($this->_context[0] == 'edit' && ($this->_existing_file != $fields['name'] && is_file($file)))
$this->_errors['name'] = __('A Utility with that name already exists. Please choose another.');

elseif($this->_context[0] == 'new' && is_file($file)) $this->_errors['name'] = __('A Utility with that name already exists. Please choose another.');

if(empty($this->_errors)){
if($this->_context[0] == 'new') {
/**
* Just before the Utility has been created
*
* @delegate UtilityPreCreate
* @since Symphony 2.2
* @param string $context
* '/blueprints/utilities/'
* @param string $file
* The path to the Utility file
* @param string $contents
* The contents of the `$fields['body']`, passed by reference
*/
Administration::instance()->ExtensionManager->notifyMembers('UtilityPreCreate', '/blueprints/utilities/', array('file' => $file, 'contents' => &$fields['body']));
}
else {
/**
* Just before the Utility has been updated
*
* @delegate UtilityPreEdit
* @since Symphony 2.2
* @param string $context
* '/blueprints/utilities/'
* @param string $file
* The path to the Utility file
* @param string $contents
* The contents of the `$fields['body']`, passed by reference
*/
Administration::instance()->ExtensionManager->notifyMembers('UtilityPreEdit', '/blueprints/utilities/', array('file' => $file, 'contents' => &$fields['body']));
}

$fields['name'] = Lang::createFilename($fields['name']);
if(General::right($fields['name'], 4) != '.xsl') $fields['name'] .= '.xsl';

$file = UTILITIES . '/' . $fields['name'];

##Duplicate
if($this->_context[0] == 'edit' && ($this->_existing_file != $fields['name'] && is_file($file)))
$this->_errors['name'] = __('A Utility with that name already exists. Please choose another.');

elseif($this->_context[0] == 'new' && is_file($file)) $this->_errors['name'] = __('A Utility with that name already exists. Please choose another.');

##Write the file
elseif(!$write = General::writeFile($file, $fields['body'], Symphony::Configuration()->get('write_mode', 'file')))
##Write the file
if(!$write = General::writeFile($file, $fields['body'], Symphony::Configuration()->get('write_mode', 'file')))
$this->pageAlert(__('Utility could not be written to disk. Please check permissions on <code>/workspace/utilities</code>.'), Alert::ERROR);

##Write Successful, add record to the database
else{

## Remove any existing file if the filename has changed
if($this->_existing_file && $file != UTILITIES . '/' . $this->_existing_file)
if($this->_existing_file && $file != UTILITIES . '/' . $this->_existing_file) {
General::deleteFile(UTILITIES . '/' . $this->_existing_file);
}

## TODO: Fix me
###
# Delegate: Edit
# Description: After saving the asset, the file path is provided.
//$ExtensionManager->notifyMembers('Edit', getCurrentPage(), array('file' => $file));

redirect(URL . '/symphony/blueprints/utilities/edit/'.str_replace('.xsl', '', $fields['name']) . '/'.($this->_context[0] == 'new' ? 'created' : 'saved') . '/');
if($this->_context[0] == 'new') {
/**
* Just after the Utility has been written to disk
*
* @delegate UtilityPostCreate
* @since Symphony 2.2
* @param string $context
* '/blueprints/utilities/'
* @param string $file
* The path to the Utility file
*/
Administration::instance()->ExtensionManager->notifyMembers('UtilityPostCreate', '/blueprints/utilities/', array('file' => $file));
}
else {
/**
* Prior to deleting the Utility
*
* @delegate UtilityPostEdit
* @since Symphony 2.2
* @param string $context
* '/blueprints/utilities/'
* @param string $file
* The path to the Utility file
*/
Administration::instance()->ExtensionManager->notifyMembers('UtilityPostEdit', '/blueprints/utilities/', array('file' => $file));
}

redirect(SYMPHONY_URL . '/blueprints/utilities/edit/'.str_replace('.xsl', '', $fields['name']) . '/'.($this->_context[0] == 'new' ? 'created' : 'saved') . '/');

}
}
}

elseif($this->_context[0] == 'edit' && @array_key_exists('delete', $_POST['action'])){

## TODO: Fix me
###
# Delegate: Delete
# Description: Prior to deleting the asset file. Target file path is provided.
//$ExtensionManager->notifyMembers('Delete', getCurrentPage(), array('file' => WORKSPACE . '/' . $this->_existing_file_rel));
/**
* Prior to deleting the Utility
*
* @delegate UtilityPreDelete
* @since Symphony 2.2
* @param string $context
* '/blueprints/utilities/'
* @param string $file
* The path to the Utility file
*/
Administration::instance()->ExtensionManager->notifyMembers('UtilityPreDelete', '/blueprints/utilities/', array('file' => $this->_existing_file));

General::deleteFile(UTILITIES . '/' . $this->_existing_file);
General::deleteFile(UTILITIES . '/' . $this->_existing_file);

redirect(URL . '/symphony/blueprints/components/');
}
redirect(SYMPHONY_URL . '/blueprints/components/');
}
}
}

?>

0 comments on commit 79e8ad4

Please sign in to comment.