making tags work

This commit is contained in:
friendica 2012-07-09 22:08:25 -07:00
parent 8855cb9c28
commit 4b066e04f6
9 changed files with 163 additions and 76 deletions

View File

@ -77,21 +77,18 @@ function networks_widget($baseurl,$selected = '') {
function fileas_widget($baseurl,$selected = '') { function fileas_widget($baseurl,$selected = '') {
$a = get_app(); $a = get_app();
if(! local_user()) if(! local_user())
return ''; return '';
$saved = get_pconfig(local_user(),'system','filetags');
if(! strlen($saved))
return;
$matches = false;
$terms = array(); $terms = array();
$cnt = preg_match_all('/\[(.*?)\]/',$saved,$matches,PREG_SET_ORDER); $r = q("select distinct(term) from term where uid = %d and type = %d order by term asc",
if($cnt) { intval(local_user()),
foreach($matches as $mtch) { intval(TERM_FILE)
$unescaped = xmlify(file_tag_decode($mtch[1])); );
$terms[] = array('name' => $unescaped,'selected' => (($selected == $unescaped) ? 'selected' : '')); if(count($r)) {
} foreach($r as $rr)
$terms[] = array('name' => $rr['term'], 'selected' => (($selected == $rr['term']) ? 'selected' : ''));
} }
return replace_macros(get_markup_template('fileas_widget.tpl'),array( return replace_macros(get_markup_template('fileas_widget.tpl'),array(
@ -108,18 +105,15 @@ function fileas_widget($baseurl,$selected = '') {
function categories_widget($baseurl,$selected = '') { function categories_widget($baseurl,$selected = '') {
$a = get_app(); $a = get_app();
$saved = get_pconfig($a->profile['profile_uid'],'system','filetags');
if(! strlen($saved))
return;
$matches = false;
$terms = array(); $terms = array();
$cnt = preg_match_all('/<(.*?)>/',$saved,$matches,PREG_SET_ORDER); $r = q("select distinct(term) from term where uid = %d and type = %d order by term asc",
if($cnt) { intval($a->profile['profile_uid']),
foreach($matches as $mtch) { intval(TERM_CATEGORY)
$unescaped = xmlify(file_tag_decode($mtch[1])); );
$terms[] = array('name' => $unescaped,'selected' => (($selected == $unescaped) ? 'selected' : '')); if(count($r)) {
} foreach($r as $rr)
$terms[] = array('name' => $rr['term'], 'selected' => (($selected == $rr['term']) ? 'selected' : ''));
} }
return replace_macros(get_markup_template('categories_widget.tpl'),array( return replace_macros(get_markup_template('categories_widget.tpl'),array(

View File

@ -611,13 +611,12 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
$shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false); $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false);
if($page_writeable) { if($page_writeable) {
/* if($toplevelpost) { */ $likebuttons = array(
$likebuttons = array( 'like' => array( t("I like this \x28toggle\x29"), t("like")),
'like' => array( t("I like this \x28toggle\x29"), t("like")), 'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")), );
); if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
if ($shareable) $likebuttons['share'] = array( t('Share this'), t('share'));
/* } */
$qc = $qcomment = null; $qc = $qcomment = null;
@ -739,10 +738,11 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
$tags=array(); $tags=array();
foreach(explode(',',$item['tag']) as $tag){ $terms = get_terms_oftype($item['term'],array(TERM_HASHTAG,TERM_MENTION,TERM_UNKNOWN));
$tag = trim($tag); if(count($terms))
if ($tag!="") $tags[] = bbcode($tag); foreach($terms as $tag)
} $tags[] = format_term_for_display($tag);
// Build the HTML // Build the HTML
@ -1102,13 +1102,26 @@ function status_editor($a,$x, $notes_cid = 0, $popup=false) {
} }
function conv_sort($arr,$order) { function conv_sort($arr,$tags,$order) {
if((!(is_array($arr) && count($arr)))) if((!(is_array($arr) && count($arr))))
return array(); return array();
$parents = array(); $parents = array();
for($x = 0; $x < count($arr); $x ++) {
if(count($tags)) {
foreach($tags as $t) {
if($t['oid'] == $arr[$x]['item_id']) {
if(! is_array($arr[$x]['term']))
$arr[$x]['term'] = array();
$arr[$x]['term'][] = $t;
}
}
}
}
foreach($arr as $x) foreach($arr as $x)
if($x['id'] == $x['parent']) if($x['id'] == $x['parent'])
$parents[] = $x; $parents[] = $x;

View File

@ -156,7 +156,7 @@ function nav(&$a) {
$banner = get_config('system','banner'); $banner = get_config('system','banner');
if($banner === false) if($banner === false)
$banner .= '<a href="http://friendica.com"><img id="logo-img" src="images/friendica-32.png" alt="logo" /></a><span id="logo-text"><a href="http://friendica.com">Friendica</a></span>'; $banner .= '<a href="http://friendica.com"><img id="logo-img" src="images/friendica-32.png" alt="logo" /></a><span id="logo-text"><a href="http://friendica.com"><span class="heart">red</span></a></span>';
$tpl = get_markup_template('nav.tpl'); $tpl = get_markup_template('nav.tpl');

View File

@ -881,20 +881,7 @@ function prepare_body($item,$attach = false) {
$a = get_app(); $a = get_app();
call_hooks('prepare_body_init', $item); call_hooks('prepare_body_init', $item);
$cache = get_config('system','itemcache'); $s = prepare_text($item['body']);
if (($cache != '')) {
$cachefile = $cache."/".$item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']);
if (file_exists($cachefile))
$s = file_get_contents($cachefile);
else {
$s = prepare_text($item['body']);
file_put_contents($cachefile, $s);
}
} else
$s = prepare_text($item['body']);
$prep_arr = array('item' => $item, 'html' => $s); $prep_arr = array('item' => $item, 'html' => $s);
call_hooks('prepare_body', $prep_arr); call_hooks('prepare_body', $prep_arr);
@ -938,30 +925,29 @@ function prepare_body($item,$attach = false) {
} }
$s .= '<div class="clear"></div></div>'; $s .= '<div class="clear"></div></div>';
} }
$matches = false;
$cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER); $x = '';
if($cnt) { $terms = get_terms_oftype($item['term'],TERM_CATEGORY);
// logger('prepare_text: categories: ' . print_r($matches,true), LOGGER_DEBUG); if($terms) {
foreach($matches as $mtch) { foreach($terms as $t) {
if(strlen($x)) if(strlen($x))
$x .= ','; $x .= ',';
$x .= xmlify(file_tag_decode($mtch[1])) $x .= $t['term']
. ((local_user() == $item['uid']) ? ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])) . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>' : ''); . ((local_user() == $item['uid']) ? ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&cat=' . $t['term'] . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>' : '');
} }
if(strlen($x)) if(strlen($x))
$s .= '<div class="categorytags"><span>' . t('Categories:') . ' </span>' . $x . '</div>'; $s .= '<div class="categorytags"><span>' . t('Categories:') . ' </span>' . $x . '</div>';
} }
$matches = false;
$x = ''; $x = '';
$cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER); $terms = get_terms_oftype($item['term'],TERM_FILE);
if($cnt) { if($terms) {
// logger('prepare_text: filed_under: ' . print_r($matches,true), LOGGER_DEBUG); foreach($terms as $t) {
foreach($matches as $mtch) {
if(strlen($x)) if(strlen($x))
$x .= '&nbsp;&nbsp;&nbsp;'; $x .= '&nbsp;&nbsp;&nbsp;';
$x .= xmlify(file_tag_decode($mtch[1])) . ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&term=' . xmlify(file_tag_decode($mtch[1])) . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>'; $x .= $t['term'] . ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&term=' . $t['term'] . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>';
} }
if(strlen($x) && (local_user() == $item['uid'])) if(strlen($x) && (local_user() == $item['uid']))
$s .= '<div class="filesavetags"><span>' . t('Filed under:') . ' </span>' . $x . '</div>'; $s .= '<div class="filesavetags"><span>' . t('Filed under:') . ' </span>' . $x . '</div>';
@ -1460,10 +1446,67 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
return true; return true;
} }
function store_item_tag($uid,$iid,$otype,$type,$term,$url = '') {
if(! $term)
return false;
$r = q("select * from term
where uid = %d and oid = %d and otype = %d and type = %d
and term = '%s' and url = '%s' ",
intval($uid),
intval($iid),
intval($otype),
intval($type),
dbesc($term),
dbesc($url)
);
if(count($r))
return false;
$r = q("insert into term (uid, oid, otype, type, term, url)
values( %d, %d, %d, %d, '%s', '%s') ",
intval($uid),
intval($iid),
intval($otype),
intval($type),
dbesc($term),
dbesc($url)
);
return $r;
}
function get_terms_oftype($arr,$type) {
$ret = array();
if(! (is_array($arr) && count($arr)))
return $ret;
if(! is_array($type))
$type = array($type);
foreach($type as $t)
foreach($arr as $x)
if($x['type'] == $t)
$ret[] = $x;
return $ret;
}
function format_term_for_display($term) {
$s = '';
if($term['type'] == TERM_HASHTAG)
$s .= '#';
elseif($term['type'] == TERM_MENTION)
$s .= '@';
if($term['url']) $s .= '<a target="extlink" href="' . $term['url'] . '">' . $term['term'] . '</a>';
else $s .= $term['term'];
return $s;
}
function file_tag_save_file($uid,$item,$file) { function file_tag_save_file($uid,$item,$file) {
$result = false; $result = false;
if(! intval($uid)) if(! intval($uid))
return false; return false;
$r = q("select file from item where id = %d and uid = %d limit 1", $r = q("select file from item where id = %d and uid = %d limit 1",
intval($item), intval($item),
intval($uid) intval($uid)

View File

@ -18,12 +18,18 @@ function filer_content(&$a) {
if($item_id && strlen($term)){ if($item_id && strlen($term)){
// file item // file item
file_tag_save_file(local_user(),$item_id,$term); store_item_tag(local_user(),$item_id,TERM_OBJ_POST,TERM_FILE,$term,'');
} else { }
// return filer dialog else {
$filetags = get_pconfig(local_user(),'system','filetags'); $filetags = array();
$filetags = file_tag_file_to_list($filetags,'file'); $r = q("select distinct(term) from term where uid = %d and type = %d order by term asc",
$filetags = explode(",", $filetags); intval(local_user()),
intval(TERM_FILE)
);
if(count($r)) {
foreach($r as $rr)
$filetags[] = $rr['term'];
}
$tpl = get_markup_template("filer_dialog.tpl"); $tpl = get_markup_template("filer_dialog.tpl");
$o = replace_macros($tpl, array( $o = replace_macros($tpl, array(
'$field' => array('term', t("Save to Folder:"), '', '', $filetags, t('- select -')), '$field' => array('term', t("Save to Folder:"), '', '', $filetags, t('- select -')),

View File

@ -6,8 +6,8 @@ function filerm_content(&$a) {
killme(); killme();
} }
$term = unxmlify(trim($_GET['term'])); $term = trim($_GET['term']);
$cat = unxmlify(trim($_GET['cat'])); $cat = trim($_GET['cat']);
$category = (($cat) ? true : false); $category = (($cat) ? true : false);
if($category) if($category)
@ -17,8 +17,14 @@ function filerm_content(&$a) {
logger('filerm: tag ' . $term . ' item ' . $item_id); logger('filerm: tag ' . $term . ' item ' . $item_id);
if($item_id && strlen($term)) if($item_id && strlen($term)) {
file_tag_unsave_file(local_user(),$item_id,$term, $category); $r = q("delete from term where uid = %d and type = %d and oid = %d and $term = '%s' limit 1",
intval(local_user()),
intval(($category) ? FILE_CATEGORY : FILE_HASHTAG),
intval($item_id),
dbesc($term)
);
}
if(x($_SESSION,'return_url')) if(x($_SESSION,'return_url'))
goaway($a->get_baseurl() . '/' . $_SESSION['return_url']); goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);

View File

@ -643,7 +643,20 @@ function network_content(&$a, $update = 0) {
dbesc($parents_str) dbesc($parents_str)
); );
$items = conv_sort($items,$ordering); $tag_finder = array();
if(count($items))
foreach($items as $item)
if(! in_array($item['item_id'],$tag_finder))
$tag_finder[] = $item['item_id'];
$tag_finder_str = implode(', ', $tag_finder);
$tags = q("select * from term where oid in ( %s ) and otype = %d",
dbesc($tag_finder_str),
intval(TERM_OBJ_POST)
);
$items = conv_sort($items,$tags,$ordering);
} else { } else {
$items = array(); $items = array();

View File

@ -280,7 +280,19 @@ function profile_content(&$a, $update = 0) {
dbesc($parents_str) dbesc($parents_str)
); );
$items = conv_sort($items,'created'); $tag_finder = array();
if(count($items))
foreach($items as $item)
if(! in_array($item['item_id'],$tag_finder))
$tag_finder[] = $item['item_id'];
$tag_finder_str = implode(', ', $tag_finder);
$tags = q("select * from term where oid in ( '%s' ) and otype = %d",
dbesc($tag_finder),
intval(TERM_OBJ_POST)
);
$items = conv_sort($items,$tags,'created');
} else { } else {
$items = array(); $items = array();
} }

View File

@ -11,8 +11,8 @@ function tagrm_post(&$a) {
if((x($_POST,'submit')) && ($_POST['submit'] === t('Cancel'))) if((x($_POST,'submit')) && ($_POST['submit'] === t('Cancel')))
goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']); goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
$tag = ((x($_POST,'tag')) ? hex2bin(notags(trim($_POST['tag']))) : ''); $tag = ((x($_POST,'tag')) ? trim($_POST['tag']) : '');
$item = ((x($_POST,'item')) ? intval($_POST['item']) : 0 ); $item = ((x($_POST,'item')) ? intval($_POST['item']) : 0 );
$r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($item), intval($item),