public function importPost($post, $dryrun = true) { // If the mapping is not defined, ignore it. if (empty($this->config['mapping'][$post['post_type']])) { return "<p>No mapping defined for posttype '" . $post['post_type'] . "'.</p>"; } // Find out which mapping we should use. $mapping = $this->config['mapping'][$post['post_type']]; // If the mapped contenttype doesn't exist in Bolt. if (!$this->app['storage']->getContentType($mapping['targetcontenttype'])) { return "<p>Bolt contenttype '" . $mapping['targetcontenttype'] . "' for posttype '" . $post['post_type'] . "' does not exist.</p>"; } // Create the new Bolt Record. $record = new \Bolt\Content($this->app, $mapping['targetcontenttype']); // 'expand' the postmeta fields to regular fields. if (!empty($post['postmeta']) && is_array($post['postmeta'])) { foreach ($post['postmeta'] as $id => $keyvalue) { $post[$keyvalue['key']] = $keyvalue['value']; } } // Iterate through the mappings, see if we can find it. foreach ($mapping['fields'] as $from => $to) { if (isset($post[$from])) { // It's present in the fields. $value = $post[$from]; switch ($from) { case "post_parent": if (!empty($value)) { $value = $mapping['fields']['post_parent_contenttype'] . "/" . $value; } break; case "post_date": if (!empty($value)) { // WXR seems to use only one date value. $record->setValue('datechanged', $value); $record->setValue('datecreated', $value); $record->setValue('datepublish', $value); } break; } switch ($to) { case "username": $value = makeSlug($value); break; case "status": if ($value == "publish") { $value = "published"; } if ($value == "future") { $value = "timed"; } break; } $record->setValue($to, $value); } } // Perhaps import the categories as well.. if (!empty($mapping['category']) && !empty($post['terms'])) { foreach ($post['terms'] as $term) { if ($term['domain'] == 'category') { $record->setTaxonomy($mapping['category'], $term['slug']); if (!in_array($term['slug'], $this->foundcategories)) { $this->foundcategories[] = $term['slug']; } } } } if ($dryrun) { $output = "<p>Original WXR Post <b>\"" . $post['post_title'] . "\"</b> -> Converted Bolt Record :</p>"; $output .= \util::var_dump($post, true); $output .= \util::var_dump($record, true); $output .= "\n<hr>\n"; } else { $this->app['storage']->saveContent($record); $output = "Import: " . $record->get('id') . " - " . $record->get('title') . " <small><em>"; $output .= $this->memUsage() . "mb.</em></small><br>"; } return $output; }
/** * Edit a unit of content, or create a new one. */ function editcontent($contenttypeslug, $id, Silex\Application $app, Request $request) { $contenttype = $app['storage']->getContentType($contenttypeslug); if ($request->getMethod() == "POST") { $content = new \Bolt\Content('', $contenttypeslug); $content->setFromPost($request->request->all(), $contenttype); if ($app['storage']->saveContent($content, $contenttype['slug'])) { if (!empty($id)) { $app['session']->setFlash('success', "The changes to this " . $contenttype['singular_name'] . " have been saved."); } else { $app['session']->setFlash('success', "The new " . $contenttype['singular_name'] . " has been saved."); } $app['log']->add($content->getTitle(), 2, $content, 'save content'); return redirect('overview', array('contenttypeslug' => $contenttype['slug'])); } else { $app['session']->setFlash('error', "There was an error saving this " . $contenttype['singular_name'] . "."); $app['log']->add("Save content error", 2, $content, 'error'); } } if (!empty($id)) { $content = $app['storage']->getSingleContent($contenttype['slug'], array('id' => $id)); $app['twig']->addGlobal('title', "Edit " . $contenttype['singular_name'] . " » " . $content->getTitle()); $app['log']->add("Edit content", 1, $content, 'edit'); } else { $content = $app['storage']->getEmptyContent($contenttype['slug']); $app['twig']->addGlobal('title', "New " . $contenttype['singular_name']); $app['log']->add("New content", 1, $content, 'edit'); } if (!empty($_GET['duplicate'])) { $content->setValue('id', ""); $content->setValue('datecreated', ""); $content->setValue('datepublish', ""); $content->setValue('datechanged', ""); $content->setValue('username', ""); $app['session']->setFlash('info', "Content was duplicated. Click 'Save " . $contenttype['singular_name'] . "' to finalize."); } // Set the users and the current owner of this content. if ($content->get('username') != "") { $contentowner = $content->get('username'); } else { $user = $app['session']->get('user'); $contentowner = $user['username']; } return $app['twig']->render('editcontent.twig', array('contenttype' => $contenttype, 'content' => $content, 'contentowner' => $contentowner)); }