Adicionar Tags ao Blog do Ning
De WikiPapagallis
Conteúdo |
Introdução
Alguns tipos de conteúdo do ning, como tópicos do fórum, vídeos e fotos têm suporte a tags criadas pelo usuário. Em uma de nossas comunidades, vimos a necessidade de permitir esta folksonomia nos Blog Posts.
Esta implementação foi feira por Lourenzo Ferreira, a partir do código dos tópicos de fórum.
Requerimentos
Você deve saber como customizar o código da sua rede ning.
Como fazer
Editar o arquivo /widgets/profiles/templates/blog/show.php
Chamadas às funções dojo
Adicione à chamada da função XG_App::ningLoaderRequire os parâmetros 'xg.shared.ActionTabs' e 'xg.shared.AutoUpdatingText'
Código
Imediatamente antes do módulo de comentários, adicione seguinte código:
<div class="xg_module_body xg_module_actionbar">
<div class="xn_actionbar">
<ul class="actions">
<li dojoType="ActionTabs"
_actionUrl="<?php echo xnhtmlentities(
$this->_buildUrl('blog','tag',
array('id' => $this->post->id, 'xn_out' => 'json')
)) ?>"
_tags="<?php echo xnhtmlentities($this->currentUserTagString) ?>"
_joinPromptText="<?php echo xnhtmlentities(XG_JoinPromptHelper::promptToJoinOnSave()) ?>"
_signUpUrl="<?php echo XG_HttpHelper::joinThenGoToProfileUrl(); ?>">
<a class="tag" href="#"><%= xg_text('TAG') %></a>
</li>
</ul>
</div>
</div>
<?php if ((count($this->tags))) { ?>
<div class="xg_module_body">
<ul class="nobullets">
<?php if ($this->tags) { ?>
<li id="tag-list"
dojoType="AutoUpdatingText"
_url="<?php echo xnhtmlentities(
$this->_buildUrl('blog','tag',
array('id' => $this->post->id, 'xn_out' => 'json'))) ?>">
<%= Profiles_HtmlHelper::tagHtmlForDetailPage($this->tags); %>
</li>
<?php } ?>
</ul>
</div>
<?php } ?>
editar /widgets/profiles/controllers/BlogController.php
Método action_show()
No método action_show() ,adicione o seguinte código antes do bloco try (linha 424)
require_once NF_APP_BASE . '/lib/XG_TagHelper.php';
$x = $this->_buildPath('lib/helpers/Profiles_HtmlHelper.php'); include_once $x;
$this->tags = XG_TagHelper::getTagNamesForObject($this->post);
$currentUserTags = $this->_user->isLoggedIn() ?
XG_TagHelper::getTagsForObjectAndUser($this->post->id, $this->_user->screenName) :
array();
$this->currentUserTagString = XG_TagHelper::implode(XN_Tag::tagNamesFromTags($currentUserTags));
Método action_tag()
Crie o método action_tag(), que irá salvar as tags no blog post. Adicione o seguinte código ao final da classe:
/**
* Sets the tags for the given object for the current user.
*
* Expected GET parameters:
* xn_out Should always be "json"
*/
public function action_tag() {
require_once NF_APP_BASE . '/lib/XG_TagHelper.php';
$x = $this->_buildPath('lib/helpers/Profiles_HtmlHelper.php'); include_once $x;
if ($_SERVER['REQUEST_METHOD'] != 'POST') { throw new Exception('Not a POST (498350803)'); }
XG_HttpHelper::trimGetAndPostValues();
$blogPost = W_Content::load($_GET['id']);
if ($blogPost->type != 'BlogPost') { throw new Exception('Not a BlogPost'); }
XG_TagHelper::setTagStringForObjectAndCurrentUser(
$blogPost,
mb_substr($_POST['tags'],
0,
XG_TagHelper::MAX_TAGS_LENGTH)
);
$this->html = Profiles_HtmlHelper::tagHtmlForDetailPage(
XG_TagHelper::getTagNamesForObject($blogPost)
);
}
Método action_listForTag()
Crie o método action_listForTag(), que irá exibir os blog posts com uma determinada tag. Adicione o seguinte código ao final da classe:
/**
* Displays a list of recent blog posts with a given tag
*
* Expected GET variables:
* page - page number (optional)
* tag - the tag
*/
public function action_listForTag() {
$query=XN_Query::create('Content')
->filter( 'owner' )
->filter( 'tag->value', 'eic', $_GET['tag']);
$posts= $query->execute();
$this->title="Blog Posts com a tag '$_GET[tag]'";
$this->posts = array('numPosts'=>$query->getTotalCount(),'posts'=>$posts);
$this->render('list');
}
criar /widgets/profiles/lib/helpers/Profiles_HtmlHelper.php
Crie o arquivo com o seguinte conteúdo:
<?php
/**
* Useful functions for HTML output.
*/
class Profiles_HtmlHelper {
/**
* Convert the tag names to a list of anchor tags separated by commas.
*
* @param array $tags The tag names
* @return string An HTML string
*/
public static function tagLinks($tags) {
$widget = W_Cache::current('W_Widget');
$links = array();
foreach ($tags as $tag) {
$links[] = '<a href="'
. xnhtmlentities(
XG_GroupHelper::buildUrl($widget->dir, 'blog', 'listForTag', array('tag' => $tag))
)
. '">'
. xnhtmlentities(xg_excerpt($tag, 30))
. '</a>';
}
return implode(', ', $links);
}
/**
* Returns HTML for the tag display for the topic-detail page.
*
* @param array $tags The topic's most popular tag names
* @return The HTML, suitable for use as innerHTML for a <p> element
*/
public static function tagHtmlForDetailPage($tags) {
return count($tags) ?
xg_html('TAGS_X', Profiles_HtmlHelper::tagLinks(array_slice($tags, 0, 10))) :
'';
}
}
