-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.php
118 lines (100 loc) · 3.2 KB
/
Extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Bolt\Extension\Bolt\Editable;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Bolt\Content;
class Extension extends ExtensionHelper
{
protected $controller;
/**
* Initialize extension
*/
public function initialize()
{
parent::initialize();
// @todo Is it a good idea to enable plugin on backend?
if ($this->authorized) {
$config = $this->config;
if (empty($config)) {
throw new \Exception('Something wrong with accessing configuration in extension ' . $this->getName());
}
$this->controller = $this->createController($config['editor']);
$this->controller->initialize($this->app);
$this->addAssets($config);
$this->app->post('/editable/save', array(
$this,
'save'
))
->method('POST')
->bind('saveit');
}
$this->addTwigFunction('editable', 'twigEditable');
}
/**
* Builds and adds html assets are defined in config.yml
*/
protected function addAssets($config)
{
$name = $config['editor'];
$styles = $config['styles'] ? : array();
$scripts = $config['scripts'] ? : array();
$editor = $name . '.js';
$this->addJquery();
$this->addJavascript("{$name}/{$editor}", false);
foreach ($styles as $item) {
$this->addCSS("{$item}.css");
}
foreach ($scripts as $item) {
$this->addJavascript("{$item}.js", true, 100);
}
}
/**
* Receives save http request
*
* @param Application $app
* @param Request $request
* @return string
*/
public function save(Application $app, Request $request)
{
$result = $this->controller->save($app, $request);
return json_encode($result);
}
// because of some caching issues always the same twig function was calling so
// can't add different php functions with same name to be able to handle different cases
/**
*
* @param string $fieldname
* @param string $record
* @param unknown $options
* @return \Twig_Markup
*/
public function twigEditable($fieldname, $record = null, $options = array())
{
$html = '';
$record = $record ? : $this->getDefaultRecord();
if ($this->authorized) {
if ($record && $record instanceof \Bolt\Content) {
$element = new EditableElement($this->app);
$element->applyRecord($record, $fieldname);
$html = $this->controller->getHtml($element, $record, $options);
}
} else {
$html = $record ? $record->values[$fieldname] : '';
}
return new \Twig_Markup($html, 'UTF-8');
}
/**
* Gets actual record in template context
*
* @return boolean Bolt\Content if no current record in template context
*/
protected function getDefaultRecord()
{
$globals = $this->app['twig']->getGlobals('record');
if (! isset($globals['record'])) {
return false;
}
return $globals['record'];
}
}