Newer
Older
<?php
/**
* This file contains \QUI\Blocks\Manager
*/
namespace QUI\Blocks;
use QUI;
use QUI\Projects\Project;
use QUI\Projects\Site;
/**
* Block Manager
*
* @package quiqqer/blocks
*/
class Manager
{
/**
* Blocks table name
*/
const TABLE = 'blocks';
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
/**
* Block temp collector
* @var array
*/
protected $_blocks = array();
/**
* Creates a new block for the project
*
* @param Project $Project
* @param Block $Block
* @return integer - Block-ID
*/
public function createBlockForProject(Project $Project, Block $Block)
{
QUI::getDataBase()->insert(
$this->_getTable(),
array(
'project' => $Project->getName(),
'title' => $Block->getAttribute('title'),
'description' => $Block->getAttribute('description'),
'type' => $Block->getAttribute('type')
)
);
$lastId = QUI::getPDO()->lastInsertId();
return $lastId;
}
/**
* Return the areas which are available in the project
*
* @param Project $Project
* @return array
*/
public function getAreasByProject(Project $Project)
{
$templates = array();
$blocks = array();
$projectName = $Project->getName();
// get all vhosts, and the used templates of the project
$vhosts = QUI::getRewrite()->getVHosts();
foreach ( $vhosts as $vhost )
{
if ( !isset( $vhost['template'] ) ) {
continue;
}
if ( $vhost['project'] != $projectName ) {
continue;
}
$templates[] = $vhost['template'];
}
// get blocks
foreach ( $templates as $template )
{
$blockXML = realpath( OPT_DIR . $template .'/blocks.xml' );
if ( !$blockXML ) {
continue;
}
$blocks = array_merge( $blocks, Utils::getTemplateAreasFromXML( $blockXML ) );
}
return $blocks;
}
/**
* Returns the available blocks
*
* @return array
*/
public function getAvailableBlocks()
{
try
{
return QUI\Cache\Manager::get( $cache );
} catch ( QUI\Exception $Exception )
{
}
$PKM = QUI::getPackageManager();
$packages = $PKM->getInstalled();
$result = array();
$result[] = array(
'title' => array( 'quiqqer/blocks', 'block.content.title' ),
'description' => array( 'quiqqer/blocks', 'block.content.description' ),
'control' => 'content'
);
foreach ( $packages as $package )
{
$blocksXML = OPT_DIR . $package['name'] .'/blocks.xml';
if ( !file_exists( $blocksXML ) ) {
continue;
}
$result = array_merge( $result, Utils::getBlocksFromXML( $blocksXML ) );
}
QUI\Cache\Manager::set( $cache, $result );
return $result;
}
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Get a Block by its Block-ID
*
* @param Integer $id
* @return Block
* @throws QUI\Exception
*/
public function getBlockById($id)
{
if ( isset( $this->_blocks[ $id ] ) ) {
return $this->_blocks[ $id ];
}
$data = QUI::getDataBase()->fetch(array(
'from' => $this->_getTable(),
'where' => array(
'id' => (int)$id
),
'limit' => 1
));
if ( !isset( $data[0] ) ) {
throw new QUI\Exception( 'Block not found' );
}
$this->_blocks[ $id ] = new Block( $data[0] );
return $this->_blocks[ $id ];
}
/**
* Return the blocks from the area
*
* @param string $blockArea - Name of the area
* @param Site $Site
* @return array
*/
public function getBlocksByArea($blockArea, Site $Site)
if ( empty( $blockArea ) ) {
return array();
}
$blockAreas = $Site->getAttribute( 'quiqqer.blocks.areas' );
/**
* Return a list with \QUI\Blocks\Block which are assigned to a project
*
* @param Project $Project
* @return array
*/
public function getBlocksFromProject(Project $Project)
{
$result = array();
$list = QUI::getDataBase()->fetch(array(
'from' => $this->_getTable(),
'where' => array(
'project' => $Project->getName()
)
));
foreach ( $list as $entry )
{
$Block = new Block();
$Block->setAttribute( 'title', $entry['title'] );
$Block->setAttribute( 'description', $entry['description'] );
$settings = json_decode( $entry['settings'], true );
$Block->setAttributes( $settings );
$result[] = $Block;
}
return $result;
}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* @param string|integer $blockId - Block-ID
* @param array $blockData - Block data
*/
public function saveBlock($blockId, array $blockData)
{
$Block = $this->getBlockById( $blockId );
$areas = array();
$areaString = '';
if ( isset( $blockData[ 'id' ] ) ) {
unset( $blockData[ 'id' ] );
}
// check areas
$Project = QUI::getProjectManager()->getProject(
$Block->getAttribute( 'project' )
);
$availableAreas = array_map(function($data)
{
if ( isset( $data[ 'name' ] ) ) {
return $data[ 'name' ];
}
return '';
}, $this->getAreasByProject( $Project ));
if ( isset( $blockData[ 'areas' ] ) )
{
$parts = explode( ',', $blockData[ 'areas' ] );
foreach ( $parts as $area )
{
if ( in_array( $area, $availableAreas ) ) {
$areas[] = $area;
}
}
}
if ( !empty( $areas ) ) {
$areaString = ','. implode( ',', $areas ) .',';
}
$Block->setAttributes( $blockData );
QUI::getDataBase()->update($this->_getTable(), array(
'title' => $Block->getAttribute( 'title' ),
'description' => $Block->getAttribute( 'description' ),
'content' => $Block->getAttribute( 'content' ),
'type' => $Block->getAttribute( 'type' ),
'settings' => json_encode( $Block->getAttribute( 'settings' ) ),
'areas' => $areaString
), array(
'id' => (int)$blockId
));
}
/**
* Returns the blocks table name
* @return String
*/
protected function _getTable()
{
return QUI::getDBTableName( self::TABLE );
}