Exemple #1
0
 public static function process($object, $strip = TRUE)
 {
     $object->documents = Snippet::documents($object->id);
     $object->votes = Snippet::votes($object->id);
     $object->syntax = Snippet::get_primary_syntax($object);
     return parent::process($object, $strip);
 }
Exemple #2
0
Route::get('user/info/(:any)/snippets', array('before' => 'cache', 'after' => 'cache', function ($username) {
    if ($username == 'me') {
        if (!($user = Auth::user())) {
            return array('error' => 'You must be logged in to view your own snippets');
        }
    } else {
        $user = User::get($username);
    }
    $count = (int) Input::get('count', 10);
    $page = (int) Input::get('page', 0);
    $order = Input::get('order', 'created');
    $order_fields = array('votes_up', 'votes_down', 'votes_total', 'created', 'updated');
    if (!in_array($order, $order_fields)) {
        return array('error' => 'You can only order snippets by the following fields: ' . implode(', ', $order_fields));
    }
    if (substr($order, 0, 5) != 'votes') {
        $order = 'snippets.' . $order;
    }
    $snippets = DB::query("SELECT snippets.*,\n\t\t\t(SELECT COUNT(*) FROM votes WHERE snippet_id = snippets.id AND direction > 0) as votes_up,\n\t\t\t(SELECT COUNT(*) FROM votes WHERE snippet_id = snippets.id AND direction < 0) as votes_down,\n\t\t\t(SELECT SUM(direction) FROM votes WHERE snippet_id = snippets.id) as votes_total\n\t\tFROM snippets\n\t\tWHERE snippets.user_id = " . (int) $user->id . "\n\t\tORDER BY {$order} DESC\n\t\tLIMIT " . $page * $count . ", " . $count);
    foreach ($snippets as $i => $snippet) {
        foreach ($snippet as $k => $v) {
            if (is_numeric($v)) {
                $snippets[$i]->{$k} = 1 * $v;
            }
        }
        $snippets[$i]->votes_total = (int) $snippet->votes_total;
        $snippets[$i]->documents = Snippet::documents($snippet->id, 100);
        $snippets[$i]->syntax = Snippet::get_primary_syntax($snippets[$i]);
    }
    return $snippets;
}));