Browse Source

Implement tag support in posts and new "Tag" public page

pull/16/merge
Jaussoin Timothée 10 years ago
parent
commit
531ea62826
  1. 11
      app/controllers/TagController.php
  2. 41
      app/models/postn/Postn.php
  3. 31
      app/models/postn/PostnDAO.php
  4. 20
      app/models/tag/Tag.php
  5. 56
      app/models/tag/TagDAO.php
  6. 7
      app/views/tag.tpl
  7. 24
      app/widgets/Blog/Blog.php
  8. 24
      app/widgets/Blog/blog.tpl
  9. 13
      app/widgets/Group/_group_posts.tpl
  10. 13
      app/widgets/Post/_post.tpl
  11. 13
      app/widgets/Publish/Publish.php
  12. 14
      app/widgets/Publish/_publish_create.tpl
  13. 1
      bootstrap.php
  14. 1
      locales/locales.ini
  15. 1
      system/Route.php

11
app/controllers/TagController.php

@ -0,0 +1,11 @@
<?php
class TagController extends BaseController {
function load() {
$this->session_only = false;
}
function dispatch() {
$this->page->setTitle(__('page.tag'));
}
}

41
app/models/postn/Postn.php

@ -24,7 +24,6 @@ class Postn extends Model {
public $updated; //
public $delay; //
public $tags; // Store the tags
public $picture; // Tell if the post contain embeded pictures
public $lat;
@ -80,8 +79,6 @@ class Postn extends Model {
{"type":"text" },
"picture" :
{"type":"int", "size":4 },
"tags" :
{"type":"text" },
"hash" :
{"type":"string", "size":128 }
}';
@ -186,19 +183,24 @@ class Postn extends Model {
// Tags parsing
if($entry->entry->category) {
$this->tags = array();
$td = new \Modl\TagDAO;
if($entry->entry->category->count() == 1
&& isset($entry->entry->category->attributes()->term))
array_push($this->tags, (string)$entry->entry->category->attributes()->term);
else
foreach($entry->entry->category as $cat)
array_push($this->tags, (string)$cat->attributes()->term);
&& isset($entry->entry->category->attributes()->term)) {
$tag = new \Modl\Tag;
$tag->nodeid = $this->__get('nodeid');
$tag->tag = (string)$entry->entry->category->attributes()->term;
$td->set($tag);
} else {
foreach($entry->entry->category as $cat) {
$tag = new \Modl\Tag;
$tag->nodeid = $this->__get('nodeid');
$tag->tag = (string)$cat->attributes()->term;
$td->set($tag);
}
}
}
if(!empty($this->tags))
$this->__set('tags', serialize($this->tags));
if($contentimg != '')
$content .= '<br />'.$contentimg;
@ -366,6 +368,21 @@ class Postn extends Model {
}
}
public function getTags()
{
$td = new \Modl\TagDAO;
$tags = $td->getTags($this->nodeid);
return array_map(function($tag) { return $tag->tag; }, $tags);
}
public function getTagsImploded()
{
$tags = $this->getTags();
if(is_array($tags)) {
return implode(', ', $tags);
}
}
public function isPublic() {
if(isset($this->privacy) && $this->privacy) {
return true;

31
app/models/postn/PostnDAO.php

@ -26,7 +26,6 @@ class PostnDAO extends SQL {
links = :links,
picture = :picture,
tags = :tags,
hash = :hash
@ -57,7 +56,6 @@ class PostnDAO extends SQL {
'links' => $post->links,
'picture' => $post->picture,
'tags' => $post->tags,
'hash' => $post->hash,
@ -97,7 +95,6 @@ class PostnDAO extends SQL {
links,
picture,
tags,
hash)
values(
@ -125,7 +122,6 @@ class PostnDAO extends SQL {
:links,
:picture,
:tags,
:hash
)';
@ -153,7 +149,6 @@ class PostnDAO extends SQL {
'links' => $post->links,
'picture' => $post->picture,
'tags' => $post->tags,
'hash' => $post->hash,
@ -210,8 +205,8 @@ class PostnDAO extends SQL {
and postn.node != \'urn:xmpp:microblog:0\'
order by postn.published desc';
if($limitr)
$this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
if($limitr !== false)
$this->_sql = $this->_sql.' limit '.(int)$limitr.' offset '.(int)$limitf;
$this->prepare(
'Postn',
@ -225,6 +220,28 @@ class PostnDAO extends SQL {
return $this->run('ContactPostn');
}
function getPublicTag($tag, $limitf = false, $limitr = false) {
$this->_sql = '
select *, postn.aid, privacy.value as privacy from postn
left outer join contact on postn.aid = contact.jid
left outer join privacy on postn.nodeid = privacy.pkey
where nodeid in (select nodeid from tag where tag = :title)
and privacy.value = 1
order by postn.published desc';
if($limitr !== false)
$this->_sql = $this->_sql.' limit '.(int)$limitr.' offset '.(int)$limitf;
$this->prepare(
'Postn',
array(
'title' => $tag # Hack
)
);
return $this->run('ContactPostn');
}
function getNodeUnfiltered($from, $node, $limitf = false, $limitr = false) {
$this->_sql = '
select *, postn.aid, privacy.value as privacy from postn

20
app/models/tag/Tag.php

@ -0,0 +1,20 @@
<?php
namespace modl;
class Tag extends Model {
public $tag;
public $nodeid;
public function __construct() {
$this->_struct = '
{
"tag" :
{"type":"string", "size":64, "mandatory":true, "key":true },
"nodeid" :
{"type":"string", "size":96, "mandatory":true, "key":true }
}';
parent::__construct();
}
}

56
app/models/tag/TagDAO.php

@ -0,0 +1,56 @@
<?php
namespace modl;
class TagDAO extends SQL {
function set(Tag $t) {
$this->_sql = '
update tag
set nodeid = :nodeid,
tag = :tag
where nodeid = :nodeid
and tag = :tag';
$this->prepare(
'Tag',
array(
'nodeid' => $t->nodeid,
'tag' => $t->tag
)
);
$this->run('Tag');
if(!$this->_effective) {
$this->_sql = '
insert into tag
(nodeid, tag)
values (:nodeid, :tag)';
$this->prepare(
'Tag',
array(
'nodeid' => $t->nodeid,
'tag' => $t->tag
)
);
$this->run('Tag');
}
}
function getTags($nodeid) {
$this->_sql = '
select * from tag
where nodeid = :nodeid';
$this->prepare(
'Tag',
array(
'nodeid' => $nodeid
)
);
return $this->run('Tag');
}
}

7
app/views/tag.tpl

@ -0,0 +1,7 @@
<main>
<section>
<div style="background-color: #EEE;">
<?php $this->widget('Blog');?>
</div>
</section>
</main>

24
app/widgets/Blog/Blog.php

@ -13,6 +13,7 @@ class Blog extends WidgetBase {
private $_messages;
private $_page;
private $_mode;
private $_tag;
function load()
{
@ -27,6 +28,10 @@ class Blog extends WidgetBase {
$this->_mode = 'group';
$this->url = Route::urlize('node', array($this->_from, $this->_node));
} elseif($this->_view == 'tag' && $this->validateTag($this->get('t'))) {
$this->_mode = 'tag';
$this->_tag = $this->get('t');
$this->title = '#'.$this->_tag;
} else {
$this->_from = $this->get('f');
@ -46,7 +51,11 @@ class Blog extends WidgetBase {
if($this->_id = $this->get('i')) {
if(Validator::int()->between(0, 100)->validate($this->_id)) {
$this->_messages = $pd->getNodeUnfiltered($this->_from, $this->_node, $this->_id * $this->_paging, $this->_paging + 1);
if(isset($this->_tag)) {
$this->_messages = $pd->getPublicTag($this->get('t'), $this->_id * $this->_paging, $this->_paging + 1);
} else {
$this->_messages = $pd->getNodeUnfiltered($this->_from, $this->_node, $this->_id * $this->_paging, $this->_paging + 1);
}
$this->_page = $this->_id + 1;
} elseif(Validator::string()->length(5, 100)->validate($this->_id)) {
$this->_messages = $pd->getPublicItem($this->_from, $this->_node, $this->_id);
@ -73,7 +82,11 @@ class Blog extends WidgetBase {
}
} else {
$this->_page = 1;
$this->_messages = $pd->getNodeUnfiltered($this->_from, $this->_node, 0, $this->_paging + 1);
if(isset($this->_tag)) {
$this->_messages = $pd->getPublicTag($this->get('t'), 0, $this->_paging + 1);
} else {
$this->_messages = $pd->getNodeUnfiltered($this->_from, $this->_node, 0, $this->_paging + 1);
}
}
if(count($this->_messages) == $this->_paging + 1) {
@ -91,6 +104,8 @@ class Blog extends WidgetBase {
$this->view->assign('mode', $this->_mode);
$this->view->assign('more', $this->_page);
$this->view->assign('posts', $this->_messages);
$this->view->assign('tag', $this->_tag);
}
private function validateServerNode($server, $node)
@ -104,6 +119,11 @@ class Blog extends WidgetBase {
else return true;
}
private function validateTag($tag)
{
return Validator::string()->notEmpty()->alnum()->validate($tag);
}
function getComments($post)
{
$pd = new \Modl\PostnDAO();

24
app/widgets/Blog/blog.tpl

@ -31,6 +31,17 @@
<p>{$contact->description}</p>
{/if}
</li>
{elseif="$mode == 'tag'"}
<li class="condensed">
<span class="icon gray">
<i class="zmdi zmdi-tag"></i>
</span>
<h2>
<a href="{$c->route('tag', array($tag))}">
#{$tag}
</a>
</h2>
</li>
{else}
<li class="condensed action">
<div class="action">
@ -132,6 +143,19 @@
</content>
</section>
<footer>
{$tags = $value->getTags()}
{if="isset($tags)"}
<ul class="thin">
<li>
<span class="icon zmdi zmdi-tag gray"></span>
<span>
{loop="$tags"}
<a href="{$c->route('tag', array($value))}">#{$value}</a>
{/loop}
</span>
</li>
</ul>
{/if}
<ul class="middle divided spaced">
{if="isset($attachements.links)"}
{loop="$attachements.links"}

13
app/widgets/Group/_group_posts.tpl

@ -56,6 +56,19 @@
</content>
</section>
<footer>
{$tags = $value->getTags()}
{if="isset($tags)"}
<ul class="thin">
<li>
<span class="icon zmdi zmdi-tag gray"></span>
<span>
{loop="$tags"}
<a target="_blank" href="{$c->route('tag', array($value))}">#{$value}</a>
{/loop}
</span>
</li>
</ul>
{/if}
<ul class="thin">
{if="isset($attachements.links)"}
{loop="$attachements.links"}

13
app/widgets/Post/_post.tpl

@ -75,6 +75,19 @@
</section>
<footer>
{$tags = $post->getTags()}
{if="isset($tags)"}
<ul class="thin">
<li>
<span class="icon zmdi zmdi-tag gray"></span>
<span>
{loop="$tags"}
<a target="_blank" href="{$c->route('tag', array($value))}">#{$value}</a>
{/loop}
</span>
</li>
</ul>
{/if}
<ul class="middle divided spaced">
{if="isset($attachements.links)"}
{loop="$attachements.links"}

13
app/widgets/Publish/Publish.php

@ -189,6 +189,19 @@ class Publish extends WidgetBase
}
}
if(Validator::string()->notEmpty()->alnum(',')->validate($form->tags->value)) {
$p->setTags(array_unique(
array_filter(
array_map(
function($value) {
return trim(strtolower($value));
},
explode(',', $form->tags->value)
)
)
));
}
if($form->embed->value != '' && filter_var($form->embed->value, FILTER_VALIDATE_URL)) {
try {
$embed = Embed\Embed::create($form->embed->value);

14
app/widgets/Publish/_publish_create.tpl

@ -48,4 +48,18 @@
</li>
{/if}
</ul>
<div>
{if="$item != false"}
{$tags = $item->getTagsImploded()}
{/if}
<input
type="text"
name="tags"
placeholder="write, comma separated, tags"
{if="isset($tags)"}
value="{$tags}"
{/if}>
<label for="title">{$c->__('post.tags')}</label>
</div>
</form>

1
bootstrap.php

@ -316,6 +316,7 @@ class Bootstrap {
Modl\Utils::loadModel('Message');
Modl\Utils::loadModel('Sessionx');
Modl\Utils::loadModel('Conference');
Modl\Utils::loadModel('Tag');
if(file_exists(DOCUMENT_ROOT.'/config/db.inc.php')) {
require DOCUMENT_ROOT.'/config/db.inc.php';

1
locales/locales.ini

@ -31,6 +31,7 @@ visio = Visio-conference
pods = Pods
share = Share
room = Room
tag = Tag
[error]
error = Error: %s

1
system/Route.php

@ -10,6 +10,7 @@ class Route extends \BaseController {
'accountnext' => array('s', 'err'),
'admin' => false,
'blog' => array('f', 'i'),
'tag' => array('t', 'i'),
'chat' => array('f'),
'conf' => false,
'contact' => array('f'),

Loading…
Cancel
Save