Work on the templating system

- create a basic template
- add text elements to the template
- rearrange elements
pull/274/head^2
iglocska 2014-06-10 15:18:49 +02:00
parent 19b4a2ae37
commit f4e5c22865
35 changed files with 659 additions and 0 deletions

View File

@ -0,0 +1,97 @@
<?php
App::uses('AppController', 'Controller');
/**
* TemplateElements Controller
*
* @property TemplateElement $TemplateElements
*/
class TemplateElementsController extends AppController {
public $components = array('Security' ,'RequestHandler');
public $paginate = array(
'limit' => 50,
'order' => array(
'TemplateElement.position' => 'asc'
)
);
public function index($id) {
//check permissions
$template = $this->TemplateElement->Template->find('first', array(
'recursive' => -1,
'fields' => array('id', 'share', 'org'),
'conditions' => array('id' => $id)
));
if (!empty($template) && !$this->_isSiteAdmin() && !$template['Template']['share'] && !$template['Template']['org']) throw new MethodNotAllowedException('Template not found or you are not authorised to view it.');
$templateElements = $this->TemplateElement->find('all', array(
'conditions' => array(
'template_id' => $id,
),
'contain' => array(
'TemplateElementAttribute',
'TemplateElementText'
),
'order' => array('TemplateElement.position ASC')
));
foreach ($templateElements as &$e) {
if (!empty($e['TemplateElementAttribute'])) {
$e['TemplateElementAttribute'][0]['type'] = json_decode($e['TemplateElementAttribute'][0]['type']);
}
}
$this->set('id', $id);
$this->layout = 'ajaxTemplate';
$this->set('elements', $templateElements);
$this->render('ajax/ajaxIndex');
}
public function templateElementAddChoices($id) {
//check permissions
if (!$this->request->is('ajax')) Throw new MethodNotAllowedException('This action is for ajax requests only.');
$this->set('id', $id);
$this->layout = 'ajax';
$this->render('ajax/template_element_add_choices');
}
public function templateElementAdd($type, $id) {
//check permissions
if (!$this->request->is('ajax')) Throw new MethodNotAllowedException('This action is for ajax requests only.');
if ($this->request->is('get')) {
$this->set('id', $id);
$this->layout = 'ajaxTemplate';
$this->render('ajax/template_element_add_' . $type);
} else if ($this->request->is('post')) {
$pos = $this->TemplateElement->lastPosition($id);
//$capType = ucfirst($type);
$this->TemplateElement->create();
$templateElement = array(
'TemplateElement' => array(
'template_id' => $id,
'position' => ++$pos,
'element_definition' => $type
),
);
if ($this->TemplateElement->save($templateElement)) {
$this->request->data['TemplateElementText']['template_element_id'] = $this->TemplateElement->id;
$this->TemplateElement->TemplateElementText->create();
if ($this->TemplateElement->TemplateElementText->save($this->request->data)) {
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => 'Element successfully added to template.')), 'status' => 200));
} else {
$this->TemplateElement->delete($this->TemplateElement->id);
}
}
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'errors' => 'The element could not be added.')), 'status' => 200));
}
}
}

View File

@ -0,0 +1,133 @@
<?php
App::uses('AppController', 'Controller');
/**
* Templates Controller
*
* @property Template $Templates
*/
class TemplatesController extends AppController {
public $components = array('Security' ,'RequestHandler');
public $paginate = array(
'limit' => 50,
'order' => array(
'Template.id' => 'desc'
)
);
public function beforeFilter() { // TODO REMOVE
parent::beforeFilter();
$this->Security->unlockedActions = array('saveElementSorting');
}
public function fetchFormFromTemplate($id) {
}
public function index($id) {
}
public function edit($id) {
}
public function view($id) {
$template = $this->Template->find('first', array(
'conditions' => array(
'id' => $id,
),
'contain' => array(
'TemplateElement',
'TemplateTag' => array(
'Tag',
),
),
));
if (empty($template)) throw new NotFoundException('No template with the provided ID exists, or you are not authorised to see it.');
$tagArray = array();
foreach($template['TemplateTag'] as $tt) {
$tagArray[] = $tt;
}
$this->set('id', $id);
$this->set('template', $template);
}
public function add() {
if ($this->request->is('post')) {
unset($this->request->data['Template']['tagsPusher']);
$tags = $this->request->data['Template']['tags'];
unset($this->request->data['Template']['tags']);
$this->request->data['Template']['org'] = $this->Auth->user('org');
$this->Template->create();
if ($this->Template->save($this->request->data)) {
$id = $this->Template->id;
$tagArray = json_decode($tags);
$this->loadModel('TemplateTag');
$this->loadModel('Tag');
foreach ($tagArray as $t) {
$tag = $this->Tag->find('first', array(
'conditions' => array('name' => $t),
'fields' => array('id', 'name'),
'recursive' => -1,
));
$this->TemplateTag->create();
$this->TemplateTag->save(array('TemplateTag' => array('template_id' => $id, 'tag_id' => $tag['Tag']['id'])));
}
$this->redirect(array('action' => 'view', $this->Template->id));
} else {
throw new Exception('The template could not be created.');
}
}
$this->loadModel('Tags');
$tags = $this->Tags->find('all');
$tagArray = array();
foreach ($tags as $tag) {
$tagArray[$tag['Tags']['id']] = $tag['Tags']['name'];
}
$this->set('tags', $tagArray);
$this->set('tagInfo', $tags);
}
public function saveElementSorting() {
// check if user can edit the template
$this->autoRender = false;
$this->request->onlyAllow('ajax');
$orderedElements = $this->request->data;
foreach($orderedElements as &$e) {
$e = ltrim($e, 'id_');
}
$extractedIds = array();
foreach ($orderedElements as $element) $extractedIds[] = $element;
$template_id = $this->Template->TemplateElement->find('first', array(
'conditions' => array('id' => $extractedIds),
'recursive' => -1,
'fields' => array('id', 'template_id'),
));
$elements = $this->Template->TemplateElement->find('all', array(
'conditions' => array('template_id' => $template_id['TemplateElement']['template_id']),
'recursive' => -1,
));
if (empty($elements)) return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Something went wrong, the supplied template elements don\'t exist, or you are not eligible to edit them.')),'status'=>200));
if (count($elements) != count($orderedElements)) return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Incomplete template element list passed as argument. Expecting ' . count($elements) . ' elements, only received positions for ' . count($orderedElements) . '.')),'status'=>200));
$template_id = $elements[0]['TemplateElement']['template_id'];
foreach ($elements as &$e) {
if ($template_id !== $e['TemplateElement']['template_id']) return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Cannot sort template elements belonging to separate templates. You should never see this message during legitimate use.')),'status'=>200));
foreach ($orderedElements as $k => $orderedElement) {
if ($orderedElement == $e['TemplateElement']['id']) {
$e['TemplateElement']['position'] = $k+1;
}
}
}
$this->Template->TemplateElement->saveMany($elements);
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => 'Elements repositioned.')),'status'=>200));
}
public function delete($id) {
}
}

12
app/Model/Template.php Normal file
View File

@ -0,0 +1,12 @@
<?php
App::uses('AppModel', 'Model');
/**
* Template Model
*
*/
class Template extends AppModel {
public $actsAs = array('Containable');
public $hasMany = array('TemplateElement', 'TemplateTag');
}

View File

@ -0,0 +1,21 @@
<?php
App::uses('AppModel', 'Model');
/**
* TemplateElement Model
*
*/
class TemplateElement extends AppModel {
public $actsAs = array('Containable');
public $hasMany = array('TemplateElementAttribute', 'TemplateElementText');
public $belongsTo = array('Template');
public function lastPosition($template_id) {
$result = $this->find('first', array(
'fields' => array('MAX(position) AS pos', 'id', 'template_id'),
'conditions' => array('template_id' => $template_id)
));
return $result[0]['pos'];
}
}

View File

@ -0,0 +1,13 @@
<?php
App::uses('AppModel', 'Model');
/**
* TemplateElementAttribute Model
*
*/
class TemplateElementAttribute extends AppModel {
public $actsAs = array('Containable');
public $hasMany = array('TemplateElementType');
public $belongsTo = array('TemplateElement');
}

View File

@ -0,0 +1,12 @@
<?php
App::uses('AppModel', 'Model');
/**
* TemplateElementText Model
*
*/
class TemplateElementText extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array('TemplateElement');
}

12
app/Model/TemplateTag.php Normal file
View File

@ -0,0 +1,12 @@
<?php
App::uses('AppModel', 'Model');
/**
* TemplateTag Model
*
*/
class TemplateTag extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array('Template', 'Tag');
}

View File

@ -0,0 +1,14 @@
<td>
<div id="tag_bubble_<?php echo $tag['Tag']['id']; ?>">
<table>
<tr>
<td style="padding-right:0px;">
<span class="tagFirstHalf" style="background-color:<?php echo $tag['Tag']['colour'];?>;color:<?php echo $this->TextColour->getTextColour($tag['Tag']['colour']);?>"><?php echo h($tag['Tag']['name']); ?></a>
</td>
<td style="padding-left:0px;padding-right:5px;">
<span class="tagSecondHalf useCursorPointer" onClick="removeTemplateTag('<?php echo $tag['Tag']['id']; ?>', '<?php echo h($tag['Tag']['name']); ?>');">x</span>
</td>
</tr>
</table>
</div>
</td>

View File

@ -0,0 +1,58 @@
<li id="id_<?php echo $element_id;?>" class="templateTableRow">
<div class="templateElementHeader" style="width:100%; position:relative;">
<div class="templateGlass"></div>
<div class ="templateElementHeaderText">Attribute</div>
</div>
<table style="width:100%">
<tr style="width:100%">
<td class="templateTableTDName templateTableCellFirst templateTableColumnName">Name</td>
<td class="templateTableTDDescription templateTableCell templateTableColumnName">Description</td>
<td class="templateTableNormal templateTableCell templateTableColumnName">Category</td>
<td class="templateTableTDTypes templateTableCell templateTableColumnName">Valid Types</td>
<td class="templateTableTDShort templateTableCell templateTableColumnName">Req.</td>
<td class="templateTableTDShort templateTableCell templateTableColumnName">Batch</td>
<td class="templateTableTDShort templateTableCell templateTableColumnName">IDS</td>
<td class="templateTableTDActions templateTableCell templateTableColumnName">Actions</td>
</tr>
<tr>
<td class="templateTableTDName templateTableCellFirst">
<?php echo $element['TemplateElementAttribute'][0]['name']; ?>&nbsp;
</td>
<td class="templateTableTDDescription templateTableCell">
<?php echo $element['TemplateElementAttribute'][0]['description']; ?>&nbsp;
</td>
<td class="templateTableNormal templateTableCell">
<?php echo $element['TemplateElementAttribute'][0]['category']; ?>&nbsp;
</td>
<td class="templateTableTDTypes templateTableCell">
<?php
foreach ($element['TemplateElementAttribute'][0]['type'] as $k => $type) {
if ($k != 0) echo ', ' . $type;
else echo $type;
}
?>&nbsp;
</td>
<td class="templateTableTDShort templateTableCell">
<?php
if ($element['TemplateElementAttribute'][0]['mandatory']) echo 'Yes';
else echo 'No';
?>&nbsp;
</td>
<td class="templateTableTDShort templateTableCell">
<?php
if ($element['TemplateElementAttribute'][0]['batch']) echo 'Yes';
else echo 'No';
?>&nbsp;
</td>
<td class="templateTableTDShort templateTableCell">
<?php
$ids_text = array('No', 'Yes', 'User');
echo $ids_text[$element['TemplateElementAttribute'][0]['to_ids']];
?>&nbsp;
</td>
<td class="templateTableTDActions templateTableCell">
&nbsp;
</td>
</tr>
</table>
</li>

View File

@ -0,0 +1,24 @@
<li id="id_<?php echo $element_id;?>" class="templateTableRow">
<div class="templateElementHeader" style="width:100%; position:relative;">
<div class="templateGlass"></div>
<div class ="templateElementHeaderText">Text</div>
</div>
<table width="100%">
<tr>
<td class="templateTableTDName templateTableCellFirst templateTableColumnName">Name</td>
<td class="templateTableTDText templateTableCell templateTableColumnName">Text</td>
<td class="templateTableTDActions templateTableCell templateTableColumnName">Actions</td>
</tr>
<tr>
<td class="templateTableTDName templateTableCellFirst">
<?php echo $element['TemplateElementText'][0]['name']; ?>&nbsp;
</td>
<td class="templateTableTDText templateTableCell">
<?php echo $element['TemplateElementText'][0]['text']; ?>&nbsp;
</td>
<td class="templateTableTDActions templateTableCell">
&nbsp;
</td>
</tr>
</table>
</li>

View File

@ -0,0 +1,25 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View.Layouts
* @since CakePHP(tm) v 0.10.0.1076
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
<?php
//echo $this->Html->script('jquery-2.1.0.min');
echo $this->Html->css('jquery-ui-1.10.4.min');
//echo $this->Html->css('bootstrap');
//echo $this->Html->css('main');
echo $this->Html->script('jquery-ui-1.10.4.min');
echo $content_for_layout; ?>

View File

@ -0,0 +1,3 @@
<?php
echo $this->element('ajaxTemplateTag');
?>

View File

@ -0,0 +1,36 @@
<div id="ajaxTemplateElementsIndex">
<h2>Template Elements</h2>
<ul id="sortable" style="list-style:none; margin:0px;">
<?php
foreach ($elements as $k => $element):
echo $this->element('templateElements/templateRow' . ucfirst($element['TemplateElement']['element_definition']), array('element' => $element, 'element_id' => $element['TemplateElement']['id']));
endforeach;
?>
</ul>
<div id="AddTemplateElementDiv" class="addTemplateElement useCursorPointer" onClick="templateAddElementClicked(<?php echo $id; ?>);">+</div>
</div>
<script type="text/javascript">
$(function() {
//Return a helper with preserved width of cells
var fixHelper = function(e, ui) {
ui.children().each(function() {
$(this).width($(this).width());
});
return ui;
};
$("#sortable").sortable({
helper: fixHelper,
update: function () {
var order = [];
$("#sortable").children().each(function (i) {
var li = $(this);
order[i] = li.attr("id");
});
saveElementSorting(JSON.stringify(order));
}
}).disableSelection();
});
</script>

View File

@ -0,0 +1,10 @@
<div class="template_element_add_choice">
<?php
echo $this->Form->create('Attribute', array('id'));
?>
<legend><?php echo __('Choose element type'); ?></legend>
<div class="templateChoiceButton" onClick="templateAddElement('attribute', '<?php echo $id;?>');">Attribute</div>
<div class="templateChoiceButton" onClick="templateAddElement('attachment', '<?php echo $id;?>');">Attachment</div>
<div class="templateChoiceButton" onClick="templateAddElement('text', '<?php echo $id;?>');">Text</div>
<div class="templateChoiceButton" onClick="cancelPopoverForm();">Cancel</div>
</div>

View File

@ -0,0 +1,43 @@
<div class="template_element_add_text">
<?php
echo $this->Form->create('TemplateElementText', array('id'));
?>
<legend><?php echo __('Add Text Element To Template'); ?></legend>
<fieldset>
<div class="add_attribute_fields">
<?php
echo $this->Form->input('name', array(
'type' => 'text',
'error' => array('escape' => false),
'div' => 'input clear',
'class' => 'input-xxlarge'
));
echo $this->Form->input('text', array(
'type' => 'textarea',
'error' => array('escape' => false),
'div' => 'input clear',
'class' => 'input-xxlarge'
));
?>
</div>
</fieldset>
<div class="overlay_spacing">
<table>
<tr>
<td style="vertical-align:top">
<span id="submitButton" class="btn btn-primary" onClick="submitPopoverForm('<?php echo $id;?>', 'addTextElement')">Submit</span>
</td>
<td style="width:540px;">
<p style="color:red;font-weight:bold;display:none;text-align:center" id="warning-message">Warning: You are about to share data that is of a classified nature (Attribution / targeting data). Make sure that you are authorised to share this.</p>
</td>
<td style="vertical-align:top;">
<span class="btn btn-inverse" id="cancel_attribute_add" onClick="cancelPopoverForm();">Cancel</span>
</td>
</tr>
</table>
</div>
<?php
echo $this->Form->end();
?>
</div>

View File

@ -0,0 +1,65 @@
<div class="templates form">
<?php
echo $this->Html->script('ajaxification');
echo $this->Form->create('Template');
?>
<fieldset>
<legend><?php echo __('Create Template'); ?></legend>
<?php
echo ($this->Form->input('name', array('div' => 'clear')));
echo ($this->Form->input('tags', array('id' => 'hiddenTags','div' => 'clear', 'label' => false, 'type' => 'text', 'value' => '[]', 'style' => 'display:none;')));
?>
<div id ="tagList">
<label>Tags</label>
<table>
<tr>
<td><table><tr id = "tags"></tr></table></td>
<td id = "addTagButtonTD">
<span onClick="activateTagField()" id="addTagButton" class="btn btn-inverse noPrint" style="line-height:10px; padding: 4px 4px;">+</span>
</td>
<td id = "addTagFieldTD">
<?
echo $this->Form->input('tagsPusher', array(
'div' => 'clear',
'id' => 'addTagField',
'options' => array($tags),
'label' => false,
'onChange' => 'tagFieldChange()',
'style' => "height:22px;padding:0px;margin-bottom:0px;display:none;",
'empty' => 'Add a tag',
));
?>
</td>
</tr>
</table>
</div><br />
<?php
echo $this->Form->input('description', array(
'label' => 'Event Description',
'div' => 'clear',
'type' => 'textarea',
'class' => 'form-control span6',
'placeholder' => 'A description of the template'
));
echo $this->Form->input('share', array(
'label' => 'Share this template with others',
));
?>
</fieldset>
<?php echo $this->Form->button(__('Create'), array('class' => 'btn btn-primary'));
echo $this->Form->end();?>
</div>
<?php
echo $this->element('side_menu', array('menuList' => 'admin', 'menuItem' => 'addUser'));
?>
<script type="text/javascript">
var selectedTags = [];
var allTags = [
<?php
foreach ($tagInfo as $tag) {
echo "{'id' : '" . $tag['Tags']['id'] . "', 'name' : '" . $tag['Tags']['name'] . "', 'colour' : '" . $tag['Tags']['colour'] . "'},";
}
?>
];
</script>
<?php echo $this->Js->writeBuffer();

View File

@ -0,0 +1,60 @@
<div class="templates view">
<?php
echo $this->Html->script('ajaxification');
?>
<h2><?php echo __('Template');?></h2>
<dl>
<dt><?php echo __('Id'); ?></dt>
<dd>
<?php echo $template['Template']['id']; ?>
&nbsp;
</dd>
<dt><?php echo __('Name'); ?></dt>
<dd>
<?php echo h($template['Template']['name']); ?>
&nbsp;
</dd>
<dt><?php echo __('Description'); ?></dt>
<dd>
<?php echo h($template['Template']['description']); ?>
&nbsp;
</dd>
<dt><?php echo __('Tags'); ?></dt>
<dd>
<table>
<tr id = "tags">
<?php
if (!empty($template['TemplateTag'])) {
foreach ($template['TemplateTag'] as $tag) {
echo $this->element('ajaxTemplateTag', array('tag' => $tag));
}
} else echo '&nbsp';
?>
</tr>
</table>
</dd>
<dt><?php echo __('Organisation'); ?></dt>
<dd>
<?php echo h($template['Template']['org']); ?>
&nbsp;
</dd>
<dt><?php echo __('Shareable'); ?></dt>
<dd>
<?php
if ($template['Template']['share']) echo 'Yes';
else echo 'No';
?>
</dd>
</dl>
<div id="templateElements">
</div>
<div id="popover_form" class="ajax_popover_form">aaa</div>
</div>
<?php
echo $this->element('side_menu', array('menuList' => 'globalActions', 'menuItem' => 'roles'));
?>
<script type="text/javascript">
$(document).ready( function () {
updateIndex(<?php echo $template['Template']['id']?>, 'template');
});
</script>

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 491 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 336 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 353 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 401 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long