Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############# |
| 2 | Smiley Helper |
| 3 | ############# |
| 4 | |
| 5 | The Smiley Helper file contains functions that let you manage smileys |
| 6 | (emoticons). |
| 7 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 8 | .. contents:: |
| 9 | :local: |
| 10 | |
| 11 | .. raw:: html |
| 12 | |
| 13 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | |
| 15 | Loading this Helper |
| 16 | =================== |
| 17 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 18 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 19 | |
| 20 | $this->load->helper('smiley'); |
| 21 | |
| 22 | Overview |
| 23 | ======== |
| 24 | |
vlakoff | 2efd1f2 | 2013-08-06 17:31:48 +0200 | [diff] [blame] | 25 | The Smiley helper has a renderer that takes plain text smileys, like |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | :-) and turns them into a image representation, like |smile!| |
| 27 | |
| 28 | It also lets you display a set of smiley images that when clicked will |
| 29 | be inserted into a form field. For example, if you have a blog that |
| 30 | allows user commenting you can show the smileys next to the comment |
| 31 | form. Your users can click a desired smiley and with the help of some |
| 32 | JavaScript it will be placed into the form field. |
| 33 | |
| 34 | Clickable Smileys Tutorial |
| 35 | ========================== |
| 36 | |
| 37 | Here is an example demonstrating how you might create a set of clickable |
| 38 | smileys next to a form field. This example requires that you first |
| 39 | download and install the smiley images, then create a controller and the |
| 40 | View as described. |
| 41 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 42 | .. important:: Before you begin, please `download the smiley images |
Steven Crothers | d200123 | 2013-07-03 02:18:00 -0700 | [diff] [blame] | 43 | <http://ellislab.com/asset/ci_download_files/smileys.zip>`_ |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 44 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
| 48 | The Controller |
| 49 | -------------- |
| 50 | |
vlakoff | 1c75780 | 2013-08-06 20:25:14 +0200 | [diff] [blame^] | 51 | In your **application/controllers/** directory, create a file called |
vlakoff | 2efd1f2 | 2013-08-06 17:31:48 +0200 | [diff] [blame] | 52 | Smileys.php and place the code below in it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
Derek Jones | 123bb20 | 2013-07-19 16:37:51 -0700 | [diff] [blame] | 54 | .. important:: Change the URL in the :func:`get_clickable_smileys()` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | function below so that it points to your smiley folder. |
| 56 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 57 | You'll notice that in addition to the smiley helper, we are also using |
| 58 | the :doc:`Table Class <../libraries/table>`:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | |
| 60 | <?php |
| 61 | |
| 62 | class Smileys extends CI_Controller { |
| 63 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 64 | public function index() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | { |
| 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 Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 75 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | } |
| 77 | |
vlakoff | 1c75780 | 2013-08-06 20:25:14 +0200 | [diff] [blame^] | 78 | In your **application/views/** folder, create a file called **smiley_view.php** |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 79 | and place this code in it:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
| 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 | |
| 96 | Field Aliases |
| 97 | ------------- |
| 98 | |
| 99 | When making changes to a view it can be inconvenient to have the field |
| 100 | id in the controller. To work around this, you can give your smiley |
| 101 | links 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 Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 107 | To map the alias to the field id, pass them both into the |
Derek Jones | 123bb20 | 2013-07-19 16:37:51 -0700 | [diff] [blame] | 108 | :func:`smiley_js()` function:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
| 110 | $image_array = smiley_js("comment_textarea_alias", "comments"); |
| 111 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 112 | Available Functions |
| 113 | =================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 115 | .. function:: get_clickable_smileys($image_url[, $alias = ''[, $smileys = NULL]]) |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 116 | |
| 117 | :param string $image_url: URL path to the smileys directory |
| 118 | :param string $alias: Field alias |
| 119 | :returns: array |
| 120 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 121 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 125 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 127 | $image_array = get_smiley_links("http://example.com/images/smileys/", "comment"); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 130 | .. function:: smiley_js([$alias = ''[, $field_id = ''[, $inline = TRUE]]]) |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 131 | |
| 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 Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 136 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 142 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 144 | <?php echo smiley_js(); ?> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 147 | .. function:: parse_smileys([$str = ''[, $image_url = ''[, $smileys = NULL]]]) |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 148 | |
| 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 Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 154 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 157 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 158 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | |
vlakoff | 2efd1f2 | 2013-08-06 17:31:48 +0200 | [diff] [blame] | 160 | $str = 'Here are some smileys: :-) ;-)'; |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 161 | $str = parse_smileys($str, "http://example.com/images/smileys/"); |
| 162 | echo $str; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
| 164 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 165 | .. |smile!| image:: ../images/smile.gif |