/** * favorite an ad * @param integer $id_user user * @param integer $id_ad ad * @return boolean */ public static function unfavorite($id_user, $id_ad) { //try to find the fav $fav = new Model_Favorite(); $fav->where('id_user', '=', $id_user)->where('id_ad', '=', $id_ad)->find(); if ($fav->loaded()) { $fav->delete(); return TRUE; } else { return FALSE; } }
public function post_add() { if (!$this->favorite) { $this->favorite = Model_Favorite::forge($this->input); try { $this->favorite->save(); } catch (\Exception $e) { throw $e; return $this->responseJson(false); } } return $this->responseJson(true); }
public function action_delete() { try { if (is_numeric($id_ad = $this->request->param('id'))) { if (Model_Favorite::unfavorite($this->user->id_user, $id_ad) === TRUE) { $this->rest_output(__('Deleted')); } else { $this->_error(__('Favorite not found'), 404); } } else { $this->_error(__('Favorite not found'), 404); } } catch (Kohana_HTTP_Exception $khe) { $this->_error($khe); } }
/** * Create favorite * * @param Model_User $user */ public function add_favorite(Model_User $user) { if ($this->loaded() && !$this->is_favorite($user)) { // Create favorite $favorite = Model_Favorite::factory(); $favorite->user_id = $user->id; $favorite->event_id = $this->id; $favorite->created = time(); if ($favorite->save()) { $this->favorite_count++; $this->save(); self::$_favorites[$this->id][(int) $user->id] = (int) $user->id; return true; } } return false; }
/** * unfavorite an ad * @param integer $id_user user * @param integer $id_ad ad * @return boolean */ public static function unfavorite($id_user, $id_ad) { //try to find the fav $fav = new Model_Favorite(); $fav->where('id_user', '=', $id_user)->where('id_ad', '=', $id_ad)->find(); if ($fav->loaded()) { $fav->delete(); // update ad favorite counter $ad = new Model_Ad($id_ad); if ($ad->loaded()) { $ad->favorited--; try { $ad->save(); } catch (Exception $e) { return FALSE; } } return TRUE; } else { return FALSE; } }
<?php } else { ?> <article class="list well clearfix"> <?php } ?> <div class="pull-right favorite" id="fav-<?php echo $ad->id_ad; ?> "> <?php if (Auth::instance()->logged_in()) { ?> <?php $fav = Model_Favorite::is_favorite($user, $ad); ?> <a data-id="fav-<?php echo $ad->id_ad; ?> " class="add-favorite <?php echo $fav ? 'remove-favorite' : ''; ?> " title="<?php echo __('Add to Favorites'); ?> " href="<?php echo Route::url('oc-panel', array('controller' => 'profile', 'action' => 'favorites', 'id' => $ad->id_ad)); ?> "> <i class="glyphicon glyphicon-heart<?php
public function action_favorites() { $user = Auth::instance()->get_user(); //favs or unfavs if (is_numeric($id_ad = $this->request->param('id'))) { $this->auto_render = FALSE; $this->template = View::factory('js'); $ad = new Model_Ad($id_ad); //ad exists if ($ad->loaded()) { //if fav exists we delete if (Model_Favorite::unfavorite($user->id_user, $id_ad) === TRUE) { //fav existed deleting $this->template->content = __('Deleted'); } else { //create the fav Model_Favorite::favorite($user->id_user, $id_ad); $this->template->content = __('Saved'); } } else { $this->template->content = __('Ad Not Found'); } } else { $this->template->title = __('My Favorites'); Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title)); Controller::$full_width = TRUE; $this->template->styles = array('//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.css' => 'screen'); $this->template->scripts['footer'][] = '//cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.min.js'; $this->template->scripts['footer'][] = 'js/oc-panel/favorite.js'; $favorites = new Model_Favorite(); $favorites = $favorites->where('id_user', '=', $user->id_user)->order_by('created', 'desc')->find_all(); $this->template->bind('content', $content); $this->template->content = View::factory('oc-panel/profile/favorites', array('favorites' => $favorites)); } }
echo __('This advertisement doesn´t exist, or is not yet published!'); ?> </h1> </div> <?else:?> <?php echo Form::errors(); ?> <div class="page-header"> <div class="pull-right favorite" id="fav-<?php echo $ad->id_ad; ?> "> <?if (Auth::instance()->logged_in()):?> <?$fav = Model_Favorite::is_favorite(Auth::instance()->get_user(),$ad);?> <a data-id="fav-<?php echo $ad->id_ad; ?> " class="add-favorite <?php echo $fav ? 'remove-favorite' : ''; ?> " title="<?php echo __('Add to Favorites'); ?> " href="<?php echo Route::url('oc-panel', array('controller' => 'profile', 'action' => 'favorites', 'id' => $ad->id_ad)); ?> "> <i class="glyphicon glyphicon-heart<?php echo $fav ? '' : '-empty';
/** * マイリスト(お気に入り)数を取得します * * @access public * @param * @return int * @author shimma */ public function getFavoriteCount() { return \Model_Favorite::getUserFavoriteCount($this->user_id); }