/** * Add a LINK between TAGS and a URL */ function _AddLink($f_szUrl, $f_szTitle, $f_szTags) { global $db; $szUrl = trim($f_szUrl); $arrTags = unaliasTags(explode(' ', str_replace('/', ' ', valid_tags($f_szTags)))); $szTitle = trim($f_szTitle); if (!$arrTags || !$szUrl) { return 'ERROR:' . __LINE__; } // Add url $iUrlId = AddUrl($szUrl, $szTitle); foreach ($arrTags as $szTag) { // Add tag $iTagId = AddTag($szTag); // Insert relation try { $db->insert('l_links', array('url_id' => $iUrlId, 'tag_id' => $iTagId, 'utc_added' => time())); } catch (db_exception $ex) { } } return 'OK'; }
/** * Main method: view URLS by TAGS */ function ViewUrlsByTag($f_szTags = '') { global $db; $g_szTag = $f_szTags; $script = $_SERVER['PHP_SELF']; $szBasePath = rtrim(dirname($script), '/') . '/'; $mobile = isset($_GET['mobile']) || isset($_SERVER['HTTP_USER_AGENT']) && is_int(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobi')); // $arrInTags = explode(' ', $g_szTag); // $szWhereClause = 1 == count($arrInTags) ? "(l_tags.tag = '".$arrInTags[0]."')" : "(l_tags.tag = '".implode("' OR l_tags.tag = '", $arrInTags)."')"; if ('~new' == $g_szTag) { $szQuery = 'SELECT * FROM l_urls ORDER BY id DESC LIMIT 250;'; } else { $tags = unaliasTags(preg_split('#[\\/\\s]+#', $g_szTag)); $and = !strstr($g_szTag, "/"); $szQuery = $db->replaceholders(' SELECT u.*, COUNT(1) as matching FROM l_links l, l_tags t, l_urls u WHERE l.url_id = u.id AND l.tag_id = t.id AND t.tag in (?) GROUP BY u.id ', array($tags)); if ($and) { $szQuery .= $db->replaceholders(' HAVING matching = ?', array(count($tags))); } $szQuery .= ' ORDER BY u.id DESC'; } $arrUrls = $db->fetch($szQuery)->all(); require 'tpl.index.php'; }