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 | |
| 8 | .. contents:: Page Contents |
| 9 | |
| 10 | Loading this Helper |
| 11 | =================== |
| 12 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 13 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 14 | |
| 15 | $this->load->helper('smiley'); |
| 16 | |
| 17 | Overview |
| 18 | ======== |
| 19 | |
| 20 | The Smiley helper has a renderer that takes plain text simileys, like |
| 21 | :-) and turns them into a image representation, like |smile!| |
| 22 | |
| 23 | It also lets you display a set of smiley images that when clicked will |
| 24 | be inserted into a form field. For example, if you have a blog that |
| 25 | allows user commenting you can show the smileys next to the comment |
| 26 | form. Your users can click a desired smiley and with the help of some |
| 27 | JavaScript it will be placed into the form field. |
| 28 | |
| 29 | Clickable Smileys Tutorial |
| 30 | ========================== |
| 31 | |
| 32 | Here is an example demonstrating how you might create a set of clickable |
| 33 | smileys next to a form field. This example requires that you first |
| 34 | download and install the smiley images, then create a controller and the |
| 35 | View as described. |
| 36 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 37 | .. important:: Before you begin, please `download the smiley images |
| 38 | <http://codeigniter.com/download_files/smileys.zip>`_ |
| 39 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
| 43 | The Controller |
| 44 | -------------- |
| 45 | |
| 46 | In your `application/controllers/` folder, create a file called |
| 47 | smileys.php and place the code below in it. |
| 48 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 49 | .. important:: Change the URL in the :php:func:`get_clickable_smileys()` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | function below so that it points to your smiley folder. |
| 51 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 52 | You'll notice that in addition to the smiley helper, we are also using |
| 53 | the :doc:`Table Class <../libraries/table>`:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
| 55 | <?php |
| 56 | |
| 57 | class Smileys extends CI_Controller { |
| 58 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 59 | public function index() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | { |
| 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 Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 70 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | In your `application/views/` folder, create a file called `smiley_view.php` |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 74 | and place this code in it:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
| 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 | |
| 91 | Field Aliases |
| 92 | ------------- |
| 93 | |
| 94 | When making changes to a view it can be inconvenient to have the field |
| 95 | id in the controller. To work around this, you can give your smiley |
| 96 | links 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 Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 102 | To map the alias to the field id, pass them both into the |
| 103 | :php:func:`smiley_js()` function:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
| 105 | $image_array = smiley_js("comment_textarea_alias", "comments"); |
| 106 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | get_clickable_smileys() |
| 108 | ======================= |
| 109 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 110 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | Returns an array containing your smiley images wrapped in a clickable |
| 117 | link. You must supply the URL to your smiley folder and a field id or |
| 118 | field alias. |
| 119 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 120 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
| 122 | $image_array = get_smiley_links("http://example.com/images/smileys/", "comment"); |
| 123 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | smiley_js() |
| 125 | =========== |
| 126 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 127 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | Generates the JavaScript that allows the images to be clicked and |
| 134 | inserted into a form field. If you supplied an alias instead of an id |
| 135 | when generating your smiley links, you need to pass the alias and |
| 136 | corresponding form id into the function. This function is designed to be |
| 137 | placed into the <head> area of your web page. |
| 138 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 139 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 140 | |
| 141 | <?php echo smiley_js(); ?> |
| 142 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | parse_smileys() |
| 144 | =============== |
| 145 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 146 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 153 | Takes a string of text as input and replaces any contained plain text |
| 154 | smileys into the image equivalent. The first parameter must contain your |
| 155 | string, the second must contain the URL to your smiley folder |
| 156 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 157 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 158 | |
| 159 | $str = 'Here are some simileys: :-) ;-)'; |
| 160 | $str = parse_smileys($str, "http://example.com/images/smileys/"); |
| 161 | echo $str; |
| 162 | |
| 163 | |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 164 | .. |smile!| image:: ../images/smile.gif |