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 | |
| 13 | This helper is loaded using the following code |
| 14 | |
| 15 | :: |
| 16 | |
| 17 | $this->load->helper('smiley'); |
| 18 | |
| 19 | Overview |
| 20 | ======== |
| 21 | |
| 22 | The Smiley helper has a renderer that takes plain text simileys, like |
| 23 | :-) and turns them into a image representation, like |smile!| |
| 24 | |
| 25 | It also lets you display a set of smiley images that when clicked will |
| 26 | be inserted into a form field. For example, if you have a blog that |
| 27 | allows user commenting you can show the smileys next to the comment |
| 28 | form. Your users can click a desired smiley and with the help of some |
| 29 | JavaScript it will be placed into the form field. |
| 30 | |
| 31 | Clickable Smileys Tutorial |
| 32 | ========================== |
| 33 | |
| 34 | Here is an example demonstrating how you might create a set of clickable |
| 35 | smileys next to a form field. This example requires that you first |
| 36 | download and install the smiley images, then create a controller and the |
| 37 | View 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 | |
| 44 | The Controller |
| 45 | -------------- |
| 46 | |
| 47 | In your `application/controllers/` folder, create a file called |
| 48 | smileys.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 | |
| 53 | You'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 | |
| 74 | In your `application/views/` folder, create a file called `smiley_view.php` |
| 75 | and 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 | |
| 94 | Field Aliases |
| 95 | ------------- |
| 96 | |
| 97 | When making changes to a view it can be inconvenient to have the field |
| 98 | id in the controller. To work around this, you can give your smiley |
| 99 | links 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 | |
| 105 | To map the alias to the field id, pass them both into the `smiley_js` |
| 106 | function |
| 107 | |
| 108 | :: |
| 109 | |
| 110 | $image_array = smiley_js("comment_textarea_alias", "comments"); |
| 111 | |
| 112 | ****************** |
| 113 | Function Reference |
| 114 | ****************** |
| 115 | |
| 116 | get_clickable_smileys() |
| 117 | ======================= |
| 118 | |
| 119 | Returns an array containing your smiley images wrapped in a clickable |
| 120 | link. You must supply the URL to your smiley folder and a field id or |
| 121 | field alias. |
| 122 | |
| 123 | :: |
| 124 | |
| 125 | $image_array = get_smiley_links("http://example.com/images/smileys/", "comment"); |
| 126 | |
| 127 | Note: Usage of this function without the second parameter, in |
| 128 | combination with `js_insert_smiley` has been deprecated. |
| 129 | |
| 130 | smiley_js() |
| 131 | =========== |
| 132 | |
| 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 | |
| 139 | :: |
| 140 | |
| 141 | <?php echo smiley_js(); ?> |
| 142 | |
| 143 | Note: This function replaces `js_insert_smiley`, which has been |
| 144 | deprecated. |
| 145 | |
| 146 | parse_smileys() |
| 147 | =============== |
| 148 | |
| 149 | Takes a string of text as input and replaces any contained plain text |
| 150 | smileys into the image equivalent. The first parameter must contain your |
| 151 | string, 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 |