public function test_insertBlock()
 {
     $block = new XoopsBlock();
     $block->setNew();
     $value = $this->object->insertBlock($block);
     $bid = $block->bid();
     $this->assertEquals($bid, $value);
     $value = $this->object->get($bid);
     $this->assertEquals($bid, $value->bid());
     $value = $this->object->deleteBlock($block);
     $this->assertSame(true, $value);
     $value = $this->object->get($bid);
     $this->assertSame(null, $value);
 }
Example #2
0
 /**
  * getForm - get block edit form
  *
  * @param string $mode mode for form, edit or clone
  *
  * @return void
  */
 public function getForm($mode = 'edit')
 {
     $xoops = Xoops::getInstance();
     $xoops->loadLanguage('blocks', 'system');
     if ($this->obj->isNew()) {
         $title = SystemLocale::ADD_BLOCK;
         $modules = array(-1);
         $groups = array(FixedGroups::USERS, FixedGroups::ANONYMOUS, FixedGroups::ADMIN);
         $this->obj->setVar('block_type', XoopsBlock::BLOCK_TYPE_CUSTOM);
         $this->obj->setVar('visible', 1);
         $op = 'save';
     } else {
         $title = '';
         $modules = array();
         // Search modules
         $blockmodulelink_handler = $xoops->getHandlerBlockModuleLink();
         $criteria = new CriteriaCompo(new Criteria('block_id', $this->obj->getVar('bid')));
         $blockmodulelink = $blockmodulelink_handler->getObjects($criteria);
         /* @var $link XoopsBlockModuleLink */
         foreach ($blockmodulelink as $link) {
             $modules[] = $link->getVar('module_id');
         }
         // Search perms
         $groupperm_handler = $xoops->getHandlerGroupPermission();
         $groups = $groupperm_handler->getGroupIds('block_read', $this->obj->getVar('bid'));
         switch ($mode) {
             case 'edit':
                 $title = SystemLocale::EDIT_BLOCK;
                 break;
             case 'clone':
                 $title = SystemLocale::CLONE_BLOCK;
                 $this->obj->setVar('bid', 0);
                 if ($this->obj->isCustom()) {
                     $this->obj->setVar('block_type', XoopsBlock::BLOCK_TYPE_CUSTOM);
                 } else {
                     $this->obj->setVar('block_type', XoopsBlock::BLOCK_TYPE_CLONED);
                 }
                 break;
         }
         $op = 'save';
     }
     parent::__construct($title, 'blockform', 'admin.php', 'post', true);
     if (!$this->obj->isNew()) {
         $this->addElement(new Xoops\Form\Label(XoopsLocale::NAME, $this->obj->getVar('name')));
     }
     // Side position
     $side_select = new Xoops\Form\Select(XoopsLocale::SIDE, 'side', $this->obj->getVar('side'));
     $side_select->addOptionArray(array(0 => XoopsLocale::LEFT, 1 => XoopsLocale::RIGHT, 3 => SystemLocale::TOP_LEFT, 4 => SystemLocale::TOP_RIGHT, 5 => SystemLocale::TOP_CENTER, 7 => SystemLocale::BOTTOM_LEFT, 8 => SystemLocale::BOTTOM_RIGHT, 9 => SystemLocale::BOTTOM_CENTER));
     $this->addElement($side_select);
     // Order
     $weight = new Xoops\Form\Text(XoopsLocale::WEIGHT, 'weight', 1, 5, $this->obj->getVar('weight'), '');
     $weight->setPattern('^\\d+$', XoopsLocale::E_YOU_NEED_A_POSITIVE_INTEGER);
     $this->addElement($weight, true);
     // Display
     $this->addElement(new Xoops\Form\RadioYesNo(XoopsLocale::VISIBLE, 'visible', $this->obj->getVar('visible')));
     // Visible In
     $mod_select = new Xoops\Form\Select(XoopsLocale::VISIBLE_IN, 'modules', $modules, 5, true);
     $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
     $criteria->add(new Criteria('isactive', 1));
     $module_list = $xoops->getHandlerModule()->getNameList($criteria);
     $module_list[-1] = XoopsLocale::TOP_PAGE;
     $module_list[0] = XoopsLocale::ALL_PAGES;
     ksort($module_list);
     $mod_select->addOptionArray($module_list);
     $this->addElement($mod_select);
     // Title
     $this->addElement(new Xoops\Form\Text(XoopsLocale::TITLE, 'title', 5, 255, $this->obj->getVar('title')), false);
     if ($this->obj->isNew() || $this->obj->isCustom()) {
         $editor_configs = array();
         $editor_configs["name"] = "content_block";
         $editor_configs["value"] = $this->obj->getVar('content', 'e');
         $editor_configs["rows"] = 15;
         $editor_configs["cols"] = 6;
         $editor_configs["editor"] = $xoops->getModuleConfig('blocks_editor', 'system');
         $this->addElement(new Xoops\Form\Editor(XoopsLocale::CONTENT, "content_block", $editor_configs), true);
         if (in_array($editor_configs["editor"], array('dhtmltextarea', 'textarea'))) {
             $ctype_select = new Xoops\Form\Select(SystemLocale::CONTENT_TYPE, 'c_type', $this->obj->getVar('c_type'));
             $ctype_select->addOptionArray(array(XoopsBlock::CUSTOM_HTML => XoopsLocale::HTML, XoopsBlock::CUSTOM_PHP => SystemLocale::PHP_SCRIPT, XoopsBlock::CUSTOM_SMILIE => SystemLocale::AUTO_FORMAT_SMILIES_ENABLED, XoopsBlock::CUSTOM_TEXT => SystemLocale::AUTO_FORMAT_SMILIES_DISABLED));
             $this->addElement($ctype_select);
         } else {
             $this->addElement(new Xoops\Form\Hidden('c_type', XoopsBlock::CUSTOM_HTML));
         }
     } else {
         if ($this->obj->getVar('template') != '') {
             $tplfile_handler = $xoops->getHandlerTplFile();
             $btemplate = $tplfile_handler->find($xoops->getConfig('template_set'), 'block', $this->obj->getVar('bid'));
             if (count($btemplate) > 0) {
                 $this->addElement(new Xoops\Form\Label(XoopsLocale::CONTENT, '<a href="' . \XoopsBaseConfig::get('url') . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate[0]->getVar('tpl_id') . '">' . SystemLocale::EDIT_TEMPLATE . '</a>'));
             } else {
                 $btemplate2 = $tplfile_handler->find('default', 'block', $this->obj->getVar('bid'));
                 if (count($btemplate2) > 0) {
                     $this->addElement(new Xoops\Form\Label(XoopsLocale::CONTENT, '<a href="' . \XoopsBaseConfig::get('url') . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate2[0]->getVar('tpl_id') . '" rel="external">' . SystemLocale::EDIT_TEMPLATE . '</a>'));
                 }
             }
         }
         if ($this->obj->getOptions() != false) {
             $this->addElement(new Xoops\Form\Label(XoopsLocale::OPTIONS, $this->obj->getOptions()));
         } else {
             $this->addElement(new Xoops\Form\Hidden('options', $this->obj->getVar('options')));
         }
         $this->addElement(new Xoops\Form\Hidden('c_type', XoopsBlock::CUSTOM_HTML));
     }
     $cache_select = new Xoops\Form\Select(SystemLocale::CACHE_LIFETIME, 'bcachetime', $this->obj->getVar('bcachetime'));
     $cache_select->addOptionArray(array('0' => XoopsLocale::NO_CACHE, '30' => sprintf(XoopsLocale::F_SECONDS, 30), '60' => XoopsLocale::ONE_MINUTE, '300' => sprintf(XoopsLocale::F_MINUTES, 5), '1800' => sprintf(XoopsLocale::F_MINUTES, 30), '3600' => XoopsLocale::ONE_HOUR, '18000' => sprintf(XoopsLocale::F_HOURS, 5), '86400' => XoopsLocale::ONE_DAY, '259200' => sprintf(XoopsLocale::F_DAYS, 3), '604800' => XoopsLocale::ONE_WEEK, '2592000' => XoopsLocale::ONE_MONTH));
     $this->addElement($cache_select);
     // Groups
     $this->addElement(new Xoops\Form\SelectGroup(XoopsLocale::GROUPS, 'groups', true, $groups, 5, true));
     $this->addElement(new Xoops\Form\Hidden('block_type', $this->obj->getVar('block_type')));
     $this->addElement(new Xoops\Form\Hidden('mid', $this->obj->getVar('mid')));
     $this->addElement(new Xoops\Form\Hidden('func_num', $this->obj->getVar('func_num')));
     $this->addElement(new Xoops\Form\Hidden('func_file', $this->obj->getVar('func_file')));
     $this->addElement(new Xoops\Form\Hidden('show_func', $this->obj->getVar('show_func')));
     $this->addElement(new Xoops\Form\Hidden('edit_func', $this->obj->getVar('edit_func')));
     $this->addElement(new Xoops\Form\Hidden('template', $this->obj->getVar('template')));
     $this->addElement(new Xoops\Form\Hidden('dirname', $this->obj->getVar('dirname')));
     $this->addElement(new Xoops\Form\Hidden('name', $this->obj->getVar('name')));
     $this->addElement(new Xoops\Form\Hidden('bid', $this->obj->getVar('bid')));
     $this->addElement(new Xoops\Form\Hidden('op', $op));
     $this->addElement(new Xoops\Form\Hidden('fct', 'blocksadmin'));
     $buttonTray = new Xoops\Form\ElementTray('', '&nbsp;');
     if ($this->obj->isNew() || $this->obj->isCustom()) {
         $preview = new Xoops\Form\Button('', 'previewblock', XoopsLocale::A_PREVIEW, 'preview');
         $preview->setExtra("onclick=\"blocks_preview();\"");
         $buttonTray->addElement($preview);
     }
     $buttonTray->addElement(new Xoops\Form\Button('', 'submitblock', XoopsLocale::A_SUBMIT, 'submit'));
     $this->addElement($buttonTray);
 }
Example #3
0
 /**
  * Blocks::buildBlock()
  *
  * @param XoopsBlock $xobject  block to render
  * @param XoopsTpl   $template template engine
  *
  * @return array|bool
  */
 public function buildBlock(XoopsBlock $xobject, XoopsTpl $template)
 {
     $xoops = \Xoops::getInstance();
     // The lame type workaround will change
     // bid is added temporarily as workaround for specific block manipulation
     $dirname = $xobject->getVar('dirname');
     $block = array('id' => $xobject->getVar('bid'), 'module' => $dirname, 'title' => $xobject->getVar('title'), 'weight' => $xobject->getVar('weight'), 'lastmod' => $xobject->getVar('last_modified'));
     $bcachetime = (int) $xobject->getVar('bcachetime');
     if (empty($bcachetime)) {
         $template->caching = 0;
     } else {
         $template->caching = 2;
         $template->cache_lifetime = $bcachetime;
     }
     $template->setCompileId($dirname);
     $tplName = ($tplName = $xobject->getVar('template')) ? "block:{$dirname}/{$tplName}" : "module:system/system_block_dummy.tpl";
     //$tplName = str_replace('.html', '.tpl', $tplName);
     $cacheid = $this->generateCacheId('blk_' . $xobject->getVar('bid'));
     $xoops->preload()->triggerEvent('core.themeblocks.buildblock.start', array($xobject, $template->isCached($tplName, $cacheid)));
     if (!$bcachetime || !$template->isCached($tplName, $cacheid)) {
         //Get theme metas
         $old = array();
         if ($this->theme && $bcachetime) {
             foreach ($this->theme->metas as $type => $value) {
                 $old[$type] = $this->theme->metas[$type];
             }
         }
         //build block
         if ($bresult = $xobject->buildBlock()) {
             $template->assign('block', $bresult);
             $block['content'] = $template->fetch($tplName, $cacheid);
         } else {
             $block = false;
         }
         //check if theme added new metas
         if ($this->theme && $bcachetime) {
             $metas = array();
             foreach ($this->theme->metas as $type => $value) {
                 $dif = \Xoops\Utils::arrayRecursiveDiff($this->theme->metas[$type], $old[$type]);
                 if (count($dif)) {
                     $metas[$type] = $dif;
                 }
             }
             if (count($metas)) {
                 \Xoops\Cache::write($cacheid, $metas);
             }
         }
     } else {
         $block['content'] = $template->fetch($tplName, $cacheid);
     }
     //add block cached metas
     if ($this->theme && $bcachetime) {
         if ($metas = \Xoops\Cache::read($cacheid)) {
             foreach ($metas as $type => $value) {
                 $this->theme->metas[$type] = array_merge($this->theme->metas[$type], $metas[$type]);
             }
         }
     }
     $template->setCompileId();
     return $block;
 }
Example #4
0
 /**
  * getAllBlocks matching selection criteria
  *
  * @param string   $rettype  what to return, values can be object, list or id
  * @param int      $side     block location (side)
  * @param int|null $visible  null for all, 0 not visible, 1 for visible only
  * @param string   $orderby  comma separated columns to order by
  * @param int      $isactive 1: active or 0:inactive blocks
  *
  * @return array
  */
 public function getAllBlocks($rettype = "object", $side = null, $visible = null, $orderby = "side,weight,bid", $isactive = 1)
 {
     $ret = array();
     $qb = $this->db2->createXoopsQueryBuilder();
     $eb = $qb->expr();
     $qb->fromPrefix('system_block', null)->where($eb->eq('isactive', $qb->createNamedParameter($isactive, \PDO::PARAM_INT)));
     if (isset($side)) {
         // get both sides in sidebox? (some themes need this)
         if ($side == XOOPS_SIDEBLOCK_BOTH) {
             $qb->andWhere($eb->in('side', array(0, 1)));
         } elseif ($side == XOOPS_CENTERBLOCK_ALL) {
             $qb->andWhere($eb->in('side', array(3, 4, 5, 7, 8, 9)));
         } else {
             $qb->andWhere($eb->eq('side', $qb->createNamedParameter($side, \PDO::PARAM_INT)));
         }
     }
     if (isset($visible)) {
         $qb->andWhere($eb->eq('visible', $qb->createNamedParameter($visible, \PDO::PARAM_INT)));
     }
     $qb->orderBy($orderby);
     switch ($rettype) {
         case "object":
             $qb->select('*');
             $result = $qb->execute();
             while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
                 $ret[] = new XoopsBlock($myrow);
             }
             break;
         case "list":
             $qb->select('*');
             $result = $qb->execute();
             while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
                 $block = new XoopsBlock($myrow);
                 $title = $block->getVar("title");
                 $title = empty($title) ? $block->getVar("name") : $title;
                 $ret[$block->getVar("bid")] = $title;
             }
             break;
         case "id":
             $qb->select('bid');
             $result = $qb->execute();
             while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
                 $ret[] = $myrow['bid'];
             }
             break;
     }
     return $ret;
 }