/** * Get a instance for a name * * @param string $name * @return self */ static function get($name) { $object = RDR_Category::getByCondition(db()->quote("user") . " = {1} AND name = {0}", array($name, user())); if ($object) { return reset($object); } $object = new self(db()); $object->name = $name; $object->user = user(); $object->store(); return $object; }
/** * Delete the feed */ public function delete() { if (!$this->getId()) { return; } db()->deleteMultiple(RDR_Entry::getByCondition("feed = {0}", array($this))); $cats = RDR_Category::getByQuery("\n SELECT o FROM RDR_Category_feeds\n WHERE k = " . $this->getId() . "\n "); foreach ($cats as $category) { $feeds = $category->feeds; if ($feeds) { foreach ($feeds as $key => $feed) { if (compare($this, $feed)) { unset($feeds[$key]); } if (count($feeds) != count($category->feeds)) { $category->store(); } } } } parent::delete(); RDR_Cleanup::cleanupFlags(); }
/** * Load the View */ public function onLoad() { needRole(null, true); # OPML if (get("opml")) { $categories = user()->getCategories(); $opml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><opml></opml>'); $opml->addAttribute("version", "1.0"); $head = $opml->addChild("head"); $head->addChild("title", "Subscriptions from nReeda"); $body = $opml->addChild("body"); foreach ($categories as $category) { $cat = $body->addChild("outline"); $cat->addAttribute("title", $category->name); $cat->addAttribute("text", $category->name); $feeds = $category->feeds; if ($feeds) { foreach ($feeds as $feed) { $f = $cat->addChild("outline"); $f->addAttribute("type", "rss"); $f->addAttribute("text", $feed->getCustomName($category)); $f->addAttribute("title", $feed->getCustomName($category)); $f->addAttribute("xmlUrl", $feed->url); } } } $data = $opml->asXML(); CHOQ_OutputManager::cleanAllBuffers(); header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"nreeda.opml\""); echo $data; die; } # textfile if (get("file")) { $categories = user()->getCategories(); $lines = array(); foreach ($categories as $category) { $feeds = $category->feeds; if ($feeds) { foreach ($feeds as $feed) { $lines[] = $feed->url; } } } $data = implode("\n", $lines); CHOQ_OutputManager::cleanAllBuffers(); header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"nreeda.txt\""); echo $data; die; } # Import if (isset($_FILES["file"]["tmp_name"])) { $data = file_get_contents($_FILES["file"]["tmp_name"]); if (strpos($data, "<?xml") === false || strpos($data, "</opml>") === false) { $event = RDR_Import::importFromFile($_FILES["file"]["tmp_name"]); if ($event->type == RDR_Event::TYPE_FILE_OK) { RDR_Import::updateAllFeeds(); } v("message", $event->getText()); } else { $event = RDR_Import::importFromOPML($_FILES["file"]["tmp_name"]); if ($event->type == RDR_Event::TYPE_OPML_OK) { RDR_Import::updateAllFeeds(); } v("message", $event->getText()); } } if (post("new") && trim(post("val"))) { RDR_Category::get(post("val")); redirect(url()->getUri(), 302); } if (req()->isAjax()) { $categories = user()->getCategories(); $feeds = user()->getFeeds(); if (post("action") == "edit" && post("val")) { if (isset($categories[post("category")])) { $category = $categories[post("category")]; if (post("feed")) { $feed = arrayValue($feeds, post("feed")); if ($feed) { $feed->setCustomName($category, post("val")); $category->store(); } } else { $category->name = post("val"); $category->store(); } } } if (post("action") == "move") { if (isset($categories[post("categoryOld")])) { $categoryOld = $categories[post("categoryOld")]; $categoryNew = $categories[post("categoryNew")]; if (post("feed")) { $feed = arrayValue($feeds, post("feed")); if ($feed) { $name = $feed->getCustomName($categoryOld); $categoryOld->remove("feedsData", $feed->getId() . "-name"); $categoryOld->remove("feeds", $feed->getId()); $categoryOld->store(); $feed->setCustomName($categoryNew, $name); $categoryNew->add("feeds", $feed); $categoryNew->store(); } } } } if (post("action") == "delete") { if (isset($categories[post("category")])) { $category = $categories[post("category")]; if (post("feed")) { $feed = arrayValue($feeds, post("feed")); if ($feed) { $category->remove("feedsData", $feed->getId() . "-name"); $category->remove("feeds", $feed); $category->store(); } } else { $category->delete(); } } } RDR_Cleanup::cleanupFeeds(); RDR_Cleanup::cleanupFlags(); user()->updateNewsCache(); return; } view("RDR_BasicFrame", array("view" => $this)); }
/** * Get content */ public function getContent() { if (get("feed") && is_array(get("feed"))) { if (post("save")) { $count = 0; $feeds = post("feed"); $message = array(); if (is_array($feeds)) { foreach ($feeds as $key => $feed) { $href = post("href[{$key}]"); $title = post("title[{$key}]"); $cat = post("cat[{$key}]"); $category = RDR_Category::get($cat); $event = RDR_Import::addFeed($href, $category); if ($event->feed) { RDR_Import::importFeedEntries($event->feed); if ($href != $title) { $event->feed->setCustomName($category, $title); $category->store(); } } $message[] = $event->getText(); } } headline(t("browserscript.log")); ?> <p><?php echo implode("<br/>", $message); ?> </p> <p><?php echo t("browserscript.close"); ?> </p> <?php return; } headline(sprintf(t("browserscript.addfeeds"), s(get("site")))); $feeds = get("feed"); ?> <form name="d" method="post" action=""> <p> <?php $categories = user()->getCategories(); foreach ($feeds as $id => $feed) { $exp = explode(";", $feed, 2); ?> <input type="hidden" name="href[<?php echo s($id); ?> ]" value="<?php echo $exp[0]; ?> "/> <input type="hidden" name="title[<?php echo s($id); ?> ]" value="<?php echo $exp[1]; ?> "/> <input type="checkbox" name="feed[<?php echo s($id); ?> ]" value="<?php echo $feed; ?> "/> <select name="cat[<?php echo s($id); ?> ]"> <option value="<?php echo t("uncategorized"); ?> "><?php echo t("uncategorized"); ?> </option> <?php foreach ($categories as $category) { if ($category->name == t("uncategorized")) { continue; } ?> <option value="<?php echo $category->name; ?> "><?php echo s($category->name); ?> </option> <?php } ?> </select> <a href="<?php echo urldecode($exp[0]); ?> " target="_blank"><?php echo urldecode($exp[1]); ?> </a><br/> <?php } ?> <br/> <input type="submit" name="save" class="btn" value="<?php echo t("browserscript.addfeeds.btn"); ?> "/> </p> </form> <?php return; } $script = file_get_contents(CHOQ_ACTIVE_MODULE_DIRECTORY . "/view/_js/browser-script.js"); $script = str_replace(array("\t", "\r", "\n"), "", $script); $script = preg_replace("~\\s{2,99}~", "", $script); $script = str_replace("{url}", url()->getByAlias("root", l($this)), $script); $script = str_replace("{nofeed}", t("browserscript.nofeed"), $script); $script = str_replace("{forward}", t("browserscript.forward"), $script); $script = "javascript:" . $script; headline(t("sidebar.25")); ?> <p><?php echo t("browserscript.info"); ?> </p> <?php headline(t("browserscript.use")); ?> <p><?php echo t("browserscript.bookmark"); ?> <br/><br/> <a href='<?php echo $script; ?> '><?php echo s($script); ?> </a> </p> <?php }
/** * Recursive through all entries * * @param SimpleXMLElement[] $elements * @param int $level */ private static function _importFromOPML(array $elements, RDR_Category $category) { foreach ($elements as $element) { $attributes = $element->attributes(); if (!isset($attributes->xmlUrl)) { self::_importFromOPML($element->xpath("outline"), RDR_Category::get((string) $attributes->title)); } else { self::addFeed((string) $attributes->xmlUrl, $category); } } }
/** * Load the View */ public function onLoad() { if (!needRole()) { return; } $jsonData = null; switch (post("action")) { case "delete-feed-user": $feed = RDR_Feed::getById(post("data[fid]")); if ($feed) { $cats = user()->getCategories(); foreach ($cats as $category) { $feeds = $category->feeds; if ($feeds) { foreach ($feeds as $key => $catFeed) { if (compare($catFeed, $feed)) { unset($feeds[$key]); } } if (count($feeds) != count($category->feeds)) { $category->feeds = $feeds; $category->store(); } } } } break; case "delete-feed-admin": if (needRole(RDR_User::ROLE_ADMIN)) { $feed = RDR_Feed::getById(post("data[fid]")); if ($feed) { $feed->delete(); } } break; case "add-feed": $event = RDR_Import::addFeed(post("data[url]"), RDR_Category::get(post("data[category]"))); if ($event->feed) { RDR_Import::importFeedEntries($event->feed); } break; case "mark-all-as-readed": $cache = session("entry.ids.original"); if ($cache) { $ids = array_keys($cache); user()->loadReadedFlags(array_keys($ids)); $insertIds = array(); foreach ($ids as $id) { if (!isset(user()->_cacheReaded[$id])) { $insertIds[$id] = $id; } } if ($insertIds) { $query = "INSERT IGNORE INTO RDR_User_readed (o,k,v) VALUES "; foreach ($insertIds as $id) { $query .= " (" . user()->getId() . ", {$id}, 1), "; } $query = substr($query, 0, -2); db()->query($query); user()->updateReadedCount(); } } break; case "update-setting-user": user()->setting(post("data[key]"), post("data[value]")); user()->store(); break; case "update-newscache": user()->updateNewsCache(); $jsonData = user()->getAjaxData(); break; case "set-entries-readed": if (post("data[ids]")) { $entries = RDR_Entry::getByIds(post("data[ids]")); if ($entries) { user()->loadReadedFlags(array_keys($entries)); $insertIds = $deleteIds = array(); foreach ($entries as $entry) { $id = $entry->getId(); if ($id < user()->setting("init.entry")) { continue; } if (isset(user()->_cacheReaded[$id])) { $deleteIds[$id] = $id; } else { $insertIds[$id] = $id; } } if ($insertIds) { $query = "INSERT IGNORE INTO RDR_User_readed (o,k,v) VALUES "; foreach ($insertIds as $id) { $query .= " (" . user()->getId() . ", {$id}, 1), "; } $query = substr($query, 0, -2); db()->query($query); } if ($deleteIds) { $query = "DELETE FROM RDR_User_readed WHERE o = " . user()->getId() . " && k IN " . db()->toDb($deleteIds); } user()->updateReadedCount(); } } break; case "set-entries-saved": if (post("data[ids]")) { $entry = RDR_Entry::getById(post("data[ids][0]")); if ($entry) { if (user()->getByKey("saved", $entry->getId())) { user()->remove("saved", $entry->getId()); } else { user()->add("saved", 1, $entry->getId()); } user()->store(); } } break; case "set-feed-property": if (needRole(RDR_User::ROLE_ADMIN)) { $feed = RDR_Feed::getById(post("data[feed]")); if ($feed) { $feed->{post("data[field]")} = post("data[value]"); $feed->store(); } } break; } echo json_encode($jsonData, JSON_FORCE_OBJECT); }
/** * Get all categories to this user * * @return RDR_Category[] */ public function getCategories() { if (self::$_cache === null) { self::$_cache = RDR_Category::getByCondition(db()->quote("user") . " = {0}", array($this), "+name"); } return self::$_cache; }
/** * Load the View */ public function onLoad() { needRole(null, true); $p = $this->getParam("param"); $explodeParam = explode("-", $p); $pType = arrayValue($explodeParam, 0); $pVal = arrayValue($explodeParam, 1); $title = t("sidebar.10"); $categories = user()->getCategories(); $feeds = user()->getFeeds(); switch ($pType) { case "cat": if (isset($categories[$pVal])) { $this->category = $categories[$pVal]; $title = $this->category->name; $feeds = $this->category->feeds; if (isset($exp[1]) && isset($feeds[$exp[1]])) { $this->feed = $feeds[$exp[1]]; $title = $this->feed->name; } } break; case "feed": if (isset($feeds[$pVal])) { $this->feed = $feeds[$pVal]; $this->category = user()->getCategoryToFeed($this->feed); $title = $this->feed->getCustomName($this->category); } elseif (needRole(RDR_User::ROLE_ADMIN)) { $this->feed = RDR_Feed::getById($pVal); if ($this->feed) { $this->feed = $feeds[$pVal]; $title = $this->feed->name; } } break; case "search": $this->readedLayoutEnabled = false; $title = sprintf(t("feeds.1"), s(get("q"))); break; case "archive": $this->readedLayoutEnabled = false; $title = t("archive.title"); break; case "saved": $this->readedLayoutEnabled = false; $title = t("sidebar.11"); break; } if (req()->isAjax()) { # fetch all html to pass it to the json output ob_start(); if (!post("requestCounter")) { $hasUnreaded = false; $cache = session("entry.cache"); if (!$categories) { $ids = array(); } elseif ($pType == "search") { $feeds = user()->getFeeds(); $query = "\n SELECT t0.id\n FROM RDR_Entry as t0\n WHERE t0.feed IN " . db()->toDb($feeds) . " && t0.title LIKE " . db()->toDb("%" . get("q") . "%") . "\n ORDER BY datetime DESC\n "; $ids = db()->fetchColumn($query); } elseif ($pType == "archive") { $feeds = user()->getFeeds(); $condition = "1 "; if (get("from") && dt(get("from"))->valid) { $condition .= " && t0.datetime >= " . db()->toDb(dt(get("from"))->getSQLDate()); } if (get("to") && dt(get("to"))->valid) { $condition .= " && t0.datetime <= " . db()->toDb(dt(get("to"))->getSQLDate()); } $query = "\n SELECT t0.id\n FROM RDR_Entry as t0\n WHERE t0.feed IN " . db()->toDb($feeds) . " && {$condition}\n ORDER BY datetime DESC\n "; $ids = db()->fetchColumn($query); } elseif ($pType == "saved") { $ids = user()->saved; if ($ids) { $ids = array_keys($ids); } if (!$ids) { $ids = array(); } } else { $ids = array(); if ($cache) { user()->loadReadedFlags(array_keys($cache)); foreach ($cache as $id => $row) { # if already readed than hide the entry if (isset(user()->_cacheReaded[$id])) { continue; } # feed mode if ($this->feed) { if ($row["feed"] == $this->feed->getId()) { $ids[$id] = $id; } continue; } # category mode if ($this->category) { if ($row["category"] == $this->category->getId()) { $ids[$id] = $id; } continue; } # all mode $ids[$id] = $id; } } } # load readed flags only if required if ($this->readedLayoutEnabled) { user()->loadReadedFlags($ids); } # if view all and no article exist, update init entry to latest read id if ($pType == "all" && !$ids) { if ($feeds) { $id = db()->fetchOne("\n SELECT MAX(CAST(t0.k as signed))\n FROM RDR_User_readed as t0\n WHERE t0.o = " . user()->getId() . "\n "); user()->updateInitEntry($id); user()->updateNewsCache(); user()->readed = null; user()->store(); } } session("entry.ids", $ids); session("entry.ids.original", $ids); } $max = RDR_Entry::ENTRIES_PER_PAGE; $ids = session("entry.ids"); if (!$ids) { $ids = array(); } $entryIds = array_slice($ids, 0, $max); session("entry.ids", array_slice($ids, $max)); $entries = RDR_Entry::getByIds($entryIds, true); foreach ($entries as $entry) { $this->displayEntry($entry); } $c = count($entries); if ($c == $max) { ?> <div id="feed-view-trigger"></div> <?php } else { ?> <div class="feeds-footer"> <div style="display: none;"><input type="button" value="<?php echo t("mark.all.category"); ?> " class="btn" id="all-read-btn"/></div> <?php echo t("end.newssection.1"); ?> <br/> <div class="small"><?php echo t("end.newssection.2"); ?> </div> </div> <?php } $jsonData = user()->getAjaxData(); $jsonData["count"] = count(session("entry.ids.original")); if ($pType == "archive") { $jsonData["archive"] = count($ids); } $jsonData["content"] = ob_get_contents(); ob_end_clean(); echo json_encode($jsonData); return; } $this->parameters["title"] = $title; view("RDR_BasicFrame", array("view" => $this)); }