public function createArrayOfObject(Entry $object) { $data = array(); $data["id"] = $object->getId(); $data["title"] = $object->getTitle(); $data["created"] = $this->getISO8601ForUser($object->getDate()); $source = $object->getSource(); if ($source instanceof Email) { $data["type"] = "email"; } else { if ($source instanceof Feed) { $data["type"] = "feed"; } } $topic = $object->getTopic(); if ($topic) { $data["topic"] = array("id" => $topic->getId(), "name" => $topic->getName(), "seoName" => $topic->getSeoName()); } $data["category"] = array("id" => $object->getCategory()->getId(), "name" => $object->getCategory()->getName(), "forum" => array("id" => $object->getCategory()->getTargetForum()->getId(), "name" => $object->getCategory()->getTargetForum()->getName())); return $data; }
public function collectNews($page = 1, $limit = 20, &$errors) { $objects = array(); $categories = $this->em->getRepository('SymbbCoreNewsBundle:Category')->findAll(); /** * @var $categories Category[] */ foreach ($categories as $category) { $sources = $category->getSources(); foreach ($sources as $source) { $date = new \DateTime(); if ($source->getLastCall()) { $date->setTimestamp($source->getLastCall()); } else { $date->modify("- 2 weeks"); } $source->setLastCall(new \DateTime()); if ($source instanceof Category\Source\Email) { // $connection is instance of \Ddeboer\Imap\Connection try { $connection = $this->getEmailConnection($source); $category = $source->getCategory(); $mailboxes = $connection->getMailboxes(); foreach ($mailboxes as $mailbox) { if (strpos($mailbox->getName(), "INBOX") === 0) { $search = new SearchExpression(); $search->addCondition(new After($date)); $search->addCondition(new Undeleted()); $messages = $mailbox->getMessages($search); foreach ($messages as $message) { // imap filter will only check the day not the time... if ($message->getDate() <= $date) { continue; } $text = $message->getBodyHtml(); if (empty($text)) { $text = $message->getBodyText(); } $text = Utils::purifyHtml($text); $objects[] = array('email_id' => $message->getId(), 'title' => $message->getSubject(), 'text' => $text, 'realSource' => $message->getFrom(), 'date' => $message->getDate(), 'type' => "email", 'category' => $category, 'source' => $source); } } } } catch (\Exception $exp) { $errors[] = array('title' => "error while connection to email server", "text" => $exp->getMessage(), "date" => new \DateTime()); } } else { if ($source instanceof Category\Source\Feed) { $reader = $this->feedReader; $feed = $reader->getFeedContent($source->getUrl(), $date); $items = $feed->getItems(); foreach ($items as $item) { $objects[] = array('feed_id' => null, 'title' => (string) $item->getTitle(), 'text' => (string) $item->getDescription(), 'date' => $item->getUpdated(), 'realSource' => (string) $item->getLink(), 'type' => "feed", 'category' => $category, 'source' => $source); } } } $this->em->persist($source); } } foreach ($objects as $object) { $entry = new Category\Entry(); $entry->setCategory($object["category"]); $entry->setSource($object["source"]); $entry->setType($object["type"]); $entry->setTitle($object["title"]); $entry->setText($object["text"]); $entry->setDate($object["date"]); $this->em->persist($entry); } $this->em->flush(); $qb = $this->em->getRepository('SymbbCoreNewsBundle:Category\\Entry')->createQueryBuilder('e'); $qb->select("e"); $qb->where("e.topic IS NULL"); $qb->addOrderBy("e.date", "desc"); $query = $qb->getQuery(); $objects = $this->createPagination($query, $page, $limit); return $objects; }