blob: 0601f02446e1b2717153d7132a6ffb1f5297caf9 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#############
2Smiley Helper
3#############
4
5The Smiley Helper file contains functions that let you manage smileys
6(emoticons).
7
Derek Jonesf33091f2013-07-19 16:44:19 -07008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15Loading this Helper
16===================
17
Andrey Andreev53b8ef52012-11-08 21:38:53 +020018This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050019
20 $this->load->helper('smiley');
21
22Overview
23========
24
vlakoff2efd1f22013-08-06 17:31:48 +020025The Smiley helper has a renderer that takes plain text smileys, like
Derek Jones8ede1a22011-10-05 13:34:52 -050026:-) and turns them into a image representation, like |smile!|
27
28It also lets you display a set of smiley images that when clicked will
29be inserted into a form field. For example, if you have a blog that
30allows user commenting you can show the smileys next to the comment
31form. Your users can click a desired smiley and with the help of some
32JavaScript it will be placed into the form field.
33
34Clickable Smileys Tutorial
35==========================
36
37Here is an example demonstrating how you might create a set of clickable
38smileys next to a form field. This example requires that you first
39download and install the smiley images, then create a controller and the
40View as described.
41
Andrey Andreev53b8ef52012-11-08 21:38:53 +020042.. important:: Before you begin, please `download the smiley images
Steven Crothersd2001232013-07-03 02:18:00 -070043 <http://ellislab.com/asset/ci_download_files/smileys.zip>`_
Andrey Andreev53b8ef52012-11-08 21:38:53 +020044 and put them in a publicly accessible place on your server.
45 This helper also assumes you have the smiley replacement array
46 located at `application/config/smileys.php`
Derek Jones8ede1a22011-10-05 13:34:52 -050047
48The Controller
49--------------
50
vlakoff1c757802013-08-06 20:25:14 +020051In your **application/controllers/** directory, create a file called
vlakoff2efd1f22013-08-06 17:31:48 +020052Smileys.php and place the code below in it.
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Derek Jones123bb202013-07-19 16:37:51 -070054.. important:: Change the URL in the :func:`get_clickable_smileys()`
Derek Jones8ede1a22011-10-05 13:34:52 -050055 function below so that it points to your smiley folder.
56
Andrey Andreev53b8ef52012-11-08 21:38:53 +020057You'll notice that in addition to the smiley helper, we are also using
58the :doc:`Table Class <../libraries/table>`::
Derek Jones8ede1a22011-10-05 13:34:52 -050059
60 <?php
61
62 class Smileys extends CI_Controller {
63
Andrey Andreev53b8ef52012-11-08 21:38:53 +020064 public function index()
Derek Jones8ede1a22011-10-05 13:34:52 -050065 {
66 $this->load->helper('smiley');
67 $this->load->library('table');
68
69 $image_array = get_clickable_smileys('http://example.com/images/smileys/', 'comments');
70 $col_array = $this->table->make_columns($image_array, 8);
71
72 $data['smiley_table'] = $this->table->generate($col_array);
73 $this->load->view('smiley_view', $data);
74 }
Andrey Andreev53b8ef52012-11-08 21:38:53 +020075
Derek Jones8ede1a22011-10-05 13:34:52 -050076 }
77
vlakoff1c757802013-08-06 20:25:14 +020078In your **application/views/** folder, create a file called **smiley_view.php**
Andrey Andreev53b8ef52012-11-08 21:38:53 +020079and place this code in it::
Derek Jones8ede1a22011-10-05 13:34:52 -050080
81 <html>
82 <head>
83 <title>Smileys</title>
84 <?php echo smiley_js(); ?>
85 </head>
86 <body>
87 <form name="blog">
88 <textarea name="comments" id="comments" cols="40" rows="4"></textarea>
89 </form>
90 <p>Click to insert a smiley!</p>
91 <?php echo $smiley_table; ?> </body> </html>
92 When you have created the above controller and view, load it by visiting http://www.example.com/index.php/smileys/
93 </body>
94 </html>
95
96Field Aliases
97-------------
98
99When making changes to a view it can be inconvenient to have the field
100id in the controller. To work around this, you can give your smiley
101links a generic name that will be tied to a specific id in your view.
102
103::
104
105 $image_array = get_smiley_links("http://example.com/images/smileys/", "comment_textarea_alias");
106
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200107To map the alias to the field id, pass them both into the
Derek Jones123bb202013-07-19 16:37:51 -0700108:func:`smiley_js()` function::
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
110 $image_array = smiley_js("comment_textarea_alias", "comments");
111
Derek Jonesf33091f2013-07-19 16:44:19 -0700112Available Functions
113===================
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Derek Jonesf33091f2013-07-19 16:44:19 -0700115.. function:: get_clickable_smileys($image_url[, $alias = ''[, $smileys = NULL]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200116
117 :param string $image_url: URL path to the smileys directory
118 :param string $alias: Field alias
119 :returns: array
120
Derek Jonesf33091f2013-07-19 16:44:19 -0700121 Returns an array containing your smiley images wrapped in a clickable
122 link. You must supply the URL to your smiley folder and a field id or
123 field alias.
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Derek Jonesf33091f2013-07-19 16:44:19 -0700125 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Derek Jonesf33091f2013-07-19 16:44:19 -0700127 $image_array = get_smiley_links("http://example.com/images/smileys/", "comment");
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Derek Jonesf33091f2013-07-19 16:44:19 -0700130.. function:: smiley_js([$alias = ''[, $field_id = ''[, $inline = TRUE]]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200131
132 :param string $alias: Field alias
133 :param string $field_id: Field ID
134 :param bool $inline: Whether we're inserting an inline smiley
135
Derek Jonesf33091f2013-07-19 16:44:19 -0700136 Generates the JavaScript that allows the images to be clicked and
137 inserted into a form field. If you supplied an alias instead of an id
138 when generating your smiley links, you need to pass the alias and
139 corresponding form id into the function. This function is designed to be
140 placed into the <head> area of your web page.
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Derek Jonesf33091f2013-07-19 16:44:19 -0700142 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Derek Jonesf33091f2013-07-19 16:44:19 -0700144 <?php echo smiley_js(); ?>
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Derek Jonesf33091f2013-07-19 16:44:19 -0700147.. function:: parse_smileys([$str = ''[, $image_url = ''[, $smileys = NULL]]])
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200148
149 :param string $str: Text containing smiley codes
150 :param string $image_url: URL path to the smileys directory
151 :param array $smileys: An array of smileys
152 :returns: string
153
Derek Jonesf33091f2013-07-19 16:44:19 -0700154 Takes a string of text as input and replaces any contained plain text
155 smileys into the image equivalent. The first parameter must contain your
156 string, the second must contain the URL to your smiley folder
Derek Jones8ede1a22011-10-05 13:34:52 -0500157
Derek Jonesf33091f2013-07-19 16:44:19 -0700158 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
vlakoff2efd1f22013-08-06 17:31:48 +0200160 $str = 'Here are some smileys: :-) ;-)';
Derek Jonesf33091f2013-07-19 16:44:19 -0700161 $str = parse_smileys($str, "http://example.com/images/smileys/");
162 echo $str;
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
164
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200165.. |smile!| image:: ../images/smile.gif