Example #1
0
 public function transferCredits($userId, $receiveUser, $creditValue)
 {
     $debitValue = $creditValue * -1;
     $userCreditsService = USERCREDITS_BOL_CreditsService::getInstance();
     $creditsService = CREDITS_BOL_Service::getInstance();
     $sendItem = $this->logAction($creditsService->getSentActionId(), $userId, $debitValue);
     $receiveItem = $this->logAction($creditsService->getReceiveActionId(), $receiveUser, $creditValue);
     $userCreditsService->increaseBalance($receiveUser, $creditValue);
     $userCreditsService->decreaseBalance($userId, $creditValue);
     $sqlInsert = "INSERT INTO " . OW_DB_PREFIX . "credits_sent_log(senderItem, receiverItem, sender, receiver) \n                             VALUES(:sendItem, :receiveItem, :userId, :receiveUser)";
     $qParams = array('sendItem' => $sendItem, 'receiveItem' => $receiveItem, 'userId' => $userId, 'receiveUser' => $receiveUser);
     OW::getDbo()->query($sqlInsert, $qParams);
     $service = BOL_UserService::getInstance();
     $avatars = BOL_AvatarService::getInstance()->getDataForUserAvatars(array($userId, $receiveUser));
     $names = $service->getDisplayNamesForList(array($userId, $receiveUser));
     $uUrls = $service->getUserUrlsForList(array($userId, $receiveUser));
     if (OW::getConfig()->getValue('credits', 'enableNotification') == '1') {
         //Send notification to receiver
         $avatar = $avatars[$userId];
         $notificationParams = array('pluginKey' => 'credits', 'action' => 'credits-received', 'entityType' => 'received', 'entityId' => $receiveItem, 'userId' => $receiveUser, 'time' => time());
         $sender = '<a href="' . $uUrls[$userId] . '" target="_blank" >' . $names[$userId] . '</a>';
         $notificationData = array('string' => array('key' => 'credits+notify_credits_received', 'vars' => array('sender' => $sender, 'credits' => $creditValue)), 'avatar' => $avatar, 'url' => $uUrls[$userId]);
         $event = new OW_Event('notifications.add', $notificationParams, $notificationData);
         OW::getEventManager()->trigger($event);
     }
     $subject = OW::getLanguage()->text('credits', 'credits_email_subject', array('requester_name' => $names[$userId], 'credits' => $creditValue));
     $content = OW::getLanguage()->text('credits', 'credits_email_content', array('requester_name' => $names[$userId], 'requester_url' => $uUrls[$userId], 'credits' => $creditValue, 'user_url' => $uUrls[$receiveUser], 'name' => $names[$receiveUser]));
     if (OW::getConfig()->getValue('credits', 'enableEmail') == '1') {
         $tmpUser = $service->findUserById($receiveUser);
         $sitemail = OW::getConfig()->getValue('base', 'site_email');
         $sitename = OW::getConfig()->getValue('base', 'site_name');
         $mail = OW::getMailer()->createMail();
         $mail->addRecipientEmail($tmpUser->getEmail());
         $mail->setSender($sitemail, $sitename);
         $mail->setSenderSuffix(true);
         $mail->setSubject($subject);
         $mail->setHtmlContent($content);
         $mail->setTextContent(UTIL_HtmlTag::stripTags($content));
         OW::getMailer()->addToQueue($mail);
     }
     if (OW::getConfig()->getValue('credits', 'enablePM') == '1') {
         $conversation = MAILBOX_BOL_ConversationService::getInstance()->createConversation($userId, $receiveUser, $subject, $content);
     }
     return true;
 }
Example #2
0
 public function send(array $params = null)
 {
     if (!OW::getUser()->isAuthenticated()) {
         throw new AuthenticateException();
     }
     $receiveUser = $params['id'];
     if (!OW::getUser()->isAuthorized('credits', 'send') || !OW::getAuthorization()->isUserAuthorized($receiveUser, 'credits', 'receive') || !isset($params['id'])) {
         $this->setTemplate(OW::getPluginManager()->getPlugin('base')->getCtrlViewDir() . 'authorization_failed.html');
         return;
     }
     $language = OW::getLanguage();
     $config = OW::getConfig();
     $userId = OW::getUser()->getId();
     $userCredits = USERCREDITS_BOL_CreditsService::getInstance()->getCreditsBalance($userId);
     $this->assign('userCredits', $userCredits);
     $this->assign('receiveUserName', BOL_UserService::getInstance()->getDisplayName($receiveUser));
     $form = new Form('creditForm');
     $element = new TextField('creditPoint');
     $element->setRequired(true);
     $element->setLabel($language->text('credits', 'credits_to_send'));
     $element->addAttribute("style", "width: 100px;");
     $validator = new IntValidator(1, $userCredits);
     $validator->setErrorMessage($language->text('credits', 'credit_value_error'));
     $element->addValidator($validator);
     $form->addElement($element);
     $element = new Submit('sendCredit');
     $element->setValue($language->text('credits', 'send_credits'));
     $form->addElement($element);
     if (OW::getRequest()->isPost()) {
         if ($form->isValid($_POST)) {
             $values = $form->getValues();
             $creditValue = (int) $values['creditPoint'];
             if (CREDITS_BOL_Service::getInstance()->transferCredits($userId, $receiveUser, $creditValue)) {
                 OW::getFeedback()->info($language->text('credits', 'credit_transfer_ok'));
                 $this->redirect(OW::getRouter()->urlForRoute('credits_transfer'));
             } else {
                 OW::getFeedback()->error($language->text('credits', 'credit_transfer_fail'));
             }
         }
     }
     $this->addForm($form);
     $this->setPageHeading($language->text('credits', 'transfer_credits_label'));
     $this->setPageTitle($language->text('credits', 'transfer_credits_label'));
     $this->setPageHeadingIconClass('ow_ic_gear_wheel');
 }