From 0711072c5ff3bc397acee8c461302c2c86ff856e Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 21 Jul 2015 17:05:35 -0400 Subject: [PATCH] WIP: redo for older toolboxes Have to work on ht_images for src urls --- controllers/Admin.php | 5 ++ controllers/Import.php | 170 ++++++++++++++++++++++++++++++----------- 2 files changed, 129 insertions(+), 46 deletions(-) diff --git a/controllers/Admin.php b/controllers/Admin.php index 3bbf822..354072e 100644 --- a/controllers/Admin.php +++ b/controllers/Admin.php @@ -157,6 +157,11 @@ class GlmWPImporter_Admin_Controller 'label' => 'Include Pages', 'type' => 'text' ), + array( + 'name' => 'parent_page', + 'label' => 'Parent Page', + 'type' => 'text' + ), ); foreach ($fieldNames as $field) { diff --git a/controllers/Import.php b/controllers/Import.php index 54e5641..9629851 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -201,9 +201,14 @@ } else { echo '

All Set!

'; $this->_connect(); - $sql = "SELECT * FROM toolbox.pages"; - $data = $this->_dbh->query($sql)->fetchAll(); - printf('

Found %d Pages

', count($data)); + $WHERE = $this->_getWhereSql(); + $sql = " + SELECT count(*) + FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']} + $WHERE"; + echo '
' . $sql . '
'; + $totalPages = $this->_dbh->query($sql)->fetchColumn(); + printf('

Found %d Pages

', $totalPages); echo '

Part 2

'; } } @@ -221,17 +226,32 @@ */ private function _displayImage($data, $alignment) { - $img_id = $this->_handleMediaFile($data['image'], $data['caption']); + if (!isset($data['image'])) { + return false; + } + if (isset($data['caption'])) { + $img_id = $this->_handleMediaFile($data['image'], $data['caption']); + } else if ($data['imagename']) { + $img_id = $this->_handleMediaFile($data['image'], $data['imagename']); + } else { + $img_id = $this->_handleMediaFile($data['image']); + } if (!$img_id) { return false; } $image = wp_get_attachment_image_src($img_id, 'medium'); $img_url = $image[0]; - if ($data['caption']) { + $content = ''; + if (isset($data['caption']) && $data['caption']) { $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]' .' ' . $data['caption'] . '[/caption]'; + } else if (isset($data['imagename']) && $data['imagename']) { + $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]' + .' ' + . $data['imagename'] . '[/caption]'; } else { $content .= ''; @@ -282,7 +302,7 @@ if ($post_id) { $post = get_post($post_id); } - if (!(($uploads = wp_upload_dir($time)) && false === $uploads['error'])) { + if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) { return new WP_Error('upload_error', $uploads['error']); } $filename = $this->_fetchRemoteImage($file, $uploads['path']); @@ -360,21 +380,14 @@ return $filename; } - /** - * _fetchAllPages - * - * Fetch all the toolbox pages - * - * @access private - * @return void - */ - private function _fetchAllPages($limit = null, $offset = 0) + private function _getWhereSql() { - $this->_connect(); + $WHERE = ''; $where = array(); if ($this->_options['include_pages']) { - $where[] = "id IN ({$this->_options['include_pages']}) - OR parent IN ({$this->_options['include_pages']}) "; + //$where[] = "id IN ({$this->_options['include_pages']}) + //OR parent IN ({$this->_options['include_pages']}) "; + $where[] = "id IN ({$this->_options['include_pages']})"; } if ($this->_options['exclude_pages']) { $where[] = "id NOT IN ({$this->_options['exclude_pages']}) @@ -384,6 +397,21 @@ if (!empty($where)) { $WHERE = ' WHERE ' . implode(' AND ', $where); } + return $WHERE; + } + + /** + * _fetchAllPages + * + * Fetch all the toolbox pages + * + * @access private + * @return void + */ + private function _fetchAllPages($limit = null, $offset = 0) + { + $this->_connect(); + $WHERE = $this->_getWhereSql(); $pageSql = " SELECT * FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']} @@ -391,24 +419,44 @@ ORDER BY parent,pos"; $pageSql .= " LIMIT $limit OFFSET $offset"; $pageData = $this->_dbh->query($pageSql)->fetchAll(PDO::FETCH_ASSOC); - $paragraphSql = " - SELECT * - FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']} - WHERE page = :page - ORDER BY pos"; - $paraStmt = $this->_dbh->prepare($paragraphSql); - $fileSql = " - SELECT * - FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_files_table']} - WHERE paragraph = :pid - ORDER BY pos"; - $fileStmt = $this->_dbh->prepare($fileSql); + if ($this->_options['toolbox_page_table'] == 'pages') { + $paragraphSql = " + SELECT * + FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']} + WHERE page = :page + ORDER BY pos"; + $paraStmt = $this->_dbh->prepare($paragraphSql); + $fileSql = " + SELECT * + FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_files_table']} + WHERE paragraph = :pid + ORDER BY pos"; + $fileStmt = $this->_dbh->prepare($fileSql); + } else { + $paragraphSql = " + SELECT b.* + FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']} b + LEFT OUTER JOIN bus_category_bus bcb ON (bcb.busid = b.id) + WHERE bcb.catid = :page + ORDER BY bcb.pos"; + $paraStmt = $this->_dbh->prepare($paragraphSql); + $fileSql = " + SELECT * + FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_files_table']} + WHERE bus_id IN (SELECT bus_id FROM bus_category_bus WHERE catid = :pid) + ORDER BY bus_id,pos"; + $fileStmt = $this->_dbh->prepare($fileSql); + } $data = $this->_dbh->query($pageSql)->fetchAll(); + //echo '
' . print_r($data, true) . '
'; + //exit; foreach ($data as &$page) { $page['srcs'] = array(); $paraStmt->bindParam(':page', $page['id'], PDO::PARAM_INT); $paraStmt->execute(); $paragraphs = $paraStmt->fetchAll(PDO::FETCH_ASSOC); + //echo '
' . print_r($paragraphs, true) . '
'; + //exit; $primaryAlign = 'right'; $secondaryAlign = 'right'; $alternateImg = false; @@ -435,38 +483,54 @@ $secondaryAlign = 'left'; break; } - $iterator = 1; $page['pageContent'] = ''; + $iterator = 1; + if ($this->_options['toolbox_page_table'] == 'bus_category') { + $page['pageContent'] .= '

'.$page['category'].'

'; + ++$iterator; + if ($page['image']) { + $page['images'][] = $page['image']; + $page['pageContent'] .= $this->_displayImage($page, $primaryAlign); + } + $page['pageContent'] = $page['description']; + } + foreach ($paragraphs as $paragraph) { switch ($iterator) { case 1: - if ($paragraph['title']) { + if (isset($paragraph['title']) && $paragraph['title']) { $page['pageContent'] .= '

'.$paragraph['title'].'

'; } - if ($paragraph['image']) { + if (isset($paragraph['image']) && $paragraph['image']) { $page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign); } break; case 2: - if ($paragraph['title']) { + if (isset($paragraph['name']) && $paragraph['name']) { + $page['pageContent'] .= '

'.$paragraph['name'].'

'; + } + if (isset($paragraph['title']) && $paragraph['title']) { $page['pageContent'] .= '

'.$paragraph['title'].'

'; } - if ($paragraph['image']) { + if (isset($paragraph['image']) && $paragraph['image']) { $page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign); } break; default: - if ($paragraph['title']) { + if (isset($paragraph['name']) && $paragraph['name']) { + $page['pageContent'] .= '

'.$paragraph['name'].'

'; + } + if (isset($paragraph['title']) && $paragraph['title']) { $page['pageContent'] .= '

'.$paragraph['title'].'

'; } - if ($alternateImg && $page['template'] == 3) { + if (isset($paragraph['template']) && $alternateImg && $page['template'] == 2) { $align = ($iterator%2 == 0) ? 'left' : 'right'; } else { $align = $secondaryAlign; } - if ($paragraph['image']) { + if (isset($paragraph['image']) && $paragraph['image']) { $page['images'][] = $paragraph['image']; $page['pageContent'] .= $this->_displayImage($paragraph, $align); } @@ -474,12 +538,16 @@ } $page['pageContent'] .= $paragraph['description']; preg_match_all("/' . print_r($matches, true) . ''; + for ($i = 0; $i < count($matches[0]); $i++) { - $newSrc = $this->_getDescriptionImage($matches[1][$i]); - $parsUrl = parse_url($newSrc); - $imgName = basename($parsUrl['path']); - $page['pageContent'] = str_replace($matches[1][$i], $newSrc, $page['pageContent']); - $page['srcs'][] = $imgName; + if ($matches[1][$i]) { + $newSrc = $this->_getDescriptionImage($matches[1][$i]); + $parsUrl = parse_url($newSrc); + $imgName = basename($parsUrl['path']); + $page['pageContent'] = str_replace($matches[1][$i], $newSrc, $page['pageContent']); + $page['srcs'][] = $imgName; + } } $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT); $fileStmt->execute(); @@ -504,6 +572,7 @@ private function _readOptions() { echo '
' . print_r($this->_options, true) . '
'; + echo '
' . print_r(get_option(GLM_WP_IMPORT_POST_OPTION, array()), true) . '
'; } /** @@ -521,21 +590,30 @@ // grab all pages and build the post array $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT); $pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start); + //echo '
' . print_r($pages, true) . '
'; + //exit; $numPagesImported = count($pages); foreach ($pages as $page) { if ($page['parent']) { } + $pageName = (isset($page['navigation_name'])) + ? $page['navigation_name'] + : $page['category']; + // if you don't find the parent page id then use the default given or 0 + $parent = isset($this->_post[$page['parent']]) + ? $this->_post[$page['parent']] + : (($this->_options['parent_page']) ? $this->_options['parent_page'] : 0); $post = array( 'post_content' => $page['pageContent'], - 'post_name' => $page['navigation_name'],// slug ? - 'post_title' => $page['navigation_name'], + 'post_name' => $pageName,// slug ? + 'post_title' => $pageName, 'post_status' => 'publish', 'post_type' => 'page', 'post_author' => 'steve', 'ping_status' => 'closed', - 'post_parent' => $this->_post[$page['parent']], + 'post_parent' => $parent, 'menu_order' => $page['pos'], 'comment_status' => 'closed', ); -- 2.17.1