From 450e392741cb547bf4ed8ca510debac1f67c9cf4 Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 23 Dec 2020 15:03:31 -0800 Subject: [PATCH] Issue #3291 gSitemap table changes. Generic handling of link updates via e_gsitemap.php --- e107_handlers/admin_ui.php | 23 ++- e107_plugins/gsitemap/admin_config.php | 257 ++++++------------------- e107_plugins/gsitemap/e_event.php | 56 ++++-- e107_plugins/gsitemap/gsitemap_sql.php | 7 +- e107_plugins/news/e_gsitemap.php | 15 +- 5 files changed, 132 insertions(+), 226 deletions(-) diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index ae5aecac9..cccb7e3b0 100755 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -4829,12 +4829,31 @@ class e_admin_controller_ui extends e_admin_controller $pid = $this->getPrimaryName(); $_posted[$pid] = $id; // add in the primary ID field. + $table = $this->getTableName(); + $pname = $this->getPluginName(); + + if($pname === 'core') // Handler 'core' plugin value. + { + $convert = array( + 'news_category' => 'news', + 'news' => 'news', + 'page' => 'page', + 'page_chapters' => 'page', + 'user' => 'user' + ); + + if(!empty($convert[$table])) + { + $pname = $convert[$table]; + } + } + $eventData = array( // use $_posted as it may include unsaved data. 'newData' => $_posted, 'oldData' => $old_data, 'id' => $id, - 'table' => $this->getTableName(), - 'plugin' => $this->getPluginName(), + 'table' => $table, + 'plugin' => $pname, ); $this->_log('Triggering Event: ' . $triggerName); diff --git a/e107_plugins/gsitemap/admin_config.php b/e107_plugins/gsitemap/admin_config.php index e5808bb6a..2e5e9e577 100644 --- a/e107_plugins/gsitemap/admin_config.php +++ b/e107_plugins/gsitemap/admin_config.php @@ -133,6 +133,10 @@ class gsitemap_ui extends e_admin_ui $this->fields['gsitemap_freq']['writeParms']['optArray'] = $this->freqList; + if(!empty($_POST['import_links'])) + { + $this->importLink(); + } } @@ -187,6 +191,9 @@ class gsitemap_ui extends e_admin_ui public function importPage() { + + + global $PLUGINS_DIRECTORY; $ns = e107::getRender(); @@ -257,6 +264,7 @@ class gsitemap_ui extends e_admin_ui { if(!in_array($row['name'], $existing)) { + $row['plugin'] = $plug; $importArray[] = $row; } } @@ -290,7 +298,8 @@ class gsitemap_ui extends e_admin_ui $id = 'gs-'.$k; $text .= " - + ".$ia['name']." ".str_replace(SITEURL,"",$ia['url'])." @@ -501,7 +510,51 @@ $text = " */ - + function importLink() + { + + $sql = e107::getDb(); + $tp = e107::getParser(); + $log = e107::getLog(); + + + foreach ($_POST['importid'] as $import) + { + list($name, $url, $type, $plugin, $table, $id) = explode("^", $import); + + $insert = array( + 'gsitemap_id' => 0, + 'gsitemap_name' => $tp->toDB($name), + 'gsitemap_url' => $tp->toDB($url), + 'gsitemap_plugin' => $tp->toDB($plugin), + 'gsitemap_table' => $tp->toDB($table), + 'gsitemap_table_id' => (int) $id, + 'gsitemap_lastmod' => time(), + 'gsitemap_freq' => $_POST['import_freq'], + 'gsitemap_priority' => $_POST['import_priority'], + 'gsitemap_cat' => $type, + 'gsitemap_order' => '0', + 'gsitemap_img' => '', + 'gsitemap_active' => '0', + ); + + if ($sql->insert("gsitemap", $insert)) + { + e107::getMessage()->addSuccess(LAN_CREATED); + $log->add('GSMAP_01', $gsitemap->message); + } + else + { + e107::getMessage()->addError(LAN_CREATED_FAILED); + } + + // $sql->insert("gsitemap", "0, '$name', '$url', '".time()."', '".$_POST['import_freq']."', '".$_POST['import_priority']."', '$type', '0', '', '0' "); + } + + // $this->message = count($_POST['importid'])." link(s) imported."; + $log->add('GSMAP_01', $gsitemap->message); + } + } @@ -619,6 +672,7 @@ class gsitemap "never" => LAN_NEVER ); + if(isset($_POST['edit'])) { $this->editSme(); @@ -634,10 +688,7 @@ class gsitemap $this->addLink(); } - if(isset($_POST['import_links'])) - { - $this->importLink(); - } + if($this->message) @@ -659,7 +710,7 @@ class gsitemap } else if(e_QUERY == "import") { - $this->importSme(); + // $this->importSme(); } else if(e_QUERY == "instructions") { @@ -939,198 +990,6 @@ class gsitemap } } - // Import site links - function importSme() - { - global $PLUGINS_DIRECTORY; - - $ns = e107::getRender(); - $sql = e107::getDb(); - //$sql2 = e107::getDb('sql2'); not used? - $frm = e107::getForm(); - $mes = e107::getMessage(); - - $existing = array(); - $sql->select("gsitemap"); - while($row = $sql->fetch()) - { - $existing[] = $row['gsitemap_name']; - } - - - $importArray = array(); - - /* sitelinks ... */ - $sql->select("links", "*", "ORDER BY link_order ASC", "no-where"); - $nfArray = $sql->db_getList(); - foreach($nfArray as $row) - { - if(!in_array($row['link_name'], $existing)) - { - $importArray[] = array( - 'table' => 'links', - 'id' => $row['link_id'], - 'name' => $row['link_name'], - 'url' => $row['link_url'], - 'type' => GSLAN_1); - } - } - - /* custom pages ... */ - $query = "SELECT p.page_id, p.page_title, p.page_sef, p.page_chapter, ch.chapter_sef as chapter_sef, b.chapter_sef as book_sef FROM #page as p - LEFT JOIN #page_chapters as ch ON p.page_chapter = ch.chapter_id - LEFT JOIN #page_chapters as b ON ch.chapter_parent = b.chapter_id - WHERE page_title !='' ORDER BY page_datestamp ASC"; - - $data = $sql->retrieve($query,true); - - foreach($data as $row) - { - if(!in_array($row['page_title'], $existing)) - { - $route = ($row['page_chapter'] == 0) ? "page/view/other" : "page/view/index"; - - $importArray[] = array( - 'table' => 'page', - 'id' => $row['page_id'], - 'name' => $row['page_title'], - 'url' => e107::getUrl()->create($route, $row, array('full'=>1, 'allow' => 'page_sef,page_title,page_id, chapter_sef, book_sef')), - 'type' => "Page" - ); - } - } - - - - /* Plugins.. - currently: forums ... */ - $addons = e107::getAddonConfig('e_gsitemap', null, 'import'); - - foreach($addons as $plug => $config) - { - - foreach($config as $row) - { - if(!in_array($row['name'], $existing)) - { - $importArray[] = $row; - } - } - - } - - $editArray = $_POST; - - $text = " -
- - - - - - - - - - - - - - - - - "; - - foreach($importArray as $k=>$ia) - { - $id = 'gs-'.$k; - $text .= " - - - - - - - "; - } - - $text .= " - - - - -
".GSLAN_2."".LAN_TYPE."".LAN_NAME."".LAN_URL."
".$ia['name']."".str_replace(SITEURL,"",$ia['url'])."
-
".GSLAN_8."   ".GSLAN_9." :    ".GSLAN_10." - -

- -
- -
-
- ". - $frm->admin_button('import_links',GSLAN_18)." -
-
- "; - - $ns->tablerender(GSLAN_7, $mes->render(). $text); - - unset($PLUGINS_DIRECTORY); - } - - - - function importLink() - { - $sql = e107::getDb(); - $tp = e107::getParser(); - $log = e107::getLog(); - - - foreach($_POST['importid'] as $import) - { - list($name, $url, $type, $table, $id) = explode("^", $import); - - $insert = array( - 'gsitemap_id' => 0, - 'gsitemap_name' => $tp->toDB($name), - 'gsitemap_url' => $tp->toDB($url), - 'gsitemap_table' => $tp->toDB($table), - 'gsitemap_table_id' => (int) $id, - 'gsitemap_lastmod' => time(), - 'gsitemap_freq' => $_POST['import_freq'], - 'gsitemap_priority' => $_POST['import_priority'], - 'gsitemap_cat' => $type, - 'gsitemap_order' => '0', - 'gsitemap_img' => '', - 'gsitemap_active' => '0', - ); - - $sql->insert("gsitemap", $insert); - - // $sql->insert("gsitemap", "0, '$name', '$url', '".time()."', '".$_POST['import_freq']."', '".$_POST['import_priority']."', '$type', '0', '', '0' "); - } - - $this->message = count($_POST['importid'])." link(s) imported."; - $log->add('GSMAP_01',$this->message); - } - - function instructions() { diff --git a/e107_plugins/gsitemap/e_event.php b/e107_plugins/gsitemap/e_event.php index 01212e96c..d55340a05 100644 --- a/e107_plugins/gsitemap/e_event.php +++ b/e107_plugins/gsitemap/e_event.php @@ -40,37 +40,51 @@ class gsitemap_event // plugin-folder + '_event' function update($data) // the method to run. { - e107::getMessage()->addDebug("GSITEMAP TRIGGERRED!!") ; + // e107::getMessage()->addDebug("GSITEMAP TRIGGERRED!!") ; - - if(empty($data['table']) || empty($data['newData'])) + if(empty($data['plugin']) || empty($data['table']) || empty($data['newData'])) { return null; } - - switch ($data['table']) + /** @var news_gsitemap $gsmap - could be another plugin too */ + if(!$gsmap = e107::getAddon($data['plugin'], 'e_gsitemap')) { - case "news": - $update = array( - 'gsitemap_url' => e107::getUrl()->create('news/view/item', $data['newData'], array('full' => 1)), - 'WHERE' => "gsitemap_table = 'news' AND gsitemap_table_id = ".(int) $data['id'] - ); - break; - - case "page": - // code - break; - + return null; } - if(!empty($update)) + if(!method_exists($gsmap, 'url')) { - if(e107::getDb()->update('gsitemap', $update)!==false) - { - e107::getMessage()->addDebug("Gsitemap updated: ".print_a($update,true)); - } + return null; } + + if(!$url = $gsmap->url($data['newData'])) + { + return null; + } + + $tp = e107::getParser(); + $id = (int) $data['id']; + + $update = array( + 'gsitemap_url' => $url, + 'WHERE' => "gsitemap_plugin = '".$tp->filter($data['plugin'])."' + AND gsitemap_table = '".$tp->filter($data['table'])."' + AND gsitemap_table_id = ".$id + ); + + // todo LAN + $message = e107::getParser()->lanVars("Updating sitemap link for #[x] to [y]. ", array($id,$url), true); + + if(e107::getDb()->update('gsitemap', $update)!==false) + { + e107::getMessage()->addSuccess($message); + } + else + { + e107::getMessage()->addError($message); + } + } diff --git a/e107_plugins/gsitemap/gsitemap_sql.php b/e107_plugins/gsitemap/gsitemap_sql.php index a2638a7c7..f02f8237d 100644 --- a/e107_plugins/gsitemap/gsitemap_sql.php +++ b/e107_plugins/gsitemap/gsitemap_sql.php @@ -1,10 +1,11 @@ CREATE TABLE gsitemap ( gsitemap_id int(11) unsigned NOT NULL auto_increment, - gsitemap_name varchar(200) NOT NULL default '', - gsitemap_url varchar(200) NOT NULL default '', + gsitemap_name varchar(200) NOT NULL, + gsitemap_url varchar(200) NOT NULL, + gsitemap_plugin varchar(50) NOT NULL default '', gsitemap_table varchar(50) NOT NULL default '', gsitemap_table_id int(7) NOT NULL default '0', - gsitemap_lastmod varchar(15) NOT NULL default '', + gsitemap_lastmod varchar(15) NOT NULL, gsitemap_freq varchar(10) NOT NULL default '', gsitemap_priority char(3) NOT NULL default '', gsitemap_cat varchar(100) NOT NULL default '', diff --git a/e107_plugins/news/e_gsitemap.php b/e107_plugins/news/e_gsitemap.php index 5ff04cac1..167aa059d 100644 --- a/e107_plugins/news/e_gsitemap.php +++ b/e107_plugins/news/e_gsitemap.php @@ -56,11 +56,24 @@ class news_gsitemap 'id' => $row['news_id'], 'table' => 'news', 'name' => $row['news_title'], - 'url' => e107::getUrl()->create('news/view/item', $row, array('full' => 1)), + 'url' => $this->url($row), 'type' => ADLAN_0 ); } return $import; } + + /** + * Used above and by gsitemap/e_event.php to update the URL when changed in news, pages etc. + * @param $row + * @return string + */ + function url($row) + { + return e107::getUrl()->create('news/view/item', $row, array('full' => 1)); + } + + + } \ No newline at end of file