diff --git a/Resources/templates/responsive/home/index.php b/Resources/templates/responsive/home/index.php index 3074cafd6c..0c2ac99d00 100644 --- a/Resources/templates/responsive/home/index.php +++ b/Resources/templates/responsive/home/index.php @@ -19,27 +19,25 @@ text('home-menu-toggle-label') ?> replace() ?> - - section('home-content') ?> insert('partials/components/main_slider', [ - 'banners' => $this->banners, - 'nav' => 'home/partials/main_slider_nav', - 'button_text' => $this->text('invest-more-info') + 'banners' => $this->response->getBanners(), + 'nav' => 'home/partials/main_slider_nav', + 'button_text' => $this->text('invest-more-info') ]) ?> insert('home/partials/projects') ?> - home['values']): ?> + response->getHomeItems()['values']): ?> insert('partials/components/values', [ 'title' => $this->t('home-footprint-values-title'), - 'footprints' => $this->footprints, - 'sdg_by_footprint' => $this->sdg_by_footprint, - 'projects_by_footprint' => $this->projects_by_footprint + 'footprints' => $this->response->getFootprints(), + 'sdg_by_footprint' => $this->response->getSdgByFootprint(), + 'projects_by_footprint' => $this->response->getProjectsByFootprint() ]) ?> @@ -61,4 +59,3 @@ insert('home/partials/modals') ?> replace() ?> - diff --git a/Resources/templates/responsive/home/partials/channels.php b/Resources/templates/responsive/home/partials/channels.php index 28c23a4421..e6f594f169 100644 --- a/Resources/templates/responsive/home/partials/channels.php +++ b/Resources/templates/responsive/home/partials/channels.php @@ -1,38 +1,34 @@ -channels && count($this->channels) > 1): ?> +response->getChannels() && count($this->response->getChannels()) > 1): ?>
matchfunding
-

- text('home-channels-title') ?> -

+

text('home-channels-title') ?>

- channels as $channel): ?> + response->getChannels() as $channel): ?> getSummary(); ?> owner_background; ?>
-
-
-
- - <?= $channel->name ?> - -
+
+
+ -
- -
- text_truncate($channel->description, 120) ?> -
+
+
+ +
text_truncate($channel->description, 120) ?>
+
diff --git a/Resources/templates/responsive/home/partials/projects.php b/Resources/templates/responsive/home/partials/projects.php index 4dfa4d67ae..532265b825 100644 --- a/Resources/templates/responsive/home/partials/projects.php +++ b/Resources/templates/responsive/home/partials/projects.php @@ -8,10 +8,10 @@
- projects): ?> + response->getProjects()): ?> insert('home/partials/projects_list', [ - 'projects' => $this->projects, - 'total_projects' => $this->total_projects + 'projects' => $this->response->getProjects(), + 'total_projects' => $this->response->getTotalProjects() ]) ?>
diff --git a/Resources/templates/responsive/home/partials/projects_list.php b/Resources/templates/responsive/home/partials/projects_list.php index 9c4d96e186..c779914571 100644 --- a/Resources/templates/responsive/home/partials/projects_list.php +++ b/Resources/templates/responsive/home/partials/projects_list.php @@ -1,5 +1,7 @@ -
- projects as $project) : ?> +
+ response->getProjects() as $project) : ?>
insert('project/widgets/normal', [ 'project' => $project diff --git a/Resources/templates/responsive/home/partials/sponsors.php b/Resources/templates/responsive/home/partials/sponsors.php index bf7b7bcc32..b5d4b535ec 100644 --- a/Resources/templates/responsive/home/partials/sponsors.php +++ b/Resources/templates/responsive/home/partials/sponsors.php @@ -1,7 +1,7 @@
- sponsors): ?> + response->getSponsors()): ?>
- sponsors as $type => $sponsors): ?> + response->getSponsors() as $type => $sponsors): ?>
-
\ No newline at end of file +
diff --git a/Resources/templates/responsive/partials/components/values.php b/Resources/templates/responsive/partials/components/values.php index 192a580031..71b2b9fc6a 100644 --- a/Resources/templates/responsive/partials/components/values.php +++ b/Resources/templates/responsive/partials/components/values.php @@ -67,7 +67,7 @@
- id] as $index => $project): ?> + id] as $project): ?>
id ?>>
diff --git a/src/Goteo/Controller/DiscoverController.php b/src/Goteo/Controller/DiscoverController.php index e0789b3058..d275f09cfe 100644 --- a/src/Goteo/Controller/DiscoverController.php +++ b/src/Goteo/Controller/DiscoverController.php @@ -21,85 +21,13 @@ class DiscoverController extends Controller { + private Project\ProjectFilters $projectFilters; + public function __construct() { $this->dbReplica(true); $this->dbCache(true); - View::setTheme('responsive'); - } - - /** - * Returns an array suitable for Project::getList($filters) - */ - protected function getProjectFilters($filter, $vars = []): array - { - $filters = [ - 'status' => [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED], - 'published_since' => (new DateTime('-6 month'))->format('Y-m-d') - ]; - - $filters['order'] = 'project.status ASC, project.published DESC, project.name ASC'; - if($vars['q']) { - $filters['global'] = $vars['q']; - unset($filters['published_since']); - $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; - } - - if ($vars['category']) { - $filters['category'] = $vars['category']; - unset($filters['published_since']); - $filters['status'] = [ Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED ]; - } - - if ($vars['location'] || ($vars['latitude'] && $vars['longitude'])) { - unset($filters['published_since']); - $filters['location'] = new ProjectLocation(['location' => $vars['location'], 'latitude' => $vars['latitude'], 'longitude' => $vars['longitude'], 'radius' => 300]); - $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; - $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; - } - - if ($filter === 'near') { - // Nearby defined as 300Km distance - // Any LocationInterface will do (UserLocation, ProjectLocation, ...) - $filters['location'] = new ProjectLocation([ 'latitude' => $vars['latitude'], 'longitude' => $vars['longitude'], 'radius' => 300 ]); - $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; - } elseif($filter === 'outdated') { - $filters['type'] = 'outdated'; - $filters['status'] = Project::STATUS_IN_CAMPAIGN; - $filters['order'] = 'project.days ASC, project.published DESC, project.name ASC'; - } elseif($filter === 'promoted') { - $filters['type'] = 'promoted'; - $filters['status'] = Project::STATUS_IN_CAMPAIGN; - $filters['order'] = 'promote.order ASC, project.published DESC, project.name ASC'; - } elseif($filter === 'popular') { - $filters['type'] = 'popular'; - $filters['status'] = Project::STATUS_IN_CAMPAIGN; - $filters['order'] = 'project.popularity DESC, project.published DESC, project.name ASC'; - } elseif($filter === 'succeeded') { - $filters['type'] = 'succeeded'; - $filters['status'] = [Project::STATUS_FUNDED, Project::STATUS_FULFILLED]; - $filters['order'] = 'project.published DESC, project.name ASC'; - unset($filters['published_since']); - } elseif($filter === 'fulfilled') { - $filters['status'] = [Project::STATUS_FULFILLED]; - $filters['order'] = 'project.published DESC, project.name ASC'; - unset($filters['published_since']); - } elseif($filter === 'archived') { - $filters['status'] = [Project::STATUS_UNFUNDED]; - $filters['order'] = 'project.published DESC, project.name ASC'; - $filters['published_since'] = (new DateTime('-24 month'))->format('Y-m-d'); - } elseif($filter === 'matchfunding') { - $filters['type'] = 'matchfunding'; - unset($filters['published_since']); - } elseif($filter === 'recent') { - $filters['type'] = 'recent'; - } - - if($vars['review']) { - $filters['status'] = [ Project::STATUS_EDITING, Project::STATUS_REVIEWING, Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED ]; - $filters['is_draft'] = true; - } - return $filters; + $this->projectFilters = new Project\ProjectFilters(); } /* @@ -127,7 +55,7 @@ public function searchAction (Request $request, $filter = '') { $vars['review'] = $request->query->get('review') === '1' ? 1 : 0 ; } - $filters = $this->getProjectFilters($filter, $vars); + $filters = $this->projectFilters->getFilters($filter, $vars); $projects = Project::getList($filters, null, 0, $limit); $total = Project::getList($filters, null, 0, 0, true); @@ -165,7 +93,7 @@ public function ajaxSearchAction(Request $request) { 'status' => [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED], 'published_since' => (new DateTime('-6 month'))->format('Y-m-d') ]; - $filters = $this->getProjectFilters($filter, $vars); + $filters = $this->projectFilters->getFilters($filter, $vars); $offset = $pag * $limit; $total_projects = 0; diff --git a/src/Goteo/Controller/IndexController.php b/src/Goteo/Controller/IndexController.php index 04be29969b..0a7771950c 100644 --- a/src/Goteo/Controller/IndexController.php +++ b/src/Goteo/Controller/IndexController.php @@ -11,24 +11,15 @@ namespace Goteo\Controller; use Goteo\Application\View; -use Goteo\Application\Config; -use Goteo\Model\Banner; -use Goteo\Model\Footprint; -use Goteo\Model\Home; -use Goteo\Model\Project; -use Goteo\Model\Sdg; -use Goteo\Model\Stories; -use Goteo\Model\Node; -use Goteo\Model\Sponsor; -use Goteo\Util\Stats\Stats; +use Goteo\UseCase\Home\HomeUseCase; use Symfony\Component\HttpFoundation\Response; -class IndexController extends DiscoverController +class IndexController extends BaseSymfonyController { - public function __construct() { + parent::__construct(); $this->dbReplica(true); $this->dbCache(true); View::setTheme('responsive'); @@ -36,50 +27,11 @@ public function __construct() public function indexAction(): Response { - $limit = 24; - $filters = $this->getProjectFilters('promoted'); - $projects = Project::getList($filters, null, 0, $limit); - $total_projects = Project::getList($filters, null, 0, 0, true); - $stories = Stories::getList(['active' => true]); - $channels = Node::getAll(['status' => 'active', 'type' => 'channel']); - $banners = Banner::getAll(true); - $stats = Stats::create('home_stats'); - $sponsors = $this->getSponsors(); - $footprints = Footprint::getList(); - $home = Home::getAll(Config::get('node'), 'index'); - - $projects_by_footprint = []; - $sdg_by_footprint = []; - foreach($footprints as $footprint) { - $footprintImpactData[$footprint->id] = $footprint->getAllImpactData(); - $projects_by_footprint[$footprint->id] = Project::getByFootprint(['footprints' => $footprint->id, 'rand' => true, 'amount_bigger_than' => 7000]); - $sdg_by_footprint[$footprint->id] = Sdg::getList(['footprint' => $footprint->id]); - } + $useCase = new HomeUseCase(); + $response = $useCase->execute(); - return $this->viewResponse('home/index', [ - 'banners' => $banners, - 'projects' => $projects, - 'total_projects' => $total_projects, - 'limit' => $limit, - 'limit_add' => 12, // Limit for javascript on addSlick - 'stories' => $stories, - 'channels' => $channels, - 'stats' => $stats, - 'sponsors' => $sponsors, - 'footprints' => $footprints, - 'home' => $home, - 'projects_by_footprint' => $projects_by_footprint, - 'sdg_by_footprint' => $sdg_by_footprint, - 'footprint_impact_data' => $footprintImpactData, + return $this->renderFoilTemplate('home/index', [ + 'response' => $response, ]); } - - private function getSponsors(): array - { - $sponsors = []; - foreach(Sponsor::getTypes() as $type) { - $sponsors[$type] = Sponsor::getList(['type' => $type]); - } - return $sponsors; - } } diff --git a/src/Goteo/Model/Project/ProjectFilters.php b/src/Goteo/Model/Project/ProjectFilters.php new file mode 100644 index 0000000000..795ec03ece --- /dev/null +++ b/src/Goteo/Model/Project/ProjectFilters.php @@ -0,0 +1,100 @@ + [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED], + 'published_since' => (new DateTime('-6 month'))->format('Y-m-d') + ]; + + $filters['order'] = 'project.status ASC, project.published DESC, project.name ASC'; + if ($vars['q']) { + $filters['global'] = $vars['q']; + unset($filters['published_since']); + $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; + } + + if ($vars['category']) { + $filters['category'] = $vars['category']; + unset($filters['published_since']); + $filters['status'] = [ Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED ]; + } + + if ($vars['location'] || ($vars['latitude'] && $vars['longitude'])) { + unset($filters['published_since']); + $filters['location'] = new ProjectLocation([ + 'location' => $vars['location'], + 'latitude' => $vars['latitude'], + 'longitude' => $vars['longitude'], + 'radius' => 300 + ]); + $filters['status'] = [Project::STATUS_IN_CAMPAIGN, Project::STATUS_FUNDED, Project::STATUS_FULFILLED, Project::STATUS_UNFUNDED]; + $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; + } + + if ($filter === 'near') { + // Nearby defined as 300Km distance + // Any LocationInterface will do (UserLocation, ProjectLocation, ...) + $filters['location'] = new ProjectLocation([ + 'latitude' => $vars['latitude'], + 'longitude' => $vars['longitude'], + 'radius' => 300 + ]); + $filters['order'] = 'Distance ASC, project.status ASC, project.published DESC, project.name ASC'; + } elseif ($filter === 'outdated') { + $filters['type'] = 'outdated'; + $filters['status'] = Project::STATUS_IN_CAMPAIGN; + $filters['order'] = 'project.days ASC, project.published DESC, project.name ASC'; + } elseif ($filter === 'promoted') { + $filters['type'] = 'promoted'; + $filters['status'] = Project::STATUS_IN_CAMPAIGN; + $filters['order'] = 'promote.order ASC, project.published DESC, project.name ASC'; + } elseif ($filter === 'popular') { + $filters['type'] = 'popular'; + $filters['status'] = Project::STATUS_IN_CAMPAIGN; + $filters['order'] = 'project.popularity DESC, project.published DESC, project.name ASC'; + } elseif ($filter === 'succeeded') { + $filters['type'] = 'succeeded'; + $filters['status'] = [Project::STATUS_FUNDED, Project::STATUS_FULFILLED]; + $filters['order'] = 'project.published DESC, project.name ASC'; + unset($filters['published_since']); + } elseif ($filter === 'fulfilled') { + $filters['status'] = [Project::STATUS_FULFILLED]; + $filters['order'] = 'project.published DESC, project.name ASC'; + unset($filters['published_since']); + } elseif ($filter === 'archived') { + $filters['status'] = [Project::STATUS_UNFUNDED]; + $filters['order'] = 'project.published DESC, project.name ASC'; + $filters['published_since'] = (new DateTime('-24 month'))->format('Y-m-d'); + } elseif ($filter === 'matchfunding') { + $filters['type'] = 'matchfunding'; + unset($filters['published_since']); + } elseif ($filter === 'recent') { + $filters['type'] = 'recent'; + } + + if ($vars['review']) { + $filters['status'] = [ + Project::STATUS_EDITING, + Project::STATUS_REVIEWING, + Project::STATUS_IN_CAMPAIGN, + Project::STATUS_FUNDED, + Project::STATUS_FULFILLED, + Project::STATUS_UNFUNDED + ]; + $filters['is_draft'] = true; + } + + return $filters; + } +} diff --git a/src/Goteo/UseCase/Home/HomeUseCase.php b/src/Goteo/UseCase/Home/HomeUseCase.php new file mode 100644 index 0000000000..6891355be8 --- /dev/null +++ b/src/Goteo/UseCase/Home/HomeUseCase.php @@ -0,0 +1,80 @@ +projectFilters = new Project\ProjectFilters(); + } + + public function execute(): HomeUseCaseResponse + { + $footprints = Footprint::getList(); + $this->generateFootprintRelatedData($footprints); + $filters = $this->projectFilters->getFilters('promoted'); + + return new HomeUseCaseResponse( + Project::getList($filters, null, 0, HomeUseCaseResponse::LIMIT), + Project::getList($filters, null, 0, 0, true), + Stories::getList(['active' => true]), + Node::getAll(['status' => 'active', 'type' => 'channel']), + Banner::getAll(true), + Stats::create('home_stats'), + $footprints, + Home::getAll(Config::get('node'), 'index'), + $this->getSponsors(), + $this->projects_by_footprint, + $this->sdg_by_footprint, + $this->footprintImpactData, + ); + } + + /** + * @param Footprint[] $footprints + * @return void + */ + private function generateFootprintRelatedData(array $footprints): void + { + foreach ($footprints as $footprint) { + $this->footprintImpactData[$footprint->id] = $footprint->getAllImpactData(); + $this->projects_by_footprint[$footprint->id] = Project::getByFootprint([ + 'footprints' => $footprint->id, + 'rand' => true, + 'amount_bigger_than' => 7000 + ]); + $this->sdg_by_footprint[$footprint->id] = Sdg::getList(['footprint' => $footprint->id]); + } + } + + /** + * @return Sponsor[] + */ + private function getSponsors(): array + { + $sponsors = []; + + foreach(Sponsor::getTypes() as $type) { + $sponsors[$type] = Sponsor::getList(['type' => $type]); + } + + return $sponsors; + } +} diff --git a/src/Goteo/UseCase/Home/HomeUseCaseResponse.php b/src/Goteo/UseCase/Home/HomeUseCaseResponse.php new file mode 100644 index 0000000000..a271bac209 --- /dev/null +++ b/src/Goteo/UseCase/Home/HomeUseCaseResponse.php @@ -0,0 +1,169 @@ +projects = $projects; + $this->totalProjects = $totalProjects; + $this->stories = $stories; + $this->channels = $channels; + $this->banners = $banners; + $this->stats = $stats; + $this->footprints = $footprints; + $this->homeItems = $homeItems; + $this->sponsors = $sponsors; + $this->projectsByFootprint = $projectsByFootprint; + $this->sdgByFootprint = $sdgByFootprint; + $this->footprintImpactData = $footprintImpactData; + } + + public function getLimit(): int + { + return self::LIMIT; + } + + /** + * @return Project[] + */ + public function getProjects(): array + { + return $this->projects; + } + + public function getTotalProjects(): int + { + return $this->totalProjects; + } + + /** + * @return Stories[] + */ + public function getStories(): array + { + return $this->stories; + } + + /** + * @return Node[] + */ + public function getChannels(): array + { + return $this->channels; + } + + /** + * @return Banner[] + */ + public function getBanners(): array + { + return $this->banners; + } + + public function getStats(): Stats + { + return $this->stats; + } + + /** + * @return Footprint[] + */ + public function getFootprints(): array + { + return $this->footprints; + } + + /** + * @return Home[] + */ + public function getHomeItems(): array + { + return $this->homeItems; + } + + /** + * @return Sponsor[] + */ + public function getSponsors(): array + { + return $this->sponsors; + } + + /** + * @return ImpactData[] + */ + public function getFootprintImpactData(): array + { + return $this->footprintImpactData; + } + + /** + * @return Project[] + */ + public function getProjectsByFootprint(): array + { + return $this->projectsByFootprint; + } + + /** + * @return Project[] + */ + public function getSdgByFootprint(): array + { + return $this->sdgByFootprint; + } +}