_getTable()->insert([]); $this->_getTable()->updateByKey(['title' => 'Block ' . $blockId], $blockId); $grid = $this->getConfigArray('grid'); if (!empty($grid['defaultItems'])) { foreach ($grid['defaultItems'] as $item) { $item['blockId'] = $blockId; $this->_getTable($this->_tableAlias . 'Item')->insert($item); } } } catch (Exception $e) { $this->_addError($e->getMessage()); return false; } return $blockId; } public function updatePositions(array $items) { if (empty($items)) { $this->_addError('Grid Items are empty!'); return false; } $this->_db->beginTransaction(); try { foreach ($items as $item) { $id = $item['id']; unset($item['id']); $this->_getTable($this->_tableAlias . 'Item')->updateByKey($item, $id); } $this->_db->commit(); } catch (Exception $e) { $this->_db->rollBack(); $this->_addError($e->getMessage()); return false; } return true; } public function insertItem($width, $height, $x = 0, $y = 0, $title = '', $content = '') { $data = compact('width', 'height', 'x', 'y', 'title', 'content'); $data['blockId'] = $this->getPrimaryKey(); try { $itemsId = $this->_getTable($this->_tableAlias . 'Item')->insert($data); } catch (Exception $e) { $this->_db->rollBack(); $this->_addError($e->getMessage()); return false; } return $itemsId; } public function getItem($itemId) { return $this->_getTable($this->_tableAlias . 'Item')->search($itemId); } public function removeItem($itemId) { if (null === ($row = $this->getItem($itemId))) { $this->_addError(static::MSG_INVALID_RECORD_ID); return false; } $itemObj = new ItemObj(); $itemObj->setPrimaryKey($itemId); $result = $itemObj->delete(); if (false === $result) { $this->_addError($itemObj->getErrors()); } return $result; } public function _deleteDependency() { $this->_deleteItemsFiles(); return parent::_deleteDependency(); } protected function _deleteItemsFiles() { $this->_dependedFiles = array_merge($this->_dependedFiles, $this->_getItemsImages()); return $this; } protected function _getItemsImages() { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'Item'), ['image']); $select->where('`blockId` = ?', $this->getPrimaryKey(), Qs_Db::INT_TYPE); return $this->_db->fetchCol($select); } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['pages'] = new Zend_Db_Expr('(' . $this->_getPagesSubselect() . ')'); return $columns; } protected function _getPagesSubselect() { $select = $this->_db->select(); $col = 'GROUP_CONCAT(' . ' CONCAT_WS("' . self::GC_FIELDS_GLUE . '", `Page`.`id`, `PageContent`.`menuTitle`) ' . ' SEPARATOR "' . self::GC_ROWS_GLUE . '"' . ')'; $select->from($this->_getPair('Page'), [$col]); $select->join( $this->_getPair('PageContent'), $this->_db->quoteInto('`Page`.`id` = `PageContent`.`idPage` AND `PageContent`.`language` = ?', DEFAULT_LANGUAGE), []); $select->join( $this->_getPair('PageItem'), '`Page`.`id` = `PageItem`.`idPage` AND `PageItem`.`type` = "GridBlock\\\\"', [] ); $select->join( $this->_getPair('PageItemConfig'), '`PageItem`.`id` = `PageItemConfig`.`idItem` AND `PageItemConfig`.`name` = "blockId"', [] ); $select->where('`PageItemConfig`.`value` = `' . $this->_tableAlias . '`.`id`'); return $select; } protected function _prepareRow(array &$row) { if (!empty($row['pages'])) { $pages = explode(self::GC_ROWS_GLUE, $row['pages']); $row['pages'] = []; foreach ($pages as $page) { list($pageId, $pageTtile) = explode(self::GC_FIELDS_GLUE, $page); $row['pages'][$pageId] = [ 'title' => $pageTtile, 'url' => $this->_getCmsPageEditUrl() . '?action=edit&id=' . $pageId, ]; } } else { $row['pages'] = []; } return parent::_prepareRow($row); } protected function _getCmsPageEditUrl() { static $url; if (null === $url) { $url = Qs_SiteMap::findFirst([], ['type' => 'Cms_'], [], 'url'); } return $url; } }