Archive for the ‘Helpers’ Category

INI Based Permission Component


Warning: fopen(/ini.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/ini.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/ini.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/ini.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Yet another permission component was created this week but this time, it’s simplier and written in a text file. In one of our project, one of the follow-up requirement was to allow users of certain groups upgrade into a better status. The project was already in production and I was using ACL with Auth component to do my authentication. Unfortunately, this is one of ACLs weakness.

Changing a users group does not change the ARO parent (effectively making it useless/impossible to change groups). I tried fixing this home-brew style, but since there’s no setParent in 1.2′s db_acl, it’s far harder than it should be.

That was reported back in 2007 and there was still no solution found in the net. It is also not an option for me to go through the above suggestion in fixing the problem so the the other solution is to find another one or create one. I chose the latter.

Why create a new one?

The other solutions that I found still needs some configuration in the controllers. Since I already have a ton of files, I don’t want to go through all of them to add a line or two. I need ACLs way of doing it all in the background. Also, I am already using Auth component and I want to keep it.

Why did I choose a text file over a database?

My intention is to make the application faster since load time is really getting slower. Since the application isn’t that big, I only have less than 200 lines for the permissions, so a database is just overkill. Even if I use a database, I might cache it anyway so it saves me extra query.

The component

So Permission Component was created. Below is the instruction on how to install it.

  1. Download this component and copy to your components folder.
  2. Create cake_dir/app/config/permissions.ini

The content for permissions.ini is as follows

1

2
[ControllerName]
3
actionName = group_id,group_id
4
 
5
[PluginName.ControllerName]
6
actionName = group_id,group_id
7

The ControllerName should be the controller name itself (in camel-case format).

The actionName is also the action name itself (in camel-case format).

The PluginName is the plugin name itself (in camel-case format).

The group_id is the one written in your Auth component. Your Auth component should have a group_id value because this is what our Permission component will look for. It could be a number or set of characters.

Example:

01

02
; Group ID 1 = Admin
03
; Group ID 2 = Member
04
[Accounts]
05
index =
06
add = 1
07
edit = 1,2
08
delete = 1,2
09
 
10
[ShoppingCart.Orders]
11
index =
12
report = 1
13

Assigning no group id in an actionName will simply ignore it.

Reminder

Use at your own risk. If you found a bug, please post them at the comment form.

jQuery star rating problem with brackets []


Warning: fopen(/html4strict.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/html.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/html4strict.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/html.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/javascript.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

If you used the star rating plugin of jQuery in cakePHP, you might experienced that the hover effects are not working properly. This is because one of the class name used to identify the buttons uses the radio button name. This will be a problem in cakephp because we use brackets in names.


<input type="radio" name="data[Model][field]" class="star" />

In order to fix that, we need to remove them. Here is quick fix. Open file jquery.rating.js:

from (line 97):


// grouping:
  var n = this.name;

to:


// grouping:
  var real_name = this.name;
  var n = real_name.replace(/[^A-Za-z0-9_\-]/g, '-'); // remove unwanted characters

from (line 108):


$.rating.groups[n].valueElem = $('<input type="hidden" name="' + n + '" value=""' + (settings.readOnly ? ' disabled="disabled"' : '') + '>');

to:


$.rating.groups[n].valueElem = $('<input type="hidden" name="' + real_name + '" value=""' + (settings.readOnly ? ' disabled="disabled"' : '') + '>');

I have reported this in the bugtracker and I hope it will be included in the next release.

Tags: , ,

Making Clickable Images on CakePHP

Hello again,

Yesterday, i stumbled upon this problem on how am i suppose to create an image with a link on it. I needed to know it because of my goal. I wanted to use images as my buttons to navigate the admin pages. So without further ado, here’s how:

1. We will use HTML Helpers Image and Link, and we shall combine this 2.

2. For Image,

Syntax:

$html->image(string $path, array $htmlAttributes, boolean $return = false);

Example:

$html->image(‘/img/images/cancel.png’, array(‘class’ => ‘save_button’));

3. For Link,

Syntax:

$html->link(string $title, string $url, array $htmlAttributes, string $confirmMessage = false, boolean $escapeTitle = true, boolean $return = false);

Example:

$html->link(‘SAVE’, ‘/registers’, array(), false, false, false);

4. So to make an image clickable,

<?php echo $html->link($html->image(‘/img/images/cancel.png’,array(‘class’ => ‘save_button’)), ‘/registers’, array(), false, false, false); ?>

That’s it. I hope this will help you in the future.

Convenient $html->link()


Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

Warning: fopen(/php.php) [function.fopen]: failed to open stream: Permission denied in /home/promet/public_html/cakephp/wp-content/plugins/devformatter/devgeshi.php on line 103

I made this quick helper that extends Html Helper. While creating my very first plugin for cakephp 1.2, I decided to use Route so that I can save some space and time typing long controller names. Here is the sample code in my views:


echo $html->link( 'Link Test',
                  array(
                    'controller' => 'long_controller_name',
                    'actions' => 'index',
                    'plugin' => 'long_plugin_name'
                  )
                );

I am a lazy typist and so I wanted to be able to use shortcut names for the array keys such as the following:

c = controller
a = action
p = plugin

OR use numbers

0 = controller
1 = action
2 = plugin

I couldn’t find in the bakery any solution so I decided to create my own Html helper. Here is how you can install it:

First, add this file to your helpers folder located in
/cakedir/app/views/helpers/
Download XHTML Helper

Then, in your controller file, include the Xhtml to your helpers list. Example


class MyController extends AppController {
  ...
  var $helpers = array( 'Xhtml' );
  ...
}

That’s it. You can use it by doing the following:


echo $xhtml->link( 'Link Test',
                    array(
                        'c' => 'long_controller_name',
                        'a' => 'index',
                        'p' => 'long_plugin_name'
                    )
                 );

OR


echo $xhtml->link( 'Link Test',
                    array( 'long_controller_name',
	                       'index',
	                       'long_plugin_name'
	                )
                 );

Take note of using xhtml instead of html. I hope I don’t mislead anyone for using the name XHTML :P . I just want an extended HTML helper.

[Update]: update code here

Tags: ,