Skip to content

Commit

Permalink
文章正文改为docbook xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin Liu committed Jan 18, 2014
1 parent 884114d commit b80fda5
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/book/src/Controller/Admin/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function editAction()
array(
'id' => $id,
'bid' => $this->params('bid'),
'cdid' => $this->params('cdid'),
'title' => $row['title'],
'content' => $row['content'],
));
Expand Down
12 changes: 6 additions & 6 deletions src/book/src/Controller/Front/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use \Zend\Db\Sql\Select;
use Pi\Mvc\Controller\ActionController;
use Module\Book\XmlToHtml;

/**
* Article controller
Expand All @@ -21,17 +22,16 @@ class ArticleController extends ActionController
public function viewAction()
{
$bid = $this->params('bid');
$cdid = $this->params('cdid');

$id = $this->params('id');
$row = $this->getModel('article')->find($id);
if ($row != null) {
$article = $row->toArray();
$articleXml = $row->toArray();
$converter = new XmlToHtml;
$articleHtml = $converter->parse($articleXml['content']);
$this->view()->assign('article', $articleHtml);
}

$this->view()->assign('bid', $bid);
$this->view()->assign('cdid', $cdid);
$this->view()->assign('article', $article);
$this->view()->assign('bid', $bid);
$this->view()->setTemplate('article-view');
}

Expand Down
29 changes: 28 additions & 1 deletion src/book/src/Controller/Front/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ public function indexAction()
'controller' => 'Book',
'action' => 'list'
)
);
);
}

public function resetpasswordAction()
{
$request = $this->getRequest();

// Make sure that we are running in a console and the user has not tricked our
// application into running this action from a public web server.
if (!$request instanceof ConsoleRequest){
throw new \RuntimeException('You can only use this action from a console!');
}

// Get user email from console and check if the user used --verbose or -v flag
$userEmail = $request->getParam('userEmail');
$verbose = $request->getParam('verbose');

// reset new password
$newPassword = Rand::getString(16);

// Fetch the user and change his password, then email him ...
// [...]

if (!$verbose){
return "Done! $userEmail has received an email with his new password.\n";
}else{
return "Done! New password for user $userEmail is '$newPassword'. It has also been emailed to him. \n";
}
}
}
27 changes: 20 additions & 7 deletions src/book/src/Form/ArticleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,32 @@ public function init() {
),
));

$editorConfig = Pi::config()->load("module.article.ckeditor.php");
$editorConfig['editor'] = 'html';
$editorConfig['set'] = '';

$this->add(array(
'name' => 'content',
'options' => array(
'label' => __('Content'),
),
'attributes' => array(
'type' => 'editor',

'type' => 'textarea',
'placeholder' => __('Conent'),
'class' => 'span12',
'rows' => 20,
),
'options' => $editorConfig,
));

// $editorConfig = Pi::config()->load("module.article.ckeditor.php");
// $editorConfig['editor'] = 'html';
// $editorConfig['set'] = '';
//
// $this->add(array(
// 'name' => 'content',
// 'attributes' => array(
// 'type' => 'editor',
//
// ),
// 'options' => $editorConfig,
// ));

$this->add(array(
'name' => 'id',
'attributes' => array(
Expand Down
126 changes: 126 additions & 0 deletions src/book/src/XmlToHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
namespace Module\Book;

use Pi;
use \DOMDocument;
/**
* Description of XmlToHtml
*
* @author linliu
*/
class XmlToHtml
{
public function parse($string)
{
$rawData = $this->getRawData($string);

if (isset($rawData['article']))
{
return $this->parseArticle($rawData['article']);
}

if (isset($rawData['qandasets']))
{
return $this->parseQandA($rawData['qandasets']);
}

if (isset($rawData['chapter']))
{
return $this->parseChapter($rawData['chapter']);
}

return 'xml parse error: ' . $string;
}

private function parseChapter($array)
{
$article = array();
$article['title'] = $array['title'];

$content = false;
foreach ($array['abstract'] as $para)
{
$content = '<p>' . $para;
}

$article['content'] = $content;
return $article;
}

private function parseArticle($array)
{
$article = array();
$article['title'] = $array['title'];
$content = '<center><h4>' . $array['subtitle'] . '</h4></center>';
$content .= '<blockquote><strong>' . $array['abstract']['#text'] . ':</strong>';

$i = 0;
foreach ($array['para'] as $para)
{
if (++$i == 1)
{
$content .= $para . '</blockquote>';
}
else
{
$content .= '<p>' . $para;
}

}
$article['content'] = $content;
return $article;
}

private function parseQandA($array)
{
$article = array();
$article['title'] = $array['title'];

$content = '';
foreach ($array['qandaentry'] as $qa)
{
$content .= $qa['question']['person'] . '&nbsp;发表于&nbsp;' . $qa['question']['time'] . ':&nbsp;' . $qa['question']['para'] . '<p>';
$content .= $qa['answer']['person'] . '&nbsp;发表于&nbsp;' . $qa['answer']['time'] . ':&nbsp;' . $qa['answer']['para'] . '<p><p>';
}
$article['content'] = $content;

return $article;
}

private function getRawData($string)
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadXML($string);
$dom->removeChild($dom->firstChild); // 去掉开始的元数据
return $this->parseNode($dom);
}

private function parseNode($node)
{
static $textNode = array('para', 'person', 'time', 'title', 'subtitle');

if (in_array($node->nodeName, $textNode) || !$node->hasChildNodes())
{
return $node->nodeValue;
}

$array = array();
foreach ($node->childNodes as $childNode)
{
$array[$childNode->nodeName][] = $this->parseNode($childNode);
}

foreach($array as $key => $value)
{
if (count($value) == 1)
$array[$key] = $value[0];
}
return $array;
}
}
13 changes: 1 addition & 12 deletions src/book/template/admin/article-edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
echo $this->formElement($element);
$element = $form->get('bid');
echo $this->formElement($element);
$element = $form->get('cdid');
echo $this->formElement($element);
$element = $form->get('fake_id');
echo $this->formElement($element);
?>
Expand All @@ -64,16 +62,7 @@
<strong>
<?php
$url = $this->url('', array('controller' => 'catalogue', 'action' => 'edit', 'bid' => $form->get('bid')->getValue()));
$link = sprintf('<a href="%s" title="%s">%s</a>', $url, __('Edit Catalogue'), __('Edit Catalogue'));
echo $link;
?>
</strong>
</div>
<div class="span2">
<strong>
<?php
$url = $this->url('', array('controller' => 'catalogue', 'action' => 'edit-item', 'bid' => $form->get('bid')->getValue(), 'cdid' => $form->get('cdid')->getValue()));
$link = sprintf('<a href="%s" title="%s">%s</a>', $url, __('Edit Catalogue Item'), __('Edit Catalogue Item'));
$link = sprintf('<a href="%s" title="%s">%s</a>', $url, __('Go to Catalogue'), __('Go to Catalogue'));
echo $link;
?>
</strong>
Expand Down
18 changes: 5 additions & 13 deletions src/book/template/admin/catalogue-edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
<i class="icon-edit"></i>
<?php _e('Save menu'); ?>
</span>
<div class="input-append" style="margin-left: 30px;">
<input type="text" placeholder="Label" class="js-add-label">
<span class="btn js-add-to">
<i class="icon-plus"></i>
<?php _e('Add to menu'); ?>
</span>
</div>
</div>
<div class="menu-to-edit"></div>
</div>
Expand Down Expand Up @@ -51,8 +44,7 @@
<span class="btn btn-small js-toggle" title="<?php _e('Click to toggle edit'); ?>">
<?php _e('Edit'); ?>
</span>
<button class="btn btn-small js-manage">Manage</button>
<button class="btn btn-small js-delete">×</button>
<button class="btn btn-small js-view">View</button>
</div>
</div>
<div class="tree-item-body">
Expand Down Expand Up @@ -111,7 +103,7 @@
events: {
'click .js-toggle': 'toggle',
'click .js-delete': 'deleteItem',
'click .js-manage': 'manageArticle',
'click .js-view': 'viewArticle',
'click .tree-item-save': 'saveItem'
},
initialize: function() {
Expand Down Expand Up @@ -150,9 +142,9 @@
this.$('[name=url]').tooltip();
return this;
},
manageArticle: function() {
var cdid = this.model.get('id');
window.location.href = "<?php echo $this->url('', array('controller' => 'catalogue', 'action' => 'editItem', 'bid' => $bid));?>" + '/cdid/' + cdid;
viewArticle: function() {
var id = this.model.get('id');
window.location.href = "<?php echo $this->url('', array('controller' => 'article', 'action' => 'edit', 'bid' => $bid));?>" + '/id/' + id;
}
});
var MenuListItemView = Backbone.View.extend({
Expand Down
10 changes: 0 additions & 10 deletions src/book/template/front/article-view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ $this->js(Pi::url('static/vendor/bootstrap/js/bootstrap.js'));
</div>
</div>

<div>
<strong>
<?php
$url = $this->url('', array('controller' => 'article', 'action' => 'list', 'bid' => $bid, 'cdid' => $cdid));
$link = sprintf('<a href="%s" title="%s">%s</a>', $url, __('Back to Catalogue Article list'), __('Back to Catalogue Article list'));
echo $link;
?>
</strong>
</div>

<div>
<strong>
<?php
Expand Down
4 changes: 2 additions & 2 deletions src/book/template/front/book-view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ $this->js(Pi::url('static/vendor/bootstrap/js/bootstrap.js'));
?>
<div>
<?php
$url = $this->url('', array('controller' => 'article', 'action' => 'list', 'bid' => $book['id'], 'cdid' => $item->id));
$url = $this->url('', array('controller' => 'article', 'action' => 'view', 'bid' => $book['id'], 'id' => $item->id));
$link = sprintf('<a href="%s" title="%s">%s</a>', $url, __('View catalogue'), $item->label);
for ($i = 0; $i < $item->depth; $i++)
echo '&nbsp;&nbsp;&nbsp;&nbsp';
echo '|—' . $link;
echo '|—&nbsp;&nbsp;' . $link;
?>
</div>
<?php
Expand Down

0 comments on commit b80fda5

Please sign in to comment.