Exemplo n.º 1
0
    public function actionInviteTop($type, $limit = 100, $giveinvites = 0, $dry = false, $delay = 5)
    {
        $admin = User::model()->findByPk(1);
        $crt = new CDbCriteria(["condition" => "t.can & 1::bit(16) = 0::bit(16)", "limit" => (int) $limit]);
        if ($type == "karma") {
            $crt->order = "rate_u desc";
        } elseif ($type == "rating") {
            $crt->order = "rate_t desc";
        } elseif ($type == "translations") {
            $crt->order = "n_trs desc";
        } elseif ($type == "owners") {
            $crt->join = "RIGHT JOIN books b ON b.owner_id = t.id";
            $crt->addCondition("b.n_verses > 10 and b.last_tr > '2014-08-08' and b.last_tr <= '2015-01-01'");
            $crt->order = "n_trs desc";
        } else {
            echo "--type=karma|rating|owners|translations\n";
        }
        $crt->join .= "\nLEFT JOIN reg_invites invites ON t.id = invites.to_id";
        $crt->addCondition("invites.to_id IS NULL");
        $users = User::model()->findAll($crt);
        if (count($users) == 0) {
            echo "Нет кандидатов для приглашения\n";
            return;
        }
        $cnt = 0;
        foreach ($users as $user) {
            printf("Приглашается %d / %d %16s karma:%3d rating:%5d ntrs:%5d ...%s", ++$cnt, count($users), $user->login, $user->rate_u, $user->rate_t, $user->n_trs, str_repeat(" ", 16));
            $user->n_invites = $giveinvites;
            $user->save(false, ["n_invites"]);
            $invite = RegInvite::gen($admin);
            $invite->to_id = $user->id;
            $invite->to_email = $user->email;
            if ($giveinvites > 0) {
                $invite->message = <<<TTT
Вы можете пригласить сюда ещё {$giveinvites} человек. Для этого после восстановления членства, зайдите в свой профиль на
вкладку «Приглашения».";
TTT;
            }
            if ($dry) {
                echo "\n";
            } else {
                try {
                    $invite->sendMail();
                } catch (Exception $e) {
                    echo "ERROR SENDING to {$invite->to_email}: " . $e->getMessage() . "\n";
                }
                if (!$invite->save(false)) {
                    echo "ERROR SAVE\n" . print_r($invite->errors, true) . "\n";
                }
                sleep($delay);
                echo "\r";
            }
        }
        echo "\n";
    }
Exemplo n.º 2
0
 public function actionInvites($id)
 {
     if (p()["registerType"] != "INVITE") {
         $this->redirect("/users/{$id}");
     }
     $user = $this->loadUser($id);
     if ($user->id !== Yii::app()->user->id) {
         throw new CHttpException(404);
     }
     if (Yii::app()->request->isPostRequest) {
         if (isset($_POST["revoke"])) {
             $invite = RegInvite::model()->findByAttributes(["id" => (int) $_POST["revoke"], "from_id" => $user->id]);
             if ($invite) {
                 $invite->delete();
                 $user->n_invites++;
                 $user->save(false, ["n_invites"]);
                 Yii::app()->user->setFlash("success", "Приглашение отозвано.");
             } else {
                 Yii::app()->user->setFlash("error", "Приглашение не найдено.");
             }
             $this->redirect($user->getUrl("invites"));
         } elseif (isset($_POST["resend"])) {
             $invite = RegInvite::model()->findByAttributes(["id" => (int) $_POST["resend"], "from_id" => $user->id]);
             $invite->to_email = $invite->buddy->email;
             $invite->save(false, ["to_email"]);
             if ($invite) {
                 $invite->sendMail();
                 Yii::app()->user->setFlash("success", "Приглашение отправлено.");
             } else {
                 Yii::app()->user->setFlash("error", "Приглашение не найдено.");
             }
             $this->redirect($user->getUrl("invites"));
         } elseif (isset($_POST["invite"]) && $user->n_invites > 0) {
             $invite = RegInvite::gen($user);
             $invite->setAttributes($_POST["invite"]);
             if ($invite->validate()) {
                 // @todo: обернуть в транзакцию
                 $invite->save();
                 $user->n_invites--;
                 $user->save(false, ["n_invites"]);
                 $invite->sendMail();
                 Yii::app()->user->setFlash("success", "Приглашение отправлено!");
                 $this->redirect($user->getUrl("invites"));
             }
         }
     } else {
         $invite = new RegInvite();
         if ($_GET["who"]) {
             $invite->type = "user";
             $invite->clue = htmlspecialchars($_GET["who"]);
         }
     }
     $sent = RegInvite::model()->findAll(["condition" => "t.from_id = :me", "params" => ["me" => $user->id], "order" => "t.cdate desc", "with" => ["buddy"]]);
     $this->side_view = ["profile_side" => ["user" => $user, "userinfo" => $user->userinfo]];
     $this->render("invites", ["user" => $user, "invite" => $invite, "sent" => $sent]);
 }