public function run() { $faker = Faker::create(); $log = new Stream('php://stdout'); $log->info('Start ' . __CLASS__); /** @var Phalcon\Db\AdapterInterface $database */ $database = $this->getDI()->get('db'); $userIds = Users::find(['columns' => 'id'])->toArray(); $database->begin(); for ($i = 0; $i <= self::POSTS_TOTAL; $i++) { $title = $faker->company; $userRandId = array_rand($userIds); $posts = new Posts(); $posts->usersId = $userIds[$userRandId]['id']; $posts->type = rand(0, 1) ? 'questions' : 'tips'; $posts->title = $title; $posts->slug = \Phalcon\Tag::friendlyTitle($title); $posts->numberViews = rand(5, 100); $posts->numberReply = rand(0, 20); $posts->content = $faker->text; $posts->sticked = 'N'; $posts->status = 'A'; $posts->locked = 'N'; $posts->deleted = 0; $posts->acceptedAnswer = 'N'; if (!$posts->save()) { var_dump($posts->getMessages()); $database->rollback(); die; } $log->info('posts: ' . $posts->getTitle()); } }
/** * */ public function process() { $badges = $this->getBadges(); foreach (Users::find() as $user) { $this->processUserBadges($user, $badges); } }
/** * Sends the digest */ public function send() { $lastMonths = new \DateTime(); $lastMonths->modify('-6 month'); $parameters = array('modifiedAt >= ?0 AND digest = "Y" AND notifications <> "N"', 'bind' => [$lastMonths->getTimestamp()]); $users = []; foreach (Users::find($parameters) as $user) { $toMail = $user->getEmail(); if ($toMail && strpos($user->email, '@phalconbook') === false) { $users[trim($toMail)] = $user->getInforUser(); } } $sitename = '[ ' . $this->config->application->name . ' Forum ]'; $subject = 'Top Stories from ' . $sitename . date('d/m/y'); foreach ($users as $email => $username) { try { $params = ['username' => $username, 'subject' => $subject, 'posts' => $this->getData()]; if (!$this->mail->send($email, 'senddigest', $params)) { var_dump('send disgest email false'); } } catch (\Exception $e) { echo $e->getMessage(), PHP_EOL; } } }
/** * Implement hook beforeUpdate of Model Phalcon * * @return mixed */ public function afterCreate() { if ($this->id > 0) { /** * Register the activity */ $activity = new Activities(); $activity->setUsersId($this->usersId); $activity->setPostsId($this->id); $activity->setType(Activities::NEW_POSTS); $activity->save(); /** * Register the user in the post's notifications */ $notification = new PostsNotifications(); $notification->setUsersId($this->usersId); $notification->setPostsId($this->id); $notification->save(); $toNotify = []; /** * Notify users that always want notifications */ foreach (Users::find(['notifications = "Y"', 'columns' => 'id'])->toArray() as $user) { if ($this->usersId != $user['id']) { $notificationId = $this->setNotification($user['id'], $this->id, null, Notifications::TYPE_POSTS); $toNotify[$user['id']] = $notificationId; } } /** * Queue notifications to be sent */ $this->getDI()->getQueue()->put($toNotify); } }
public function afterCreate() { if ($this->id > 0) { $activity = new Activities(); $activity->setUsersId($this->usersId); $activity->setPostsId($this->postsId); $activity->setType(Activities::NEW_REPLY); $activity->save(); $toNotify = []; /** * Notify users that always want notifications */ foreach (Users::find(['notifications = "Y"', 'columns' => 'id'])->toArray() as $user) { if ($this->usersId != $user['id']) { $notificationId = $this->setNotification($user['id'], $this->postsId, $this->id, Notifications::TYPE_REPLY); $this->setActivityNotifications($user['id'], $this->postsId, $this->id, $this->usersId, ActivityNotifications::TYPE_REPLY); $toNotify[$user['id']] = $notificationId; } } /** * Notify users that always want notifications for comment */ /** * Register users subscribed to the post */ foreach (PostsSubscribers::findByPostsId($this->postsId) as $subscriber) { if (!isset($toNotify[$subscriber->getUsersId()])) { $notificationId = $this->setNotification($subscriber->getUsersId(), $this->postsId, $this->id, Notifications::TYPE_REPLY); $this->setActivityNotifications($subscriber->getUsersId(), $this->postsId, $this->id, $this->usersId, ActivityNotifications::TYPE_REPLY); $toNotify[$subscriber->getUsersId()] = $notificationId; } } /** * Register the user in the post's notifications */ if (!isset($toNotify[$this->usersId])) { $parameters = ['usersId = ?0 AND postsId = ?1', 'bind' => array($this->usersId, $this->postsId)]; $hasNotifications = PostsNotifications::count($parameters); if (!$hasNotifications) { $notification = new PostsNotifications(); $notification->setUsersId($this->usersId); $notification->setPostsId($this->postsId); $notification->save(); } } /** * Queue notifications to be sent */ $this->getDI()->getQueue()->put($toNotify); } }