tableName = DB_PREFIX_SERVICE.'Doc'; $this->id_parent = intval($id_parent); $this->DBObj($id); } function _prepSiteMap($siteMap) { foreach ($siteMap as $pageid => $page){ $siteMap[$pageid] = $this->_prepPage($page); if (is_array($siteMap[$pageid]['sub'])) { $siteMap[$pageid]['sub'] = $this->_prepSiteMap($page['sub']); } } return $siteMap; } function _prepPage($page) { if (!is_array($page['items']) || empty($page['items'])) { return $page; } $items = $page['items']; foreach ($items as $type => $value) { if (strpos($type, 'HTMLBlock/Show') === false){ unset($page['items'][$type]); } } return $page; } function getUserendSiteMap($deniedPages = array()) { /** @var CMS_SiteMapShowView $SiteMapShow */ $SiteMapShow = SiteMap::getObj('CMS/SiteMapShow/SiteMapShowView.php'); $siteMap = $SiteMapShow->getSiteMap('', 0, false, $deniedPages); $siteMap = $this->_prepSiteMap($siteMap); return $siteMap; } function getSiteMap() { $SiteMapShow = SiteMap::getObj('CMS/SiteMapShow/SiteMapShowView.php'); $siteMap = $SiteMapShow->getSiteMap('', 0, true); $siteMap = $this->_prepSiteMap($siteMap); $this->total_pages = array_sum($SiteMapShow->cnt_rows); return $siteMap; } function getPagentPages4Sel($siteMap = false, $lev = 1) { if (false === $siteMap){ $siteMap = $this->getSiteMap(); $all = array(0 => 'Root'); }else { $all = array(); } foreach ($siteMap AS $page){ if ($page['id'] != $this->id){ $all[ $page['id'] ] = str_repeat('  ', $lev).$page['title']; $all = $all + $this->getPagentPages4Sel($page['sub'], $lev +1); } } return $all; } function _getFirstDoc4Parent($id_parent) { $id_parent = intval($id_parent); $sql = "SELECT alias FROM {$this->tableNameDB} WHERE id_parent = ".$this->db->quote($id_parent). " ORDER BY sorter"; return $this->db->queryOne($sql); } function initFromDB() { parent::initFromDB(); $this->id_parent = $this->getData('id_parent'); return true; } function _getPageidByAlias($alias, $chItms = true) { require_once('class/Form/Rule/RegexApp.php'); foreach (explode('/', $alias) as $key => $aliasLocal) { if (!preg_match(HTML_QuickForm_Rule_RegexApp::getRegex('alias'), $aliasLocal) && !empty($aliasLocal)) { return null; } } $id_parent = 0; $id_parent_final = null; $sqlTpl = "SELECT * FROM {$this->tableNameDB} WHERE id_parent = %s AND alias = %s "; foreach (explode('/', $alias) as $aliasLocal) { $sql = sprintf($sqlTpl, $this->db->quote($id_parent), $this->db->quote($aliasLocal) ); $docArr = $this->db->queryRow($sql); $id_parent = $id_doc = intval( $docArr['id']); if ('y' == $docArr['final']){ $id_parent_final = $id_doc; }elseif ('n' == $docArr['final']) { $id_parent_final = null; } if (empty($id_doc) && !empty($alias) ){ return $id_parent_final; } } if ($chItms){ $this->_initItem($id_doc); if ( count($this->item->getList()) == 0 ) { $aliasLocal = $alias; if (!empty($aliasLocal)){ $aliasLocal .= '/'; } $aliasLocal .= $this->_getFirstDoc4Parent($id_parent); return $this->_getPageidByAlias($aliasLocal); } } return $id_doc; } function _getPath2Doc($id_doc) { $aliasToPage = ''; $sql = "SELECT * FROM {$this->tableNameDB} WHERE id = %s"; do{ $docArr = $this->db->queryRow(sprintf($sql, $this->db->quote($id_doc) )); $aliasToPage = $docArr['alias'].'/'.$aliasToPage; $id_doc = $docArr['id_parent']; }while ($docArr['id_parent'] != 0); return substr($aliasToPage, 0, -1); } function getPageByAlias($alias) { $lang = array_shift(explode('/', $alias)); require_once 'class/DB/Lang/DLang.php'; $DLang = new DLang(); $lang = $DLang->validName($lang); $aliasInLang = preg_replace("/^$lang\/?/", '', $alias); //vdie($lang, $aliasInLang); if (is_null($id = $this->_getPageidByAlias($aliasInLang)) ){ $notFound = siteMap::get404Doc($aliasFull, 'SiteDoc'); $notFound['lang'] = $lang; return $notFound; }; $data = $this->getFromDB($id); $data['CURR_PAGE'] = $this->_getPath2Doc($id); $restAlias = substr($aliasInLang, strlen($data['CURR_PAGE']) + 1 ); $data['title'] = $data['title'][$lang]; $data['keyword'] = $data['keyword'][$lang]; $data['description'] = $data['description'][$lang]; $data['head'] = $data['head'][$lang]; $data['right_content'] = $data['right_content'][$lang]; $data['docHandler'] = $data['handler']; $data['item'] = array(); $this->_initItem($id); foreach ($data['items'] as $item => $value) { list($type, $name) = explode($this->item->getDelimiter(), $item); $data['item'][] = array( 'type' => $type, 'name' => $name, 'lang' => $lang, '_restAlias' => $restAlias, ); } unset($data['items']); $data['lang'] = $lang; return $data; } function getTypes() { $tableName = 'tbl'.$this->tableName.'Handler'; $sql = "SELECT name, title FROM {$this->db->$tableName} where enabled = 'y'"; return $this->db->queryAll($sql, null, MDB2_FETCHMODE_ASSOC, true); } function _getWhat4Grid() { return parent::_getWhat4Grid().", {$this->tableName}Handler.title AS handler, {$this->tableName}Title.menutitle, {$this->tableName}Title.title AS title"; } function _getWhat4SiteMap() { return parent::_getWhat4Grid().", IF ({$this->tableName}Title.menutitle<>'', {$this->tableName}Title.menutitle, {$this->tableName}Title.title) AS title, COUNT({$this->tableName}Item.id_doc) as block_cnt "; } function _initContent($id = null) { if (is_null($id)){ $id = $this->id; } require_once('class/DB/Lang/Content.php'); $this->title = new LangContent($this->tableName.'Title', $id, array('title','menutitle','keyword','description', 'header', 'head', 'right_content')); } function insert($data = null) { parent::insert($data); $this->_initContent(); $this->title->save($this->_data); $this->_initItem(); $this->item->save($this->_data['items']); $this->_setSorter4Ins(); return $this->id; } function _setSorter4Ins() { $sql = "SELECT MAX(sorter) FROM {$this->tableNameDB} WHERE id_parent = ".$this->db->quote($this->id_parent); $sorter = $this->db->queryOne($sql) + 1; $this->table->update(array('sorter' => $sorter), 'id = '.$this->db->quote($this->id) ); return true; } function initFromArray($data, $files = null) { $data['id_parent'] = $this->id_parent; parent::initFromArray($data, $files); } function delete() { $id = $this->id; $sql = "SELECT * FROM {$this->tableNameDB} WHERE id_parent = ".$this->db->quote($this->id); $subPages = $this->db->queryAll($sql); foreach ($subPages as $subPage) { $this->id = $subPage['id']; $this->delete(); } $this->id = $id; parent::delete(); $this->_initItem(); $this->item->delete(); $this->_initContent(); $this->title->delete(); } function update($data = null, $files = null) { parent::update($data); $this->_initContent(); $this->title->save($this->_data); $this->_initItem(); $this->item->save($this->_data['items']); return true; } function getFromDB($id, $field = false) { $res = (array)parent::getFromDB($id); $this->_initContent($id); $res = $res + $this->title->get(); $this->_initItem($id); $res['items'] = $this->item->getList(); return $this->getSubElem($res, $field); } function _initItem($id_doc = null) { if (is_null($id_doc)){ $id_doc = $this->id; } $this->item = SiteMap::getObj('CMS/Doc/DocItem.php', $id_doc); return true; } function getItemNames() { $this->_initItem(); return $this->item->getNames(); } function _getWhere4Grid($opt = array()) { $where = " WHERE {$this->tableName}.id_parent = ".$this->db->quote($this->id_parent); if (count($opt['addonWhere'])) { foreach ($opt['addonWhere'] as $key => $value) { $where .= " AND " . $key . " = '" . $value . "'"; } } return $where; } function _getWhere4SiteMap($opt = array()) { $where = " WHERE 1 "; if (count($opt['addonWhere'])) { foreach ($opt['addonWhere'] as $key => $value) { $where .= " AND " . $key . " = " . $this->db->quote($value) . ""; } } if (!empty($opt['addonWhereSql'])) { $where .= $opt['addonWhereSql']; } return $where; } function _getJoin4Grid($opt = array()) { $tblDLang = 'tbl'.DB_PREFIX_SERVICE.'DLang'; $join = " LEFT JOIN {$this->tableNameDB}Handler AS {$this->tableName}Handler ON {$this->tableName}Handler.name = {$this->tableName}.handler LEFT JOIN {$this->tableNameDB}Title AS {$this->tableName}Title ON {$this->tableName}Title.id_parent = {$this->tableName}.id INNER JOIN {$this->db->$tblDLang} AS DLang ON DLang.name = {$this->tableName}Title.lang "; if (!isset($opt['lang'])){ $join .= " AND DLang.def = 'y' "; }else { $join .= " AND DLang.name = ".$this->db->quote($opt['lang']); } return $join; } function _getJoin4SiteMap($opt = array()) { $join = $this->_getJoin4Grid($opt = array()); $join .= " LEFT JOIN {$this->tableNameDB}Item as {$this->tableName}Item ON {$this->tableName}Item.id_doc = {$this->tableName}.id "; return $join; } function _getGroup4SiteMap() { return "GROUP BY {$this->tableName}.id"; } function getIdPath($lang = null) { $list = array(); if ($this->id_parent){ $id_parent = $this->id_parent; $sql = "SELECT {$this->tableName}.*, {$this->tableName}Title.title, {$this->tableName}Title.lang FROM {$this->tableNameDB} AS {$this->tableName} "; $sql .= $this->_getJoin4Grid(array('lang' => $lang)); $sql .= " WHERE {$this->tableName}.id = %s "; do { $data = $this->db->queryRow(sprintf($sql, $id_parent)); $id_parent = $data['id_parent']; if ($data['id']){ $list[] = $data; } }while ($id_parent); } return array_reverse($list); } function isAliasUnique($alias) { if (empty($this->id_parent)){ if (file_exists(WWW_PATH.'/'.$alias)){ return false; } require_once 'class/SiteMap/SiteMap.php'; $SiteMap = new SiteMap(); $docArr = $SiteMap->getArr($alias); if (empty($docArr['notFoundFlag']) && !$docArr['can_duplicate'] ){ return false; } } $sql = "SELECT COUNT(*) FROM {$this->tableNameDB} WHERE id_parent = ".$this->db->quote($this->id_parent). ' AND id != '.$this->db->quote(intval($this->id)). ' AND alias = '.$this->db->quote($alias); if ($this->db->queryOne($sql)){ return false; } return true; } function reorder($list) { $this->_initTable(); foreach ((array)$list as $sorter => $id) { $this->table->update(array('sorter' => $sorter), 'id = '.$this->db->quote($id).' AND id_parent = '.$this->db->quote($this->id_parent) ); } return true; } function getItemsList4Post($ids) { require_once(SiteMap::getPath('CMS/Doc/DocItem.php')); $list = array(); $types = $this->db->queryAll("SELECT name, title FROM {$this->tableNameDB}ItemType ", null, MDB2_FETCHMODE_ASSOC, true); foreach ($ids as $item_id){ list($type, $name) = explode(DocItem::getDelimiter(), $item_id); $list[$item_id] = $types[$type].DocItem::getDelimiter(). $name; } return $list; } function getList4SiteMap($opt) { $sql = "SELECT ".$this->_getWhat4SiteMap($opt)." FROM {$this->tableNameDB} AS {$this->tableName} ". $this->_getJoin4SiteMap($opt).' '. $this->_getWhere4SiteMap($opt).' '. $this->_getGroup4SiteMap($opt).' '. $this->_getHaving4Grid($opt).' '. $this->_getOrder4Grid($opt); $res = $this->db->queryAll($sql); if (PEAR::isError($res)) { $GLOBALS['_DEBUG']['level'] = D_ECHO; } $this->listParseUrl($res); return $res; } function getItems4SiteMap() { $sql = " SELECT * FROM {$this->db->tblzz_DocItem} as DocItem "; return $this->db->queryAll($sql); } } ?>