Esempio n. 1
0
 public function getData(BASE_CLASS_WidgetParameter $params)
 {
     $count = (int) $params->customParamList['count'];
     $userId = OW::getUser()->getId();
     $service = OCSFAVORITES_BOL_Service::getInstance();
     $lang = OW::getLanguage();
     $router = OW::getRouter();
     $multiple = OW::getConfig()->getValue('ocsfavorites', 'can_view') && OW::getUser()->isAuthorized('ocsfavorites', 'view_users');
     $toolbar = array();
     $lists = array();
     $resultList = array();
     $toolbar['my'] = array('label' => $lang->text('base', 'view_all'), 'href' => $router->urlForRoute('ocsfavorites.list'));
     $lists['my'] = $service->findFavoritesForUser($userId, 1, $count);
     if ($multiple) {
         $toolbar['me'] = array('label' => $lang->text('base', 'view_all'), 'href' => $router->urlForRoute('ocsfavorites.added_list'));
         $lists['me'] = $service->findUsersWhoAddedUserAsFavorite($userId, 1, $count);
         $toolbar['mutual'] = array('label' => $lang->text('base', 'view_all'), 'href' => $router->urlForRoute('ocsfavorites.mutual_list'));
         $lists['mutual'] = $service->findMutualFavorites($userId, 1, $count);
     }
     $this->setSettingValue(self::SETTING_TOOLBAR, array($toolbar['my']));
     $resultList['my'] = array('menu-label' => $lang->text('ocsfavorites', 'my'), 'menu_active' => true, 'userIds' => $this->getIds($lists['my'], 'favoriteId'), 'toolbar' => array($toolbar['my']));
     if ($multiple) {
         if ($lists['me']) {
             $resultList['me'] = array('menu-label' => $lang->text('ocsfavorites', 'who_added_me'), 'userIds' => $this->getIds($lists['me'], 'userId'), 'toolbar' => array($toolbar['me']));
         }
         if ($lists['mutual']) {
             $resultList['mutual'] = array('menu-label' => $lang->text('ocsfavorites', 'mutual'), 'userIds' => $this->getIds($lists['mutual'], 'userId'), 'toolbar' => array($toolbar['mutual']));
         }
     }
     return $resultList;
 }
Esempio n. 2
0
 public function remove()
 {
     if (!OW::getRequest()->isAjax()) {
         exit(json_encode(array()));
     }
     $lang = OW::getLanguage();
     if (!OW::getUser()->isAuthenticated()) {
         exit(json_encode(array('result' => false, 'error' => $lang->text('ocsfavorites', 'signin_required'))));
     }
     if (!OW::getUser()->isAuthorized('ocsfavorites', 'add_to_favorites')) {
         exit(json_encode(array()));
     }
     if (empty($_POST['favoriteId'])) {
         exit(json_encode(array()));
     }
     $service = OCSFAVORITES_BOL_Service::getInstance();
     $userId = OW::getUser()->getId();
     $favoriteId = (int) $_POST['favoriteId'];
     $user = BOL_UserService::getInstance()->findUserById($favoriteId);
     if (!$user) {
         exit(json_encode(array()));
     }
     $favorite = $service->isFavorite($userId, $favoriteId);
     if (!$favorite) {
         exit(json_encode(array()));
     }
     $service->deleteFavorite($userId, $favoriteId);
     exit(json_encode(array('result' => true, 'msg' => $lang->text('ocsfavorites', 'favorite_removed'))));
 }
Esempio n. 3
0
 public function action()
 {
     if (!OW::getRequest()->isAjax()) {
         exit(json_encode(array()));
     }
     $lang = OW::getLanguage();
     if (!OW::getUser()->isAuthenticated()) {
         exit(json_encode(array('result' => false, 'error' => $lang->text('ocsfavorites', 'signin_required'))));
     }
     if (empty($_POST['favoriteId'])) {
         exit(json_encode(array()));
     }
     $command = !empty($_POST['command']) && in_array($_POST['command'], array("remove-favorite", "add-favorite")) ? $_POST['command'] : "add-favorite";
     $service = OCSFAVORITES_BOL_Service::getInstance();
     $userId = OW::getUser()->getId();
     $favoriteId = (int) $_POST['favoriteId'];
     $favorite = $service->isFavorite($userId, $favoriteId);
     if (!$favorite && !OW::getUser()->isAuthorized('ocsfavorites', 'add_to_favorites')) {
         exit(json_encode(array('result' => false)));
     }
     $user = BOL_UserService::getInstance()->findUserById($favoriteId);
     if (!$user) {
         exit(json_encode(array('result' => false)));
     }
     if ($favorite && $command == "add-favorite" || !$favorite && $command == "remove-favorite") {
         exit(json_encode(array('result' => false)));
     }
     switch ($command) {
         case "add-favorite":
             $service->addFavorite($userId, $favoriteId);
             // track credits
             $eventParams = array('pluginKey' => 'ocsfavorites', 'action' => 'add_to_favorites');
             if (OW::getEventManager()->call('usercredits.check_balance', $eventParams) === true) {
                 OW::getEventManager()->call('usercredits.track_action', $eventParams);
             }
             exit(json_encode(array('result' => true, 'msg' => $lang->text('ocsfavorites', 'favorite_added'))));
         case "remove-favorite":
             $service->deleteFavorite($userId, $favoriteId);
             exit(json_encode(array('result' => true, 'msg' => $lang->text('ocsfavorites', 'favorite_removed'))));
     }
 }
Esempio n. 4
0
 public function onActionToolbarAddFavoriteActionTool(BASE_CLASS_EventCollector $event)
 {
     if (!OW::getUser()->isAuthenticated()) {
         return;
     }
     $params = $event->getParams();
     if (empty($params['userId'])) {
         return;
     }
     $userId = (int) $params['userId'];
     if (OW::getUser()->getId() == $userId) {
         return;
     }
     if (!OW::getUser()->isAuthorized('ocsfavorites', 'add_to_favorites')) {
         return;
     }
     $action = array("group" => 'addition', "order" => 3);
     $eventParams = array('pluginKey' => 'ocsfavorites', 'action' => 'add_to_favorites');
     $credits = OW::getEventManager()->call('usercredits.check_balance', $eventParams);
     $service = OCSFAVORITES_BOL_Service::getInstance();
     $lang = OW::getLanguage();
     $uniqId = uniqid("ocsfav-");
     $isFavorite = $service->isFavorite(OW::getUser()->getId(), $userId);
     $action["label"] = $isFavorite ? $lang->text('ocsfavorites', 'remove_favorite_button') : $lang->text('ocsfavorites', 'add_favorite_button');
     $toggleText = !$isFavorite ? $lang->text('ocsfavorites', 'remove_favorite_button') : $lang->text('ocsfavorites', 'add_favorite_button');
     $action["href"] = 'javascript://';
     $action["id"] = $uniqId;
     if (!$isFavorite && $credits === false) {
         $error = OW::getEventManager()->call('usercredits.error_message', $eventParams);
         $js = '$("#' . $uniqId . '").click(function(){
             OWM.error(' . json_encode($error) . ');
         });
         ';
     } else {
         $action["attributes"] = array();
         $action["attributes"]["data-command"] = $isFavorite ? "remove-favorite" : "add-favorite";
         $toggleCommand = !$isFavorite ? "remove-favorite" : "add-favorite";
         $js = UTIL_JsGenerator::newInstance();
         $js->jQueryEvent("#" . $uniqId, "click", 'var self = this;
             $.ajax({
                 url: e.data.url,
                 type: "POST",
                 data: { favoriteId: e.data.userId, command: $(self).attr("data-command") },
                 dataType: "json",
                 success: function(data) {
                     if ( data.result == true ) {
                         OW.info(data.msg);
                     }
                     else if ( data.error != undefined ) {
                         OW.warning(data.error);
                     }
                     OWM.Utils.toggleText($(".owm_context_action_list_item_c", self), e.data.toggleText);
                     OWM.Utils.toggleAttr(self, "data-command", e.data.toggleCommand);
                 }
             });
             ', array("e"), array("url" => OW::getRouter()->urlForRoute("ocsfavorites.action"), "userId" => $userId, "toggleText" => $toggleText, "toggleCommand" => $toggleCommand));
     }
     OW::getDocument()->addOnloadScript($js);
     $action["key"] = "ocsfavorites.add_to_favorites";
     $event->add($action);
 }
Esempio n. 5
0
 /**
  * @param OW_Event $event
  */
 public function onQuery(OW_Event $event)
 {
     if (!OW::getUser()->isAuthenticated() || !OW::getUser()->isAuthorized('ocsfavorites', 'add_to_favorites')) {
         return;
     }
     $params = $event->getParams();
     if (!in_array($params["command"], array("favorites.add", "favorites.remove"))) {
         return;
     }
     $userId = OW::getUser()->getId();
     $favoriteId = $params["params"]['userId'];
     $service = OCSFAVORITES_BOL_Service::getInstance();
     $info = null;
     $error = null;
     switch ($params["command"]) {
         case "favorites.remove":
             $service->deleteFavorite($userId, $favoriteId);
             $info = OW::getLanguage()->text('ocsfavorites', 'favorite_removed');
             break;
         case "favorites.add":
             $service->addFavorite($userId, $favoriteId);
             $info = OW::getLanguage()->text('ocsfavorites', 'favorite_added');
             break;
     }
     $event->setData(array("info" => $info, "error" => $error));
 }
Esempio n. 6
0
 /**
  * @param OW_Event $e
  */
 public function onAddFavorite(OW_Event $e)
 {
     $params = $e->getParams();
     $userId = (int) $params['userId'];
     $favoriteId = (int) $params['favoriteId'];
     $id = (int) $params['id'];
     if (OW::getConfig()->getValue('ocsfavorites', 'can_view') && OW::getAuthorization()->isUserAuthorized($favoriteId, 'ocsfavorites', 'view_users')) {
         $params = array('pluginKey' => 'ocsfavorites', 'entityType' => 'ocsfavorites_add_favorite', 'entityId' => $id, 'action' => 'ocsfavorites-add_favorite', 'userId' => $favoriteId, 'time' => time());
         $mutual = OCSFAVORITES_BOL_Service::getInstance()->isFavorite($favoriteId, $userId);
         $avatar = BOL_AvatarService::getInstance()->getDataForUserAvatars(array($userId));
         $data = array('avatar' => $avatar[$userId], 'string' => array('key' => 'ocsfavorites+email_notification_post' . ($mutual ? '_mutual' : ''), 'vars' => array('userName' => $avatar[$userId]['title'], 'userUrl' => $avatar[$userId]['url'])), 'url' => $avatar[$userId]['url']);
         $event = new OW_Event('notifications.add', $params, $data);
         OW::getEventManager()->trigger($event);
     }
 }
Esempio n. 7
0
 public function getData($listType, $excludeList = array(), $showOnline = true, $count)
 {
     $list = array();
     $start = count($excludeList);
     $service = OCSFAVORITES_BOL_Service::getInstance();
     while ($count > count($list)) {
         $tmpList = $service->getDataForUsersList($listType, OW::getUser()->getId(), $start, $count);
         $itemList = $tmpList[0];
         $itemCount = $tmpList[1];
         if (empty($itemList)) {
             break;
         }
         foreach ($itemList as $key => $item) {
             if (count($list) == $count) {
                 break;
             }
             if (!in_array($item->id, $excludeList)) {
                 $list[] = $item->id;
             }
         }
         $start += $count;
         if ($start >= $itemCount) {
             break;
         }
     }
     return $list;
 }