blob: 941ba11e3c9adee0521f31bda1a9e910fe0dc417 [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
13This helper is loaded using the following code
14
15::
16
17 $this->load->helper('smiley');
18
19Overview
20========
21
22The Smiley helper has a renderer that takes plain text simileys, like
23:-) and turns them into a image representation, like |smile!|
24
25It also lets you display a set of smiley images that when clicked will
26be inserted into a form field. For example, if you have a blog that
27allows user commenting you can show the smileys next to the comment
28form. Your users can click a desired smiley and with the help of some
29JavaScript it will be placed into the form field.
30
31Clickable Smileys Tutorial
32==========================
33
34Here is an example demonstrating how you might create a set of clickable
35smileys next to a form field. This example requires that you first
36download and install the smiley images, then create a controller and the
37View as described.
38
39.. important:: Before you begin, please `download the smiley images <http://codeigniter.com/download_files/smileys.zip>`_
40 and put them in a publicly accessible place on your server. This helper
41 also assumes you have the smiley replacement array located at
42 `application/config/smileys.php`
43
44The Controller
45--------------
46
47In your `application/controllers/` folder, create a file called
48smileys.php and place the code below in it.
49
50.. important:: Change the URL in the `get_clickable_smileys()`
51 function below so that it points to your smiley folder.
52
53You'll notice that in addition to the smiley helper we are using the :doc:`Table Class <../libraries/table>`.
54
55::
56
57 <?php
58
59 class Smileys extends CI_Controller {
60
61 function index()
62 {
63 $this->load->helper('smiley');
64 $this->load->library('table');
65
66 $image_array = get_clickable_smileys('http://example.com/images/smileys/', 'comments');
67 $col_array = $this->table->make_columns($image_array, 8);
68
69 $data['smiley_table'] = $this->table->generate($col_array);
70 $this->load->view('smiley_view', $data);
71 }
72 }
73
74In your `application/views/` folder, create a file called `smiley_view.php`
75and place this code in it:
76
77::
78
79 <html>
80 <head>
81 <title>Smileys</title>
82 <?php echo smiley_js(); ?>
83 </head>
84 <body>
85 <form name="blog">
86 <textarea name="comments" id="comments" cols="40" rows="4"></textarea>
87 </form>
88 <p>Click to insert a smiley!</p>
89 <?php echo $smiley_table; ?> </body> </html>
90 When you have created the above controller and view, load it by visiting http://www.example.com/index.php/smileys/
91 </body>
92 </html>
93
94Field Aliases
95-------------
96
97When making changes to a view it can be inconvenient to have the field
98id in the controller. To work around this, you can give your smiley
99links a generic name that will be tied to a specific id in your view.
100
101::
102
103 $image_array = get_smiley_links("http://example.com/images/smileys/", "comment_textarea_alias");
104
105To map the alias to the field id, pass them both into the `smiley_js`
106function
107
108::
109
110 $image_array = smiley_js("comment_textarea_alias", "comments");
111
112******************
113Function Reference
114******************
115
116get_clickable_smileys()
117=======================
118
119Returns an array containing your smiley images wrapped in a clickable
120link. You must supply the URL to your smiley folder and a field id or
121field alias.
122
123::
124
125 $image_array = get_smiley_links("http://example.com/images/smileys/", "comment");
126
127Note: Usage of this function without the second parameter, in
128combination with `js_insert_smiley` has been deprecated.
129
130smiley_js()
131===========
132
133Generates 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
139::
140
141 <?php echo smiley_js(); ?>
142
143Note: This function replaces `js_insert_smiley`, which has been
144deprecated.
145
146parse_smileys()
147===============
148
149Takes a string of text as input and replaces any contained plain text
150smileys into the image equivalent. The first parameter must contain your
151string, the second must contain the URL to your smiley folder
152
153::
154
155 $str = 'Here are some simileys: :-) ;-)';
156 $str = parse_smileys($str, "http://example.com/images/smileys/");
157 echo $str;
158
159
160.. |smile!| image:: ../images/smile.gif