Exemple #1
0
/**
 * Associate a resource and an EAN-13 (ISBN-13) code. 
 *
 * These codes are treated differently from ordinary tags, first of
 * all because treating them as tags would expand the list of tags to
 * equal potentially the list of resources, also because the
 * relationships between EAN-13 and resources are fundamentally
 * different from those between resources and tags.
 *
 * Since we are allowing multiple EAN13 per resource (article about 2
 * or more books...), re-posting a 2nd ean13 to the same resource will
 * just add a new ean13 and will not affect previous data.
 *
 * We check the ean13 data and return a 406 if it is non-numeric or
 * too long.
 *
 * POST, res, ean13
 */
function assocEan13(folksoQuery $q, folksoDBconnect $dbc, folksoSession $fks)
{
    $r = new folksoResponse();
    $u = $fks->userSession(null, 'folkso', 'redac');
    if (!$u instanceof folksoUser || !$u->checkUserRight('folkso', 'redac')) {
        return $r->unAuthorized($u);
    }
    try {
        $i = new folksoDBinteract($dbc);
        /** check **/
        if (!ean13dataCheck($q->get_param('ean13'))) {
            $r->setError(406, "Bad EAN13 data", "The folksoean13 field should consist of exactly 13 digits. " . $q->get_param('ean13') . " is " . strlen($q->get_param('ean13')) . " long " . is_numeric($q->get_param('ean13')) ? " and it is numeric " : " but it is not numeric " . "\n\nPlease check your " . "data before trying again.");
            return $r;
        }
        if (is_numeric($q->res)) {
            $sql = "INSERT INTO ean13 SET resource_id = " . $q->res . ", ean13 = " . $q->get_param('ean13');
        } else {
            $sql = "INSERT INTO ean13 (ean13, resource_id) " . " VALUES (" . $q->get_param('ean13') . ", " . " (SELECT id FROM resource " . "WHERE uri_normal = url_whack('" . $i->dbescape($q->res) . "'))) ";
        }
        $i->query($sql);
    } catch (dbConnectionException $e) {
        $r->dbConnectionException($e->getMessage());
        return $r;
    } catch (dbQueryException $e) {
        if ($e->sqlcode == 1048 || $e->sqlcode == 1452) {
            $r->setError(404, "Resource not found", "The resource you tried to associate with a EAN13 was not" . " found in the database. \n\nPerhaps it has not yet been indexed," . "  or your reference is incorrect.");
        } elseif ($e->sqlcode == 1062) {
            $r->setError(409, 'Duplicate EAN13', "This resource/EAN13 combination is already present in the database.\n\n" . "Duplicate EAN13 entries for the same resource are not allowed. This is " . "slightly different from how tags work.");
        } else {
            $r->dbQueryError($i->error_info());
        }
        return $r;
    }
    $r->setOk(200, 'Added');
    $r->t("The EAN13 information was added to a resource.");
    return $r;
}