-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b665d3
commit 9dfab49
Showing
18 changed files
with
1,500 additions
and
326 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
plugins/Sandbox/src/Controller/FavoriteExamplesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Sandbox\Controller; | ||
|
||
use Cake\Core\Configure; | ||
use Cake\Event\EventInterface; | ||
use Sandbox\Model\Entity\SandboxUser; | ||
|
||
class FavoriteExamplesController extends SandboxAppController { | ||
|
||
protected ?string $defaultTable = 'Sandbox.SandboxPosts'; | ||
|
||
/** | ||
* @throws \Exception | ||
* @return void | ||
*/ | ||
public function initialize(): void { | ||
parent::initialize(); | ||
|
||
$this->fetchTable('Sandbox.SandboxPosts')->ensureDemoData(); | ||
|
||
$user = $this->user(); | ||
// For demo purposes we use Configure instead of session | ||
Configure::write('Auth.User.id', $user->id); | ||
|
||
// You usually only need one of them per entity type | ||
$this->loadComponent('Favorites.Starable', ['actions' => ['star']]); | ||
$this->loadComponent('Favorites.Likeable', ['actions' => ['like']]); | ||
$this->loadComponent('Favorites.Favoriteable', ['actions' => ['favorite']]); | ||
} | ||
|
||
/** | ||
* | ||
* @param \Cake\Event\EventInterface $event | ||
* | ||
* @return void | ||
*/ | ||
public function beforeFilter(EventInterface $event) { | ||
parent::beforeFilter($event); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function index() { | ||
$user = $this->user(); | ||
|
||
$this->set(compact('user')); | ||
} | ||
|
||
/** | ||
* @return \Cake\Http\Response|null|void | ||
*/ | ||
public function star() { | ||
$user = $this->user(); | ||
$posts = $this->fetchTable('Sandbox.SandboxPosts')->find() | ||
->contain(['Starred']) | ||
->all() | ||
->toArray(); | ||
|
||
$this->set(compact('user', 'posts')); | ||
} | ||
|
||
/** | ||
* @return \Cake\Http\Response|null|void | ||
*/ | ||
public function like() { | ||
$user = $this->user(); | ||
$posts = $this->fetchTable('Sandbox.SandboxPosts')->find() | ||
->contain(['Liked']) | ||
->all() | ||
->toArray(); | ||
|
||
$this->set(compact('user', 'posts')); | ||
} | ||
|
||
/** | ||
* @return \Cake\Http\Response|null|void | ||
*/ | ||
public function favorite() { | ||
$user = $this->user(); | ||
$posts = $this->fetchTable('Sandbox.SandboxPosts')->find() | ||
->contain(['Favorite']) | ||
->all() | ||
->toArray(); | ||
|
||
$this->set(compact('user', 'posts')); | ||
} | ||
|
||
/** | ||
* @return \Sandbox\Model\Entity\SandboxUser | ||
*/ | ||
protected function user(): SandboxUser { | ||
// For the demo we bind it to the user session to avoid other people testing it to have side-effects :) | ||
$sid = $this->request->getSession()->id() ?: env('REMOTE_ADDR'); | ||
$user = $this->fetchTable('Sandbox.SandboxUsers')->find() | ||
->where(['email' => $sid . '@example.de']) | ||
->first(); | ||
if (!$user) { | ||
$user = $this->fetchTable('Sandbox.SandboxUsers')->newEntity([ | ||
'username' => 'DemoUser', | ||
'slug' => 'demo-user', | ||
'email' => $sid . '@example.de', | ||
'password' => '', | ||
]); | ||
$this->fetchTable('Sandbox.SandboxUsers')->saveOrFail($user); | ||
} | ||
|
||
return $user; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* @var \App\View\AppView $this | ||
* @var \Sandbox\Model\Entity\SandboxPost[] $posts | ||
*/ | ||
?> | ||
|
||
<nav class="actions col-sm-4 col-12"> | ||
<?php echo $this->element('navigation/favorite'); ?> | ||
</nav> | ||
<div class="page index col-sm-8 col-12"> | ||
|
||
<h3>Likes</h3> | ||
<p>You can allow adding custom reactions to any record with ease.</p> | ||
<ul> | ||
<li>Add the FavoriteableComponent to your controller</li> | ||
<li>Add the post link icons in the frontend (e.g. using the FavoritesHelper)</li> | ||
</ul> | ||
|
||
<h3>Demo</h3> | ||
|
||
<?php | ||
foreach ($posts as $post) { | ||
?> | ||
<h4><?php echo h($post->title); ?></h4> | ||
<p><?php echo $this->Favorites->widget('FavoritePosts', $post->id, $post->favorite ? $post->favorite->value : null);?></p> | ||
<p><?php echo h($post->content) ?></p> | ||
<?php | ||
} | ||
?> | ||
|
||
|
||
<hr> | ||
|
||
<p>Note:</p> | ||
<ul> | ||
<li>The examples use the native and unobstrusive UTF8 characters/emoji, but you can also use FontAwesome icons or custom HTML snippets</li> | ||
<li>The example above is not using AJAX or JS, usually it would be advised to create a more sophisticated and user-friendly snippet here</li> | ||
</ul> | ||
|
||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* @var \App\View\AppView $this | ||
* @var \Sandbox\Model\Entity\SandboxUser $user | ||
*/ | ||
|
||
$this->loadHelper('Queue.QueueProgress'); | ||
?> | ||
|
||
<nav class="actions col-sm-4 col-12"> | ||
<?php echo $this->element('navigation/favorite'); ?> | ||
</nav> | ||
<div class="page index col-sm-8 col-12"> | ||
|
||
<h3>Basic Usage</h3> | ||
<p>The most common use cases are usually</p> | ||
<ul> | ||
<li>Star (yes/no)</li> | ||
<li>Like (up/down/none)</li> | ||
<li>Favorite (some emoji or icon as reaction)</li> | ||
</ul> | ||
|
||
There are many implementations in modern applications: | ||
<ul> | ||
<li>Starred a.k.a GitHub stars (and remove star)</li> | ||
<li>GitHub Reactions</li> | ||
<li>Facebook Reactions</li> | ||
<li>YouTube Likes</li> | ||
<li>Slack Reactions</li> | ||
<li>Reddit Votes</li> | ||
<li>Medium Claps</li> | ||
</ul> | ||
|
||
<h4>Give it a try</h4> | ||
<p> | ||
Hello <b><?php echo h($user->username); ?></b>, | ||
<br>the following examples will be using this account to "favorite" things. | ||
</p> | ||
|
||
|
||
</div> |
Oops, something went wrong.