1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: use Xoops\Core\Service\AbstractContract;
13: use Xoops\Core\Service\Contract\EmojiInterface;
14: use Xoops\Core\Service\Response;
15:
16: /**
17: * Smilies provider for service manager
18: *
19: * @category SmiliesProvider
20: * @package Smilies
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2015 XOOPS Project (http://xoops.org)
23: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24: * @link http://xoops.org
25: */
26: class SmiliesProvider extends AbstractContract implements EmojiInterface
27: {
28: /**
29: * getName - get a short name for this service provider. This should be unique within the
30: * scope of the named service, so using module dirname is suggested.
31: *
32: * @return string - a unique name for the service provider
33: */
34: public function getName()
35: {
36: return 'smilies';
37: }
38:
39: /**
40: * getDescription - get human readable description of the service provider
41: *
42: * @return string
43: */
44: public function getDescription()
45: {
46: return 'Traditional XOOPS Smilies';
47: }
48:
49: /**
50: * renderEmoji - given a string of source text being built for display, perform any processing of Emoji
51: * references required to display the intended imagery.
52: *
53: * @param Response $response \Xoops\Core\Service\Response object
54: * @param string $buffer source text to be processed
55: *
56: * @return void - $response->value set to processed buffer
57: */
58: public function renderEmoji(Response $response, $buffer)
59: {
60: $emojiList = $this->getSmileyList();
61: $emojiName = array_column($emojiList, 'name');
62: $emojiRendered = array_column($emojiList, 'rendered');
63:
64: $processedBuffer = str_replace($emojiName, $emojiRendered, $buffer);
65:
66: $response->setValue($processedBuffer);
67: }
68:
69: /**
70: * getEmojiList - return a list of available emoji
71: *
72: * @param Response $response \Xoops\Core\Service\Response object
73: *
74: * @return void - $response->value set to array of emoji information
75: * 'name' => (string) code that represents the emoji, i.e. ":wink:"
76: * 'description' => (string) description
77: * 'rendered' => (string) valid HTML to display a rendering of the emoji
78: */
79: public function getEmojiList(Response $response)
80: {
81: $response->setValue($this->getSmileyList());
82: }
83:
84: /**
85: * renderEmojiSelector - provide emoji selector support for editing
86: *
87: * This should return an HTML string that, when displayed, will provide a link to an emoji selector.
88: * Additionally, this should perform any additional tasks required to make the link function, such
89: * as adding script or stylesheet assets to the active theme.
90: *
91: * @param Response $response \Xoops\Core\Service\Response object
92: * @param string $identifier element identifier to receive emoji from selector
93: *
94: * @return void - $response->value (string) HTML code to launch the emoji selector, i.e. button
95: */
96: public function renderEmojiSelector(Response $response, $identifier)
97: {
98: $selector = '<img src="' . \XoopsBaseConfig::get('url') . '/images/smiley.gif" alt="'
99: . \XoopsLocale::SMILIES . '" title="' . \XoopsLocale::SMILIES . '" onclick=\'openWithSelfMain("'
100: . \XoopsBaseConfig::get('url') . '/modules/smilies/popup.php?target=' . $identifier
101: . '","smilies",300,650);\' onmouseover=\'style.cursor="hand"\'/> ';
102:
103: $response->setValue($selector);
104: }
105:
106: /**
107: * get list of smilies in emoji format
108: *
109: * @return array emoji list
110: */
111: private function getSmileyList()
112: {
113: static $emojiList = null;
114:
115: if ($emojiList === null) {
116: $smiliesArray = \Xoops::getInstance()->getModuleHandler('smiley', 'smilies')->getActiveSmilies(false);
117: $emojiList = [];
118: foreach ($smiliesArray as $smile) {
119: $emoji['name'] = $smile['smiley_code'];
120: $emoji['description'] = $smile['smiley_emotion'];
121: $emoji['rendered'] =
122: '<img src="' . $smile['smiley_url'] . '" alt="' . $smile['smiley_emotion'] . '" />';
123: $emojiList[] = $emoji;
124: }
125: }
126: return $emojiList;
127: }
128: }
129: