blob: 7069b465e2898bd7ca2aa305ca5799d7c1fd6bf9 [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
8.. contents:: Page Contents
9
10Loading this Helper
11===================
12
Andrey Andreev53b8ef52012-11-08 21:38:53 +020013This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15 $this->load->helper('smiley');
16
17Overview
18========
19
20The Smiley helper has a renderer that takes plain text simileys, like
21:-) and turns them into a image representation, like |smile!|
22
23It also lets you display a set of smiley images that when clicked will
24be inserted into a form field. For example, if you have a blog that
25allows user commenting you can show the smileys next to the comment
26form. Your users can click a desired smiley and with the help of some
27JavaScript it will be placed into the form field.
28
29Clickable Smileys Tutorial
30==========================
31
32Here is an example demonstrating how you might create a set of clickable
33smileys next to a form field. This example requires that you first
34download and install the smiley images, then create a controller and the
35View as described.
36
Andrey Andreev53b8ef52012-11-08 21:38:53 +020037.. important:: Before you begin, please `download the smiley images
Steven Crothersd2001232013-07-03 02:18:00 -070038 <http://ellislab.com/asset/ci_download_files/smileys.zip>`_
Andrey Andreev53b8ef52012-11-08 21:38:53 +020039 and put them in a publicly accessible place on your server.
40 This helper also assumes you have the smiley replacement array
41 located at `application/config/smileys.php`
Derek Jones8ede1a22011-10-05 13:34:52 -050042
43The Controller
44--------------
45
46In your `application/controllers/` folder, create a file called
47smileys.php and place the code below in it.
48
Andrey Andreev53b8ef52012-11-08 21:38:53 +020049.. important:: Change the URL in the :php:func:`get_clickable_smileys()`
Derek Jones8ede1a22011-10-05 13:34:52 -050050 function below so that it points to your smiley folder.
51
Andrey Andreev53b8ef52012-11-08 21:38:53 +020052You'll notice that in addition to the smiley helper, we are also using
53the :doc:`Table Class <../libraries/table>`::
Derek Jones8ede1a22011-10-05 13:34:52 -050054
55 <?php
56
57 class Smileys extends CI_Controller {
58
Andrey Andreev53b8ef52012-11-08 21:38:53 +020059 public function index()
Derek Jones8ede1a22011-10-05 13:34:52 -050060 {
61 $this->load->helper('smiley');
62 $this->load->library('table');
63
64 $image_array = get_clickable_smileys('http://example.com/images/smileys/', 'comments');
65 $col_array = $this->table->make_columns($image_array, 8);
66
67 $data['smiley_table'] = $this->table->generate($col_array);
68 $this->load->view('smiley_view', $data);
69 }
Andrey Andreev53b8ef52012-11-08 21:38:53 +020070
Derek Jones8ede1a22011-10-05 13:34:52 -050071 }
72
73In your `application/views/` folder, create a file called `smiley_view.php`
Andrey Andreev53b8ef52012-11-08 21:38:53 +020074and place this code in it::
Derek Jones8ede1a22011-10-05 13:34:52 -050075
76 <html>
77 <head>
78 <title>Smileys</title>
79 <?php echo smiley_js(); ?>
80 </head>
81 <body>
82 <form name="blog">
83 <textarea name="comments" id="comments" cols="40" rows="4"></textarea>
84 </form>
85 <p>Click to insert a smiley!</p>
86 <?php echo $smiley_table; ?> </body> </html>
87 When you have created the above controller and view, load it by visiting http://www.example.com/index.php/smileys/
88 </body>
89 </html>
90
91Field Aliases
92-------------
93
94When making changes to a view it can be inconvenient to have the field
95id in the controller. To work around this, you can give your smiley
96links a generic name that will be tied to a specific id in your view.
97
98::
99
100 $image_array = get_smiley_links("http://example.com/images/smileys/", "comment_textarea_alias");
101
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200102To map the alias to the field id, pass them both into the
103:php:func:`smiley_js()` function::
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
105 $image_array = smiley_js("comment_textarea_alias", "comments");
106
Derek Jones8ede1a22011-10-05 13:34:52 -0500107get_clickable_smileys()
108=======================
109
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200110.. php:function:: get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
111
112 :param string $image_url: URL path to the smileys directory
113 :param string $alias: Field alias
114 :returns: array
115
Derek Jones8ede1a22011-10-05 13:34:52 -0500116Returns an array containing your smiley images wrapped in a clickable
117link. You must supply the URL to your smiley folder and a field id or
118field alias.
119
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200120Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
122 $image_array = get_smiley_links("http://example.com/images/smileys/", "comment");
123
Derek Jones8ede1a22011-10-05 13:34:52 -0500124smiley_js()
125===========
126
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200127.. php:function:: smiley_js($alias = '', $field_id = '', $inline = TRUE)
128
129 :param string $alias: Field alias
130 :param string $field_id: Field ID
131 :param bool $inline: Whether we're inserting an inline smiley
132
Derek Jones8ede1a22011-10-05 13:34:52 -0500133Generates the JavaScript that allows the images to be clicked and
134inserted into a form field. If you supplied an alias instead of an id
135when generating your smiley links, you need to pass the alias and
136corresponding form id into the function. This function is designed to be
137placed into the <head> area of your web page.
138
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200139Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
141 <?php echo smiley_js(); ?>
142
Derek Jones8ede1a22011-10-05 13:34:52 -0500143parse_smileys()
144===============
145
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200146.. php:function:: parse_smileys($str = '', $image_url = '', $smileys = NULL)
147
148 :param string $str: Text containing smiley codes
149 :param string $image_url: URL path to the smileys directory
150 :param array $smileys: An array of smileys
151 :returns: string
152
Derek Jones8ede1a22011-10-05 13:34:52 -0500153Takes a string of text as input and replaces any contained plain text
154smileys into the image equivalent. The first parameter must contain your
155string, the second must contain the URL to your smiley folder
156
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200157Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
159 $str = 'Here are some simileys: :-) ;-)';
160 $str = parse_smileys($str, "http://example.com/images/smileys/");
161 echo $str;
162
163
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200164.. |smile!| image:: ../images/smile.gif