Skip to content

Commit

Permalink
fix handling xml content with single items, add comments
Browse files Browse the repository at this point in the history
There was issue that template and playlists service allways exected
array for list and template properties.

I added also example for articlesLists theme.xml declaration and
examples for render function.
  • Loading branch information
ahilles107 committed Apr 10, 2015
1 parent 9bccb33 commit c2a7be5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,22 @@ public function wizardThemePlaylistsAction()
$theme = $this->getThemeService()->findById($this->_request->getParam('id'));
$path = __DIR__.'/../../../../themes/'.$theme->getPath().'theme.xml';

$this->view->themePlaylists = $playlistsService->loadThemePlaylists($path);
$themePlaylists = $playlistsService->loadThemePlaylists($path);
if (array_key_exists('template', $themePlaylists['list'])) {
$bakThemePlaylists = $themePlaylists;
$themePlaylists = array();
$themePlaylists['list'][0] = $bakThemePlaylists['list'];
}

foreach($themePlaylists['list'] as $key => $themePlaylist) {
if (array_key_exists('@attributes', $themePlaylist['template'])) {
$bakThemePlaylist = $themePlaylist;
$themePlaylists['list'][0]['template'] = array();
$themePlaylists['list'][0]['template'][0] = $bakThemePlaylist['template'];
}
}

$this->view->themePlaylists = $themePlaylists;
$this->view->playlistsAreUpToDate = $playlistsService->checkIfThemePlaylistsAreUpToDate($theme, $this->view->themePlaylists);
$this->view->theme = $theme;
}
Expand Down
8 changes: 2 additions & 6 deletions newscoop/classes/cache/TemplateCacheHandler_DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,8 @@ public function handler($action, &$cache_content, $tpl_file = null, $cache_id =
$exp_time += time();
$tpl_file = md5($tpl_file).substr($tpl_file, -15);

$uri = CampSite::GetURIInstance();
$smarty = CampTemplate::singleton();
$campsiteVector = array_merge(
$uri->getCampsiteVector(),
$smarty->campsiteVector
);
$smarty = CampTemplate::singleton();
$campsiteVector = $smarty->campsiteVector;

$return = false;
if ($action != 'clean') {
Expand Down
22 changes: 22 additions & 0 deletions newscoop/include/smarty/campsite_plugins/function.render.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
/**
* Campsite render function plugin
*
* Newscoop caching save cached template file content with special vector parameters.
* By default vector is filled with 5 parameters:
* * language
* * publication
* * issue
* * section
* * article
*
* There is also "params" parameter where we can save put array of custom parameters (serialized into string) or string.
*
* To ignore one or more parameters (to make cached template this same for many articles, sections, issues etc) just set it value to "off"
*
* You can also provide custom cache lifetime (or set it in admin themes management) - use "cache" parameter. Setting "cache" to off will not cache this rendered file.
*
* Examples:
*
* {{ render file="_tpl/_html-head.tpl" cache="3200" }} - cache "_tpl/_html-head.tpl" file for 3200 seconds with current context vector
*
* {{ render file="_tpl/_html-head.tpl" publication="2" }} - change default publication value in vector to 2
*
* {{ render file="_tpl/_html-head.tpl" article="off" cache="3200" }} - cache "_tpl/_html-head.tpl" file for 3200 seconds for all articles in current vector (section, issue, publication and language)
*
* Type: function
* Name: render
* Purpose: template rendering
Expand Down
71 changes: 47 additions & 24 deletions newscoop/library/Newscoop/Services/PlaylistsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,21 @@ public function removeLeftItems($playlist)
}

/**
* Load xml file
* Load articlesLists from xml file
*
* example template articles lists schema:
*
* <articlesLists> # main section
* <list name="FrontPage"> # single playlist declaration
* <template file="article.tpl" /> # single template file assigned to playlist declaration
* </list>
* <list name="Second Playlist">
* <template file="issue.tpl" />
* <template file="front.tpl" />
* </list>
* </articlesLists>
*
* Cache for all assigned to playlist templates will be cleared after playlist update action
*
* @param string $path
*
Expand All @@ -239,13 +253,7 @@ public function loadThemePlaylists($path)
*/
public function checkIfThemePlaylistsAreUpToDate($theme, $themePlaylists)
{
$newThemePlaylists = array();
foreach($themePlaylists['list'] as $themePlaylist) {
$newThemePlaylists[$themePlaylist['@attributes']['name']] = array();
foreach($themePlaylist['template'] as $template) {
$newThemePlaylists[$themePlaylist['@attributes']['name']]['templates'][] = $template['@attributes']['file'];
}
}
$newThemePlaylists = $this->buildNewThemePlaylists($themePlaylists);

foreach ($newThemePlaylists as $playlistName => $themePlaylist) {
$playlist = $this->em->getRepository('Newscoop\Entity\Playlist')->getPlaylistByTitle($playlistName)->getOneOrNullResult();
Expand Down Expand Up @@ -276,14 +284,9 @@ public function checkIfThemePlaylistsAreUpToDate($theme, $themePlaylists)
*
* @return boolean
*/
public function updateThemePlaylists($theme, $themePlaylists) {
$newThemePlaylists = array();
foreach($themePlaylists['list'] as $themePlaylist) {
$newThemePlaylists[$themePlaylist['@attributes']['name']] = array();
foreach($themePlaylist['template'] as $template) {
$newThemePlaylists[$themePlaylist['@attributes']['name']]['templates'][] = $template['@attributes']['file'];
}
}
public function updateThemePlaylists($theme, $themePlaylists)
{
$newThemePlaylists = $this->buildNewThemePlaylists($themePlaylists);

foreach ($newThemePlaylists as $playlistName => $themePlaylist) {
$playlist = $this->em->getRepository('Newscoop\Entity\Playlist')->getPlaylistByTitle($playlistName)->getOneOrNullResult();
Expand Down Expand Up @@ -313,14 +316,9 @@ public function updateThemePlaylists($theme, $themePlaylists) {
*
* @return boolean
*/
public function removeThemeFromPlaylists($theme, $themePlaylists) {
$newThemePlaylists = array();
foreach($themePlaylists['list'] as $themePlaylist) {
$newThemePlaylists[$themePlaylist['@attributes']['name']] = array();
foreach($themePlaylist['template'] as $template) {
$newThemePlaylists[$themePlaylist['@attributes']['name']]['templates'][] = $template['@attributes']['file'];
}
}
public function removeThemeFromPlaylists($theme, $themePlaylists)
{
$newThemePlaylists = $this->buildNewThemePlaylists($themePlaylists);

foreach ($newThemePlaylists as $playlistName => $themePlaylist) {
$playlist = $this->em->getRepository('Newscoop\Entity\Playlist')->getPlaylistByTitle($playlistName)->getOneOrNullResult();
Expand Down Expand Up @@ -352,4 +350,29 @@ public function clearPlaylistTemplates($playlist)
}
}
}

private function buildNewThemePlaylists($themePlaylists)
{
$newThemePlaylists = array();
if (array_key_exists('template', $themePlaylists['list'])) {
$bakThemePlaylists = $themePlaylists;
$themePlaylists = array();
$themePlaylists['list'][0] = $bakThemePlaylists['list'];
}

foreach($themePlaylists['list'] as $themePlaylist) {
$newThemePlaylists[$themePlaylist['@attributes']['name']] = array();
if (array_key_exists('@attributes', $themePlaylist['template'])) {
$bakThemePlaylist = $themePlaylist;
$themePlaylist = array();
$themePlaylist['template'][0] = $bakThemePlaylist['template'];
}

foreach($themePlaylist['template'] as $template) {
$newThemePlaylists[$themePlaylist['@attributes']['name']]['templates'][] = $template['@attributes']['file'];
}
}

return $newThemePlaylists;
}
}

0 comments on commit c2a7be5

Please sign in to comment.