Brady Edgar
Brady Edgar

Reputation: 543

When changing templates on custom block the content gets deleted from the DB

I have a custom block for Concrete5 I built which uses multiple template files associated with it. If I apply the template to the block when I am initially adding the block to the page everything works fine. However if I then try to change templates after the block has already been set I run into issues. When saving changes with the new template, all my content gets deleted from the DB; so everything in that current row equals null except the block id "bID", the bID will change to the next increment. enter image description here

I do not know why this happens!! I feel like I ran into a similar a long time ago but don't remember how it was resolved. Any advice would be great!

My template files are just standard html in a php file with the <?php defined('C5_EXECUTE') or die("Access Denied."); ?> at the top of the file.

My controller (which is my suspect for the issue right now) look like this:

<?php

namespace Concrete\Package\ThemeCaboodle\Block\GridBlock;

use Concrete\Core\Block\BlockController;
use Database;
use Page;
use Concrete\Core\Editor\LinkAbstractor;
use Core;
use File;
use View;
use BlockType;

class Controller extends BlockController
{

public $defaultBlockClassList   = '';
public $defaultEntriesClassList = 'unit-md-4';

protected $btTable                                 = 'btGrid';
protected $btExportTables                          = array('btGrid', 'btGridEntries');
protected $btInterfaceWidth                        = "600";
protected $btWrapperClass                          = 'ccm-ui';
protected $btInterfaceHeight                       = "550";
protected $btCacheBlockRecord                      = true;
protected $btExportFileColumns                     = array('thumbnailFID');
protected $btCacheBlockOutput                      = true;
protected $btCacheBlockOutputOnPost                = true;
protected $btCacheBlockOutputForRegisteredUsers    = false;
protected $btIgnorePageThemeGridFrameworkContainer = true;

public function getBlockTypeDescription()
{
    return t("Easily add a grid with prebuilt templates to your site using the grid block");
}

public function getBlockTypeName()
{
    return t("Grid");
}

public function getFileObject($fID) {
    return File::getByID($fID);
}

public function getSearchableContent()
{
    $db      = Database::get();
    $rows    = $db->Execute('SELECT * FROM btGridEntries WHERE bID = ?', array($this->bID));
    $content = '';
    foreach ($rows as $row) {
        $content .= $row['title'].' ';
        $content .= $row['description'].' ';
    }
    return $content;
}

public function view()
{
    $this->set('entries', $this->getEntries());
    $this->set('block', $this->getBlockData());
    $this->addHeaderItem('<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>');
}

public function add()
{
    $this->requireAsset('core/file-manager');
    $this->requireAsset('core/sitemap');
    $this->requireAsset('redactor');
}

public function edit()
{
    $this->requireAsset('core/file-manager');
    $this->requireAsset('core/sitemap');
    $this->requireAsset('redactor');
    $this->set('entries', $this->getEntries());
    $this->set('block', $this->getBlockData());
}

public function composer()
{
    $this->edit();
}

public function registerViewAssets($outputContent = '')
{
    $al = \Concrete\Core\Asset\AssetList::getInstance();
    $this->requireAsset('javascript', 'jquery');
}

public function duplicate($newBID)
{
    parent::duplicate($newBID);
    $db = Database::get();
    $rows = $db->Execute('SELECT * FROM btGridEntries WHERE bID = ?', array($this->bID));
    while ($row = $rows->FetchRow()) {
        $db->execute('INSERT INTO btGridEntries (bID, thumbnailFID, fallbackFID, title, description, classList, buttonText, sortOrder, externalLinkURL, internalLinkCID, fileFID) values(?,?,?,?,?,?,?,?,?,?,?)',
            array(
                $newBID,
                $row['ENTRY_thumbnailFID'],
                $row['ENTRY_fallbackFID'],
                $row['ENTRY_title'],
                $row['ENTRY_description'],
                $row['ENTRY_classList'],
                $row['ENTRY_buttonText'],
                $row['ENTRY_sortOrder'],
                $row['ENTRY_externalLinkURL'],
                $row['ENTRY_internalLinkCID'],
                $row['ENTRY_fileFID']
            )
        );
    }
}

public function delete()
{
    $db = Database::get();
    $db->delete('btGridEntries', array('bID' => $this->bID));
    parent::delete();
}

public function save($args)
{
    $db = Database::get();
    $db->execute('DELETE from btGridEntries WHERE bID = ?', array($this->bID));
    parent::save($args);

    if (isset($args['ENTRY_sortOrder'])) {
        $count = count($args['ENTRY_sortOrder']);
        $i = 0;

        while ($i < $count) {
            $externalLinkURL = $args['ENTRY_externalLinkURL'][$i];
            $internalLinkCID = $args['ENTRY_internalLinkCID'][$i];
            $fileFID = $args['ENTRY_fileFID'][$i];
            switch (intval($args['ENTRY_linkType'][$i])) {
                case 1:
                    $externalLinkURL = '';
                    $fileFID = 0;
                    break;
                case 2:
                    $internalLinkCID = 0;
                    $fileFID = 0;
                    break;
                case 3:
                    $externalLinkURL = '';
                    $internalLinkCID = 0;
                    break;
                default:
                    $externalLinkURL = '';
                    $internalLinkCID = 0;
                    $fileFID = 0;
                    break;
            }

            if (isset($args['ENTRY_description'][$i])) {
                $args['ENTRY_description'][$i] = LinkAbstractor::translateTo($args['ENTRY_description'][$i]);
            }

            $db->execute('INSERT INTO btGridEntries (bID, thumbnailFID, fallbackFID, title, description, classList, buttonText, sortOrder, externalLinkURL, internalLinkCID, fileFID) values(?,?,?,?,?,?,?,?,?,?,?)',
                array(
                    $this->bID,
                    intval($args['ENTRY_thumbnailFID'][$i]),
                    intval($args['ENTRY_fallbackFID'][$i]),
                    $args['ENTRY_title'][$i],
                    $args['ENTRY_description'][$i],
                    $args['ENTRY_classList'][$i],
                    $args['ENTRY_buttonText'][$i],
                    $args['ENTRY_sortOrder'][$i],
                    $externalLinkURL,
                    $internalLinkCID,
                    $fileFID
                )
            );
            ++$i;
        }
    }
}

public function getBlockAssetPath() {
    $bt = BlockType::getByHandle('buckets_block');
    return Core::make('helper/concrete/urls')->getBlockTypeAssetsURL($bt);
}

public function getBlockData()
{
    $db  = Database::get();
    $row = $db->GetRow('SELECT * FROM btGrid WHERE bID = ?', array($this->bID));
    if ($row['bgFID']) {
        $row['BG'] = \File::getByID($row['bgFID'])->getVersion()->getRelativePath();
    }

    return $row;
}

public function getEntries()
{
    $v    = View::getInstance();
    $db   = Database::get();
    $rows = $db->GetAll('SELECT * FROM btGridEntries WHERE bID = ? ORDER BY sortOrder', array($this->bID));
    // in view mode, linkURL takes us to where we need to go whether it's on our site or elsewhere
    $entries = array();
    foreach ($rows as $row) {
        // Generate the URL based on what the linkType is
        if ($row['externalLinkURL'] =='' && !$row['fileFID'] && $row['internalLinkCID']) {
            $c = Page::getByID($row['internalLinkCID'], 'ACTIVE');
            $row['linkURL'] = $c->getCollectionLink();
        } elseif ($row['externalLinkURL'] =='' && !$row['internalLinkCID'] && $row['fileFID']) {
            $f = File::getByID($row['fileFID']);
            $row['linkURL'] = $f ? $f->getVersion()->getRelativePath() : '';
        } elseif ($row['externalLinkURL']!='') {
            $row['linkURL'] = $row['externalLinkURL'];
        } else {
            $row['linkURL'] = '';
        }

        // Thumbnail
        $thumbnail = $row['thumbnailFID'] ? File::getByID($row['thumbnailFID'])->getVersion()->getRelativePath() : $v->getThemePath().'/no-image.jpg';
        $row['thumbnail'] = $thumbnail;

        $fallback = $row['fallbackFID'] ? File::getByID($row['fallbackFID'])->getVersion()->getRelativePath() : $v->getThemePath().'/no-image.jpg';
        $row['fallback']    = $fallback;

        $row['description'] = LinkAbstractor::translateFrom($row['description']);
        $entries[]          = $row;
    }

    return $entries;
}

public function getClassList($string) {
    $array = explode(',',$string);
    if (count($array) > 0) {
        return implode(' ',$array);
    }
}

}

I apologize for the pretty long file in advance ;) i just want to make sure you can see all possible issues

Upvotes: 0

Views: 201

Answers (1)

Nour Akalay
Nour Akalay

Reputation: 439

I think your problem is that in the duplicate function, once you got your results back from your select you put an ENTRY_ prefix everywhere like in $row['ENTRY_thumbnailFID'] when it should really be $row['thumbnailFID'] according to your screenshot

Upvotes: 0

Related Questions