예제 #1
0
파일: tag.php 프로젝트: josf/folkso
/**
 * Retrieves a list of the resources associated with the given tag.
 *
 * Parameters: GET, resources (single param)
 *
 * We might have to think about adding a way to announce whether there
 * is a "next page" or not.
 * 
 * Currently supports xhtml and xml dataformats.
 */
function getTagResources(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks)
{
    $r = new folksoResponse();
    try {
        $i = new folksoDBinteract($dbc);
        // check to see if tag exists -- can this be done with the main query instead?
        if (!$i->tagp($q->tag)) {
            $r->setError(404, 'Tag not found', $q->tag . " does not exist in the database");
            return $r;
        }
        $querybase = "SELECT\n                 r.uri_raw AS href, r.id AS id, r.title AS title, \n                 CASE \n                   WHEN title IS NULL THEN uri_normal \n                   ELSE title \n                 END AS display\n              FROM resource r\n                 JOIN tagevent ON r.id = tagevent.resource_id\n                 JOIN tag ON tagevent.tag_id = tag.id ";
        // tag by ID
        if (is_numeric($q->tag)) {
            $querybase .= "WHERE tag.id = " . $i->dbescape($q->tag);
        } else {
            $querybase .= "WHERE tag.tagnorm = normalize_tag('" . $i->dbescape($q->tag) . "')";
        }
        $querybase .= " ORDER BY r.visited DESC ";
        //pagination
        if (!$q->is_param('page') || $q->get_param('page') == 1) {
            $querybase .= "  LIMIT 20 ";
        } else {
            $querybase .= "  LIMIT " . $q->get_param('page') * 20 . ",20 ";
        }
        $i->query($querybase);
    } catch (dbException $e) {
        return $r->handleDBexception($e);
    }
    switch ($i->result_status) {
        case 'NOROWS':
            $r->setOk(204, 'No resources associated with  tag');
            $r->t("No resources are currently associated with " . $q->tag);
            return $r;
            break;
        case 'OK':
            $r->setOk(200, 'Tag found');
            break;
            // now we do the rest, assuming all is well.
    }
    $df = new folksoDisplayFactory();
    $dd = $df->ResourceList();
    if ($q->content_type() == 'text/html') {
        $dd->activate_style('xhtml');
    } else {
        $dd->activate_style('xml');
    }
    $r->t($dd->startform());
    while ($row = $i->result->fetch_object()) {
        $r->t($dd->line($row->id, $row->href, array('xml' => $row->display, 'default' => $row->display)));
    }
    $r->t($dd->endform());
    return $r;
}
예제 #2
0
파일: user.php 프로젝트: josf/folkso
/**
 *
 * 
 */
function getUserResByTag(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks)
{
    $r = new folksoResponse();
    try {
        $u = $fks->userSession(null);
        if (!$u instanceof folksoUser && !$q->is_param('user')) {
            return $r->setError(404, 'No user');
        } elseif ($q->is_param('user')) {
            $u = new folksoUser($dbc);
            // we create a user object anyway
            $u->setUid($q->get_param('user'));
            if (!$u->exists($q->get_param('user'))) {
                return $r->setError(404, 'Missing or invalid user');
            }
        }
        $i = new folksoDBinteract($dbc);
        $uq = new folksoUserQuery();
        $sql = $uq->resourcesByTag($q->tag, $u->userid);
        $i->query($sql);
        /* these are inside the try block because exists() hits the DB */
        if ($i->rowCount == 0) {
            if (isset($u->nick) || $u->exists()) {
                return $r->setOk(204, 'User has no resources with this tag');
            } else {
                // no longer necessary
                return $r->setError(404, 'Unknown user');
            }
        }
    } catch (dbException $e) {
        return $r->handleDBexception($e);
    } catch (badUseridException $e) {
        return $r->handleDBexception($e);
        // TODO: update this with new class
    }
    $r->setOk(200, 'Found');
    $df = new folksoDisplayFactory();
    if ($q->content_type() == 'json') {
        $dd = new folksoDataJson('resid', 'url', 'title');
    } else {
        $dd = $df->ResourceList('xml');
    }
    $r->t($dd->startform());
    while ($row = $i->result->fetch_object()) {
        $r->t($dd->line($row->id, htmlspecialchars($row->uri_raw), htmlspecialchars($row->title)));
    }
    $r->t($dd->endform());
    return $r;
}