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 | |
Andrey Andreev | 21c3c22 | 2014-12-04 12:10:00 +0200 | [diff] [blame] | 8 | .. important:: The Smiley helper is DEPRECATED and should not be used. |
| 9 | It is currently only kept for backwards compatibility. |
| 10 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 11 | .. contents:: |
| 12 | :local: |
| 13 | |
| 14 | .. raw:: html |
| 15 | |
| 16 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | |
| 18 | Loading this Helper |
| 19 | =================== |
| 20 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 21 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | |
| 23 | $this->load->helper('smiley'); |
| 24 | |
| 25 | Overview |
| 26 | ======== |
| 27 | |
vlakoff | 2efd1f2 | 2013-08-06 17:31:48 +0200 | [diff] [blame] | 28 | The Smiley helper has a renderer that takes plain text smileys, like |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | :-) and turns them into a image representation, like |smile!| |
| 30 | |
| 31 | It also lets you display a set of smiley images that when clicked will |
| 32 | be inserted into a form field. For example, if you have a blog that |
| 33 | allows user commenting you can show the smileys next to the comment |
| 34 | form. Your users can click a desired smiley and with the help of some |
| 35 | JavaScript it will be placed into the form field. |
| 36 | |
| 37 | Clickable Smileys Tutorial |
| 38 | ========================== |
| 39 | |
| 40 | Here is an example demonstrating how you might create a set of clickable |
| 41 | smileys next to a form field. This example requires that you first |
| 42 | download and install the smiley images, then create a controller and the |
| 43 | View as described. |
| 44 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 45 | .. important:: Before you begin, please `download the smiley images |
Steven Crothers | d200123 | 2013-07-03 02:18:00 -0700 | [diff] [blame] | 46 | <http://ellislab.com/asset/ci_download_files/smileys.zip>`_ |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 47 | and put them in a publicly accessible place on your server. |
| 48 | This helper also assumes you have the smiley replacement array |
| 49 | located at `application/config/smileys.php` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | |
| 51 | The Controller |
| 52 | -------------- |
| 53 | |
vlakoff | 1c75780 | 2013-08-06 20:25:14 +0200 | [diff] [blame] | 54 | In your **application/controllers/** directory, create a file called |
vlakoff | 2efd1f2 | 2013-08-06 17:31:48 +0200 | [diff] [blame] | 55 | Smileys.php and place the code below in it. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 57 | .. important:: Change the URL in the :php:func:`get_clickable_smileys()` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | function below so that it points to your smiley folder. |
| 59 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 60 | You'll notice that in addition to the smiley helper, we are also using |
| 61 | the :doc:`Table Class <../libraries/table>`:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
| 63 | <?php |
| 64 | |
| 65 | class Smileys extends CI_Controller { |
| 66 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 67 | public function index() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | { |
| 69 | $this->load->helper('smiley'); |
| 70 | $this->load->library('table'); |
| 71 | |
| 72 | $image_array = get_clickable_smileys('http://example.com/images/smileys/', 'comments'); |
| 73 | $col_array = $this->table->make_columns($image_array, 8); |
| 74 | |
| 75 | $data['smiley_table'] = $this->table->generate($col_array); |
| 76 | $this->load->view('smiley_view', $data); |
| 77 | } |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 78 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | } |
| 80 | |
vlakoff | 787fe13 | 2014-04-14 14:45:36 +0200 | [diff] [blame] | 81 | In your **application/views/** directory, create a file called **smiley_view.php** |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 82 | and place this code in it:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
| 84 | <html> |
| 85 | <head> |
| 86 | <title>Smileys</title> |
| 87 | <?php echo smiley_js(); ?> |
| 88 | </head> |
| 89 | <body> |
| 90 | <form name="blog"> |
| 91 | <textarea name="comments" id="comments" cols="40" rows="4"></textarea> |
| 92 | </form> |
| 93 | <p>Click to insert a smiley!</p> |
| 94 | <?php echo $smiley_table; ?> </body> </html> |
| 95 | When you have created the above controller and view, load it by visiting http://www.example.com/index.php/smileys/ |
| 96 | </body> |
| 97 | </html> |
| 98 | |
| 99 | Field Aliases |
| 100 | ------------- |
| 101 | |
| 102 | When making changes to a view it can be inconvenient to have the field |
| 103 | id in the controller. To work around this, you can give your smiley |
| 104 | links a generic name that will be tied to a specific id in your view. |
| 105 | |
| 106 | :: |
| 107 | |
| 108 | $image_array = get_smiley_links("http://example.com/images/smileys/", "comment_textarea_alias"); |
| 109 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 110 | 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] | 111 | :func:`smiley_js()` function:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
| 113 | $image_array = smiley_js("comment_textarea_alias", "comments"); |
| 114 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 115 | Available Functions |
| 116 | =================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 118 | .. php:function:: get_clickable_smileys($image_url[, $alias = ''[, $smileys = NULL]]) |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 119 | |
| 120 | :param string $image_url: URL path to the smileys directory |
| 121 | :param string $alias: Field alias |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 122 | :returns: An array of ready to use smileys |
| 123 | :rtype: array |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 124 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 125 | Returns an array containing your smiley images wrapped in a clickable |
| 126 | link. You must supply the URL to your smiley folder and a field id or |
| 127 | field alias. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 129 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 131 | $image_array = get_clickable_smileys('http://example.com/images/smileys/', 'comment'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 133 | .. php:function:: smiley_js([$alias = ''[, $field_id = ''[, $inline = TRUE]]]) |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 134 | |
| 135 | :param string $alias: Field alias |
| 136 | :param string $field_id: Field ID |
| 137 | :param bool $inline: Whether we're inserting an inline smiley |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 138 | :returns: Smiley-enabling JavaScript code |
| 139 | :rtype: string |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 140 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 141 | Generates the JavaScript that allows the images to be clicked and |
| 142 | inserted into a form field. If you supplied an alias instead of an id |
| 143 | when generating your smiley links, you need to pass the alias and |
| 144 | corresponding form id into the function. This function is designed to be |
| 145 | placed into the <head> area of your web page. |
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 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 149 | <?php echo smiley_js(); ?> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 151 | .. php:function:: parse_smileys([$str = ''[, $image_url = ''[, $smileys = NULL]]]) |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 152 | |
| 153 | :param string $str: Text containing smiley codes |
| 154 | :param string $image_url: URL path to the smileys directory |
| 155 | :param array $smileys: An array of smileys |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 156 | :returns: Parsed smileys |
| 157 | :rtype: string |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 158 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 159 | Takes a string of text as input and replaces any contained plain text |
| 160 | smileys into the image equivalent. The first parameter must contain your |
| 161 | string, the second must contain the URL to your smiley folder |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 163 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
vlakoff | 2efd1f2 | 2013-08-06 17:31:48 +0200 | [diff] [blame] | 165 | $str = 'Here are some smileys: :-) ;-)'; |
Andrey Andreev | ea801ab | 2014-01-20 15:03:43 +0200 | [diff] [blame] | 166 | $str = parse_smileys($str, 'http://example.com/images/smileys/'); |
Derek Jones | f33091f | 2013-07-19 16:44:19 -0700 | [diff] [blame] | 167 | echo $str; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 169 | .. |smile!| image:: ../images/smile.gif |