This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the getrandom User API method to fetch random pubs from a pubty…
…pe. Closes #58, #113. The RandomPub and RandomList blocks are examples of using the new method, working with the same standards as list and display methods.
- Loading branch information
Showing
7 changed files
with
644 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
<?php | ||
/** | ||
* Clip | ||
* | ||
* @copyright (c) Clip Team | ||
* @link http://github.com/zikula-modules/clip/ | ||
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html | ||
* @package Clip | ||
* @subpackage Block | ||
*/ | ||
|
||
/** | ||
* RandomList Block. | ||
*/ | ||
class Clip_Block_RandomList extends Zikula_Controller_AbstractBlock | ||
{ | ||
/** | ||
* Initialise block. | ||
*/ | ||
public function init() | ||
{ | ||
SecurityUtil::registerPermissionSchema('Clip:block:randomlist', 'Block Id:Pubtype Id:'); | ||
} | ||
|
||
/** | ||
* Get information on block. | ||
*/ | ||
public function info() | ||
{ | ||
return array( | ||
'module' => 'Clip', | ||
'text_type' => $this->__('Random List'), | ||
'text_type_long' => $this->__('Random list of publications'), | ||
'allow_multiple' => true, | ||
'form_content' => false, | ||
'form_refresh' => false, | ||
'show_preview' => true | ||
); | ||
} | ||
|
||
/** | ||
* Display the block according its configuration. | ||
*/ | ||
public function display($blockinfo) | ||
{ | ||
$alert = $this->getVar('devmode', false) && Clip_Access::toClip(ACCESS_ADMIN); | ||
|
||
// get variables from content block | ||
$vars = BlockUtil::varsFromContent($blockinfo['content']); | ||
|
||
// validation of required parameters | ||
if (!isset($vars['tid']) || empty($vars['tid'])) { | ||
return $alert ? $this->__f('Required parameter [%s] not set or empty.', 'tid') : null; | ||
} | ||
if (!Clip_Util::validateTid($vars['tid'])) { | ||
return $alert ? LogUtil::registerError($this->__f('Error! Invalid publication type ID passed [%s].', DataUtil::formatForDisplay($vars['tid']))) : null; | ||
} | ||
|
||
// security check | ||
// FIXME SECURITY centralize on Clip_Access | ||
if (!SecurityUtil::checkPermission('Clip:block:randomlist', "$blockinfo[bid]:$vars[tid]:", ACCESS_OVERVIEW)) { | ||
return; | ||
} | ||
|
||
// default values | ||
$tpl = (isset($vars['template']) && !empty($vars['template'])) ? $vars['template'] : 'list'; | ||
$cachelt = isset($vars['cachelifetime']) ? (int)$vars['cachelifetime'] : null; | ||
|
||
$pubtype = Clip_Util::getPubType($vars['tid']); | ||
$template = "{$pubtype['folder']}/random_{$tpl}.tpl"; | ||
|
||
// check if the common does not exist | ||
if (!$this->view->template_exists($template) && !$this->getVar('devmode', false)) { | ||
return; | ||
} | ||
|
||
// check if cache is enabled and this block is cached | ||
if (!empty($cachelt)) { | ||
$this->view->setCacheLifetime($cachelt); | ||
|
||
Clip_Util::register_nocache_plugins($this->view); | ||
|
||
$cacheid = 'tid_'.$vars['tid'].'/randomlist' | ||
.'/bid_'.$blockinfo['bid'] | ||
.'/'.UserUtil::getGidCacheString(); | ||
// FIXME PLUGINS Add plugin specific cache sections | ||
// $cacheid .= '|field'.id.'|'.output | ||
|
||
// set the output info | ||
$this->view->setCaching(Zikula_View::CACHE_INDIVIDUAL) | ||
->setCacheId($cacheid); | ||
|
||
if ($this->view->is_cached($template)) { | ||
$blockinfo['content'] = $this->view->fetch($template); | ||
|
||
return BlockUtil::themeBlock($blockinfo); | ||
} | ||
} else { | ||
$cacheid = null; | ||
$this->view->setCaching(Zikula_View::CACHE_DISABLED); | ||
} | ||
|
||
//// Execution | ||
$vars['limit'] = isset($vars['limit']) && (int)$vars['limit'] > 1 ? (int)$vars['limit'] : 3; | ||
|
||
$vars['filter'] = isset($vars['filter']) && !empty($vars['filter']) ? $vars['filter'] : '()'; | ||
|
||
$vars['where'] = array(); | ||
$vars['where'][] = array('core_online = ?', 1); | ||
$vars['where'][] = array('core_visible = ?', 1); | ||
$vars['where'][] = array('core_intrash = ?', 0); | ||
|
||
// get the random list | ||
$publist = ModUtil::apiFunc('Clip', 'user', 'getrandom', $vars); | ||
|
||
if (!$publist->count()) { | ||
return; | ||
} | ||
|
||
// register clip_util | ||
Clip_Util::register_utilities($this->view); | ||
|
||
//// Output | ||
// assign the pubdata and pubtype to the output | ||
$this->view->assign('publist', $publist) | ||
->assign('pubtype', $pubtype); | ||
|
||
// check if template is not available | ||
if (!$this->view->template_exists($template)) { | ||
if ($alert) { | ||
LogUtil::registerStatus($this->__f('Notice: Template [%s] not found.', $template)); | ||
} | ||
|
||
$template = 'generic_blocklist.tpl'; | ||
|
||
// settings for the autogenerated display template | ||
$this->view->setForceCompile(true) | ||
->setCaching(Zikula_View::CACHE_DISABLED) | ||
->assign('clip_generic_tpl', true); | ||
} | ||
|
||
$blockinfo['content'] = $this->view->fetch($template, $cacheid); | ||
|
||
if (empty($blockinfo['content'])) { | ||
return; | ||
} | ||
|
||
return BlockUtil::themeBlock($blockinfo); | ||
} | ||
|
||
/** | ||
* Modify block settings. | ||
*/ | ||
public function modify($blockinfo) | ||
{ | ||
// get current content | ||
$vars = BlockUtil::varsFromContent($blockinfo['content']); | ||
|
||
// defaults | ||
if (!isset($vars['tid'])) { | ||
$vars['tid'] = ''; | ||
} | ||
if (!isset($vars['filter'])) { | ||
$vars['filter'] = ''; | ||
} | ||
if (!isset($vars['limit']) || (int)$vars['limit'] < 1) { | ||
$vars['limit'] = 3; | ||
} | ||
if (!isset($vars['template'])) { | ||
$vars['template'] = ''; | ||
} | ||
if (!isset($vars['cachelifetime'])) { | ||
$vars['cachelifetime'] = 0; | ||
} | ||
|
||
// builds the pubtypes selector | ||
$pubtypes = Clip_Util::getPubType(-1)->toKeyValueArray('tid', 'title'); | ||
|
||
// builds the output | ||
$this->view->assign('vars', $vars) | ||
->assign('pubtypes', $pubtypes); | ||
|
||
// return output | ||
return $this->view->fetch('clip_block_randomlist_modify.tpl'); | ||
} | ||
|
||
/** | ||
* Update block settings. | ||
*/ | ||
public function update($blockinfo) | ||
{ | ||
$vars = array ( | ||
'tid' => FormUtil::getPassedValue('tid'), | ||
'filter' => FormUtil::getPassedValue('filter'), | ||
'limit' => FormUtil::getPassedValue('limit'), | ||
'template' => FormUtil::getPassedValue('template'), | ||
'cachelifetime' => FormUtil::getPassedValue('cachelifetime') | ||
); | ||
|
||
$vars['limit'] = (int)$vars['limit'] > 1 ? (int)$vars['limit'] : 3; | ||
|
||
$blockinfo['content'] = BlockUtil::varsToContent($vars); | ||
|
||
return $blockinfo; | ||
} | ||
} |
Oops, something went wrong.