Files

314 lines
9.9 KiB
PHP
Raw Permalink Normal View History

<?php
class ContentActions extends Actions
{
2021-05-14 20:10:54 +08:00
public const
2016-09-05 18:19:12 -04:00
SLUG_RSS = 'rss.xml',
SLUG_NEWS = 'news',
SLUG_FAQ = 'faq',
SLUG_CREDIT_REPORTS = 'credit-reports',
2018-04-25 21:07:09 -04:00
SLUG_JOBS = 'jobs',
2016-09-05 18:19:12 -04:00
URL_NEWS = '/' . self::SLUG_NEWS,
URL_FAQ = '/' . self::SLUG_FAQ,
URL_CREDIT_REPORTS = '/' . self::SLUG_CREDIT_REPORTS,
2016-09-05 18:19:12 -04:00
2016-09-07 22:01:30 -04:00
CONTENT_DIR = ROOT_DIR . '/content',
VIEW_FOLDER_NEWS = self::CONTENT_DIR . '/' . self::SLUG_NEWS,
VIEW_FOLDER_FAQ = self::CONTENT_DIR . '/' . self::SLUG_FAQ,
2018-04-25 21:07:09 -04:00
VIEW_FOLDER_CREDIT_REPORTS = self::CONTENT_DIR . '/' . self::SLUG_CREDIT_REPORTS,
VIEW_FOLDER_JOBS = self::CONTENT_DIR . '/' . self::SLUG_JOBS;
2016-05-27 16:11:46 -04:00
2018-05-21 11:49:15 -04:00
public static function executeHome(): array
2016-07-11 22:21:24 -04:00
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2018-05-21 11:49:15 -04:00
return ['page/home'];
}
2017-03-14 16:57:13 -04:00
2019-02-01 16:30:09 -06:00
//
public static function executeOrg(): array
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2019-02-01 16:30:09 -06:00
return ['page/org'];
}
public static function executeTv(): array
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2019-02-01 16:30:09 -06:00
return ['page/tv'];
}
//
public static function executeNews(?string $slug = null): array
2018-05-21 11:49:15 -04:00
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2018-05-21 11:49:15 -04:00
if (!$slug || $slug == static::SLUG_RSS) {
$posts = array_filter(
2021-05-14 20:10:54 +08:00
Post::find(static::VIEW_FOLDER_NEWS, Post::SORT_DATE_DESC),
function (Post $post) {
2018-05-21 11:49:15 -04:00
return !$post->getDate() || $post->getDate()->format('U') <= date('U');
}
2021-05-14 20:10:54 +08:00
);
2018-05-21 11:49:15 -04:00
if ($slug == static::SLUG_RSS) {
Response::setHeader(Response::HEADER_CONTENT_TYPE, 'text/xml; charset=utf-8');
return ['content/rss', [
2017-03-14 16:57:13 -04:00
'posts' => array_slice($posts, 0, 10),
'_no_layout' => true
]];
2018-05-21 11:49:15 -04:00
}
2017-03-14 16:57:13 -04:00
2018-05-21 11:49:15 -04:00
return ['content/news', [
2016-09-05 18:19:12 -04:00
'posts' => $posts,
View::LAYOUT_PARAMS => [
'showRssLink' => true
]
]];
2018-05-21 11:49:15 -04:00
}
2016-07-11 22:21:24 -04:00
2018-05-21 11:49:15 -04:00
try {
$post = Post::load(static::SLUG_NEWS . '/' . ltrim($slug, '/'));
2019-03-25 17:25:04 -04:00
} catch (PostException $e) {
2018-05-21 11:49:15 -04:00
return NavActions::execute404();
}
2016-09-05 18:19:12 -04:00
2018-05-21 11:49:15 -04:00
return ['content/news-post', [
2016-09-05 18:19:12 -04:00
'post' => $post,
2016-05-27 16:11:46 -04:00
View::LAYOUT_PARAMS => [
'showRssLink' => true
]
]];
2018-05-21 11:49:15 -04:00
}
2016-05-27 16:11:46 -04:00
public static function executeFaq(?string $slug = null): array
2016-09-05 18:19:12 -04:00
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2016-09-05 18:19:12 -04:00
2018-05-21 11:49:15 -04:00
if (!$slug) {
$allPosts = Post::find(static::VIEW_FOLDER_FAQ, Post::SORT_ORD_ASC);
$allCategories = [
'LBRY 101' => 'Intro to LBRY',
2016-09-05 18:19:12 -04:00
'getstarted' => 'Getting Started',
2019-10-18 18:20:26 -04:00
'tutorial' => 'LBRY Tutorials',
'publisher' => 'Publishers and Creators',
2017-07-18 09:58:56 -04:00
'troubleshooting' => 'Help and Troubleshooting',
2020-02-07 15:50:41 -05:00
'powerusers' => 'LBRY for Power Users',
'wallet' => 'Wallet and Transactions',
'bots' => 'LBRY Bots',
'mining' => 'Mining LBC',
2016-10-11 16:39:14 -04:00
'differences' => 'What Makes LBRY Different?',
'other' => 'Other Questions',
2016-10-11 16:39:14 -04:00
] + Post::collectMetadata($allPosts, 'category');
2018-05-21 11:49:15 -04:00
$selectedCategory = Request::getParam('category');
$filters = array_filter([
2016-09-05 18:19:12 -04:00
'category' => $selectedCategory && isset($allCategories[$selectedCategory]) ? $selectedCategory : null,
]);
2018-05-21 11:49:15 -04:00
$posts = $filters ? Post::filter($allPosts, $filters) : $allPosts;
2016-09-05 18:19:12 -04:00
2018-05-21 11:49:15 -04:00
$groups = array_fill_keys(array_keys($allCategories), []);
2016-09-05 18:19:12 -04:00
2018-05-21 11:49:15 -04:00
foreach ($posts as $post) {
$groups[$post->getCategory()][] = $post;
}
2016-09-05 18:19:12 -04:00
2018-05-21 11:49:15 -04:00
return ['content/faq', [
2016-09-05 18:19:12 -04:00
'categories' => $allCategories,
'selectedCategory' => $selectedCategory,
'postGroups' => $groups
]];
2018-05-21 11:49:15 -04:00
}
try {
$post = Post::load(static::SLUG_FAQ . '/' . ltrim($slug, '/'));
2019-03-25 17:25:04 -04:00
} catch (PostException $e) {
2018-05-21 11:49:15 -04:00
return Controller::redirect('/' . static::SLUG_FAQ);
}
return ['content/faq-post', ['post' => $post]];
2016-09-05 18:19:12 -04:00
}
2018-05-21 11:49:15 -04:00
public static function executeCreditReports(?string $year = null, ?string $month = null): array
2016-08-18 14:05:32 -04:00
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2018-05-21 11:49:15 -04:00
$posts = Post::find(static::VIEW_FOLDER_CREDIT_REPORTS);
2018-05-21 11:49:15 -04:00
return ['content/credit-reports', [
'posts' => $posts
]];
}
2018-05-21 11:49:15 -04:00
public static function executeCreditReport(?string $year = null, ?string $quarter = null): array
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2018-05-21 11:49:15 -04:00
try {
$post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter);
2019-03-25 17:25:04 -04:00
} catch (PostException $e) {
2018-05-21 11:49:15 -04:00
return Controller::redirect('/' . static::SLUG_CREDIT_REPORTS);
}
$metadata = $post->getMetadata();
return ['content/credit-report', [
'post' => $post,
'sheetUrl' => $metadata['sheet']
]];
2016-09-07 11:17:38 -04:00
}
2018-05-21 11:49:15 -04:00
2016-09-07 11:17:38 -04:00
public static function executeBountyRedirect(?string $slug = null): array
2016-09-07 22:01:30 -04:00
{
2019-11-05 15:27:56 -05:00
return Controller::redirect('https://lbry.tech/contribute');
2016-09-01 10:31:17 -04:00
}
2016-08-29 16:04:51 -04:00
2020-02-07 15:50:41 -05:00
public static function executeRoadmap(string $year = '2020')
2018-05-21 11:49:15 -04:00
{
$cache = !Request::getParam('nocache');
2020-02-04 18:33:01 -05:00
$years = range(2019, 2020);
2020-02-07 15:50:41 -05:00
$year = $year ?: $years[count($years) - 1];
2020-02-04 18:33:01 -05:00
if (!in_array($year, $years)) {
2020-02-07 15:50:41 -05:00
Controller::redirect('/roadmap');
2020-02-04 18:33:01 -05:00
}
2018-05-21 11:49:15 -04:00
return ['content/roadmap', [
2020-02-04 18:33:01 -05:00
'year' => $year,
'years' => $years,
'items' => Github::listRoadmapItems($year, $cache)
2019-01-28 17:48:55 -05:00
]];
2018-05-21 11:49:15 -04:00
}
2016-08-10 16:57:01 -04:00
2018-05-21 11:49:15 -04:00
public static function executePressKit(): array
{
// $zipFileName = 'lbry-press-kit-' . date('Y-m-d') . '.zip';
// $zipPath = tempnam('/tmp', $zipFileName);
2016-06-29 13:56:11 -04:00
//
// $zip = new ZipArchive();
// $zip->open($zipPath, ZipArchive::OVERWRITE);
//
// foreach (glob(ROOT_DIR . '/web/img/press/*') as $productImgPath) {
// $imgPathTokens = explode('/', $productImgPath);
// $imgName = $imgPathTokens[count($imgPathTokens) - 1];
// $zip->addFile($productImgPath, 'logo_and_product/' . $imgName);
// }
//
// foreach (glob(ContentActions::CONTENT_DIR . '/bio/*.md') as $bioPath) {
// list($metadata, $bioHtml) = View::parseMarkdown($bioPath);
// $zip->addFile($bioPath, 'team_bios/' . $metadata['name'] . ' - ' . $metadata['role'] . '.txt');
// }
2016-06-29 10:52:05 -04:00
2018-05-21 11:49:15 -04:00
/*
* team bio images are no longer included in press kit now that they've moved to spee.ch
* this should be fixed if we care about the press-kit page
*/
2017-12-18 17:14:05 -05:00
// foreach (array_filter(glob(ROOT_DIR . '/web/img/team/*.jpg'), function ($path)
// {
// return strpos($path, 'spooner') === false;
// }) as $bioImgPath)
// {
// $imgPathTokens = explode('/', $bioImgPath);
// $imgName = str_replace('644x450', 'lbry', $imgPathTokens[count($imgPathTokens) - 1]);
// $zip->addFile($bioImgPath, '/team_photos/' . $imgName);
// }
2016-06-29 10:52:05 -04:00
//
// $zip->close();
//
2019-09-22 10:06:46 -04:00
// Response::enablePublicImmutableCache();
// Response::setDownloadHttpHeaders($zipFileName, 'application/zip', filesize($zipPath));
//
// return ['internal/zip', [
// '_no_layout' => true,
// 'zipPath' => $zipPath
// ]];
2018-05-21 11:49:15 -04:00
}
2016-06-29 10:52:05 -04:00
2018-05-21 11:49:15 -04:00
public static function prepareBioPartial(array $vars): array
{
$person = $vars['person'];
$path = 'bio/' . $person . '.md';
2021-05-31 17:43:51 -04:00
[$metadata, $bioHtml] = View::parseMarkdown($path);
2018-05-21 11:49:15 -04:00
return $vars + $metadata + [
2021-05-31 17:43:51 -04:00
'imgSrc' => '/img/bio/' . $person . '.jpg',
'bioHtml' => $bioHtml,
];
}
public static function prepareBioCirclePartial(array $vars): array
{
$post = ContentActions::prepareBioPartial(['person' => $vars['slug']]);
return [
'bioSlug' => $vars['slug'],
'imgSrc' => $post['imgSrc'],
'name' => $post['name'],
'role' => $post['role'],
];
2018-05-21 11:49:15 -04:00
}
2018-01-21 15:44:03 -05:00
2018-05-21 11:49:15 -04:00
public static function preparePostAuthorPartial(array $vars): array
{
$post = $vars['post'];
return [
2021-05-31 17:43:51 -04:00
'authorName' => $post->getAuthorName(),
'photoImgSrc' => $post->getAuthorPhoto(),
'authorBioHtml' => $post->getAuthorBioHtml(),
'authorGithub' => $post->getAuthorGithubID(),
'authorTwitter' => $post->getAuthorTwitterID(),
'authorEmail' => $post->getAuthorPostEmail()
];
2018-05-21 11:49:15 -04:00
}
2018-05-21 11:49:15 -04:00
public static function preparePostListPartial(array $vars): array
{
$count = $vars['count'] ?? 3;
return [
2021-05-31 17:43:51 -04:00
'posts' => array_slice(Post::find(static::VIEW_FOLDER_NEWS, Post::SORT_DATE_DESC), 0, $count)
];
2018-05-21 11:49:15 -04:00
}
public static function executePostCategoryFilter(string $category)
{
2019-09-22 10:06:46 -04:00
Response::enablePublicImmutableCache();
2018-01-29 15:53:50 -05:00
2018-05-21 11:49:15 -04:00
$filter_post = [];
2018-01-29 15:53:50 -05:00
2018-05-21 11:49:15 -04:00
$posts = array_filter(
2021-05-14 20:10:54 +08:00
Post::find(static::VIEW_FOLDER_NEWS, Post::SORT_DATE_DESC),
function (Post $post) use ($category) {
2021-05-31 17:43:51 -04:00
return (($post->getCategory() === $category) && (!$post->getDate() || $post->getDate()->format('U') <= date('U')));
}
2021-05-14 20:10:54 +08:00
);
2018-01-29 15:53:50 -05:00
2018-05-21 11:49:15 -04:00
return ['content/news', [
2021-05-31 17:43:51 -04:00
'posts' => $posts,
View::LAYOUT_PARAMS => [
'showRssLink' => true
]
]];
2018-05-21 11:49:15 -04:00
}
2018-04-25 21:07:09 -04:00
2018-05-21 11:49:15 -04:00
public static function prepareJobsPartial(array $vars)
{
$jobs =
2021-05-31 17:43:51 -04:00
array_filter(
array_map('View::parseMarkdown', glob(static::VIEW_FOLDER_JOBS . '/*')),
function ($job) {
return $job[0]['status'] !== 'closed';
}
);
2018-04-25 21:07:09 -04:00
2018-05-21 11:49:15 -04:00
usort($jobs, function ($jobA, $jobB) {
if ($jobA[0]['status'] === 'active' xor $jobB[0]['status'] === 'active') {
return $jobA[0]['status'] === 'active' ? -1 : 1;
}
return $jobA[0]['order'] <=> $jobB[0]['order'];
});
2018-05-21 11:49:15 -04:00
return $vars + ['jobs' => $jobs];
}
}