Hey folks,
I've created a GeSHi Behaviour for my application I am working on.
Here is the code:
<?php
class GeshiBehavior extends ModelBehavior {
function setup (&$model, $settings = array()) {
$default = array(
'colour1' => '#fff',
'colour2' => '#eee'
);
if(!isset($this->settings[$model->name])) {
$this->settings[$model->name] = $default;
}
$this->settings[$model->name] =
array_merge($this->settings[$model->name], ife(is_array($settings),
$settings, array()));
}
function afterFind(&$model, $results) {
foreach ($results as $key => $val) {
if(isset($val['Paste']['paste'])) {
$results[$key]['Paste']['paste_formatted'] =
$this->_generate($val['Paste']['paste'],
strtolower($val['Language']['language']), $model);
}
}
pr($results);
return $results;
}
function _generate($source,$lang,&$model) {
vendor('geshi/geshi');
$geshi =& new GeSHi($source,$lang);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);
$geshi->set_line_style('background:' .
$this->settings[$model->name]['colour1'] . ';', 'background:' .
$this->settings[$model->name]['colour2'] . ';');
$geshi->set_header_type(GESHI_HEADER_DIV);
return $geshi->parse_code();
}
}
?>
Now, when I view my /pastes/view/x, this behavior works fine. It
takes the raw code from the paste field and generates the
paste_formatted sudo-field which is displayed. Here is an example of
an array from my /paste/view/x:
Array
(
[0] => Array
(
[Paste] => Array
(
[id] => 9
[paste] => $pastemonkey(document).ready(function() {
/* Basic Bind Functions */
$pastemonkey('.new-paste').bind('click.newPaste', function(){
$pastemonkey('
').addClass('ajax-new-paste').css({display: 'none'}).insertBefore('.pastes');
$pastemonkey('.ajax-new-paste').load('/pastes/add',
function(){
$pastemonkey(this).animate({height: 'show',
opacity: 'show'});
});
return false;
});
$pastemonkey('.home').bind('click.home', function(){
$pastemonkey('.paste').load($pastemonkey(this).attr('href'));
return false;
});
/* Live Query Functions*/
$pastemonkey('.cancel-paste').livequery(function(){
$pastemonkey(this).bind('click.cancelPaste', function(){
$pastemonkey('.ajax-new-paste').animate({height:'hide',
opacity:'hide'}).remove();
return false;
});
}, function(){
$pastemonkey(this).unbind('click.cancelPaste');
});
$pastemonkey('a', '.paging').livequery(function(){
$pastemonkey(this).bind('click.paging', function(){
$pastemonkey('#main').load($pastemonkey(this).attr('href'));
return false;
});
}, function(){
$pastemonkey(this).unbind('click.paging');
});
$pastemonkey('.viewPaste').livequery(function(){
$pastemonkey(this).bind('click.viewPaste', function(){
$pastemonkey('#main').load($pastemonkey(this).attr('href'));
return false;
});
}, function(){
$pastemonkey(this).unbind('click.viewPaste');
});
/* AJAX Start/Stop Functions*/
$pastemonkey('#main').ajaxStart(function(){
$pastemonkey('.loading').animate({opacity:'show',
backgroundColor:
'#0f0'}, 'slow', 'linear')
.animate({backgroundColor: '#fff'});
});
$pastemonkey('#main').ajaxStop(function(){
$pastemonkey('.loading').animate({opacity: 'hide'});
});
});
[note] => Paste Monkey JS File
[author] => Tane
[tags] => Paste Monkey, Jquery
[parent_id] =>
[language_id] => 33
[created] => 2007-09-25 10:41:26
[expiry] => 2007-09-25 10:40:00
[paste_formatted] =>
1.
$pastemonkey(document).ready(function() {
2.
3.
/* Basic Bind Functions */
4.
5.
$pastemonkey('.new-paste').bind('click.newPaste', function(){
6.
$pastemonkey('<div></div>').addClass('ajax-new-paste').css({display:
'none'}).insertBefore('.pastes');
7.
$pastemonkey('.ajax-new-paste').load('/pastes/add', function(){
8.
$pastemonkey(this).animate({height: 'show', opacity: 'show'});
9.
});
10.
return false;
11.
});
12.
13.
$pastemonkey('.home').bind('click.home', function(){
14.
$pastemonkey('.paste').load($pastemonkey(this).attr('href'));
15.
return false;
16.
});
17.
18.
/* Live Query Functions*/
19.
20.
$pastemonkey('.cancel-paste').livequery(function(){
21.
$pastemonkey(this).bind('click.cancelPaste', function(){
22.
$pastemonkey('.ajax-new-paste').animate({height:'hide',
opacity:'hide'}).remove();
23.
return false;
24.
});
25.
}, function(){
26.
$pastemonkey(this).unbind('click.cancelPaste');
27.
});
28.
29.
$pastemonkey('a', '.paging').livequery(function(){
30.
$pastemonkey(this).bind('click.paging', function(){
31.
$pastemonkey('#main').load($pastemonkey(this).attr('href'));
32.
return false;
33.
});
34.
}, function(){
35.
$pastemonkey(this).unbind('click.paging');
36.
});
37.
38.
$pastemonkey('.viewPaste').livequery(function(){
39.
$pastemonkey(this).bind('click.viewPaste', function(){
40.
$pastemonkey('#main').load($pastemonkey(this).attr('href'));
41.
return false;
42.
});
43.
}, function(){
44.
$pastemonkey(this).unbind('click.viewPaste');
45.
});
46.
47.
/* AJAX Start/Stop Functions*/
48.
49.
$pastemonkey('#main').ajaxStart(function(){
50.
$pastemonkey('.loading').animate({opacity:'show', backgroundColor:
'#0f0'}, 'slow', 'linear')
51.
.animate({backgroundColor: '#fff'});
52.
});
53.
$pastemonkey('#main').ajaxStop(function(){
54.
$pastemonkey('.loading').animate({opacity: 'hide'});
55.
});
56.
57.
58.
});
)
[Parent] => Array
(
[id] =>
[paste] =>
[note] =>
[author] =>
[tags] =>
[parent_id] =>
[language_id] =>
[created] =>
[expiry] =>
)
[Language] => Array
(
[id] => 33
[language] => JavaScript
)
[Tag] => Array
(
[0] => Array
(
[id] => 2
[tag] => Jquery
)
[1] => Array
(
[id] => 1
[tag] => Paste Monkey
)
)
)
)
Now, I am trying to use the behavior on another view,
/languages/view/x. This is a view where you can see all posts related
to the specific language. For the same example above, here is an
output of my array:
Array
(
[0] => Array
(
[Language] => Array
(
[id] => 33
[language] => JavaScript
)
[Paste] => Array
(
[0] => Array
(
[id] => 9
[paste] => $pastemonkey(document).ready(function() {
/* Basic Bind Functions */
$pastemonkey('.new-paste').bind('click.newPaste', function(){
$pastemonkey('
').addClass('ajax-new-paste').css({display: 'none'}).insertBefore('.pastes');
$pastemonkey('.ajax-new-paste').load('/pastes/add',
function(){
$pastemonkey(this).animate({height: 'show',
opacity: 'show'});
});
return false;
});
$pastemonkey('.home').bind('click.home', function(){
$pastemonkey('.paste').load($pastemonkey(this).attr('href'));
return false;
});
/* Live Query Functions*/
$pastemonkey('.cancel-paste').livequery(function(){
$pastemonkey(this).bind('click.cancelPaste', function(){
$pastemonkey('.ajax-new-paste').animate({height:'hide',
opacity:'hide'}).remove();
return false;
});
}, function(){
$pastemonkey(this).unbind('click.cancelPaste');
});
$pastemonkey('a', '.paging').livequery(function(){
$pastemonkey(this).bind('click.paging', function(){
$pastemonkey('#main').load($pastemonkey(this).attr('href'));
return false;
});
}, function(){
$pastemonkey(this).unbind('click.paging');
});
$pastemonkey('.viewPaste').livequery(function(){
$pastemonkey(this).bind('click.viewPaste', function(){
$pastemonkey('#main').load($pastemonkey(this).attr('href'));
return false;
});
}, function(){
$pastemonkey(this).unbind('click.viewPaste');
});
/* AJAX Start/Stop Functions*/
$pastemonkey('#main').ajaxStart(function(){
$pastemonkey('.loading').animate({opacity:'show',
backgroundColor:
'#0f0'}, 'slow', 'linear')
.animate({backgroundColor: '#fff'});
});
$pastemonkey('#main').ajaxStop(function(){
$pastemonkey('.loading').animate({opacity: 'hide'});
});
});
[note] => Paste Monkey JS File
[author] => Tane
[tags] => Paste Monkey, Jquery
[parent_id] =>
[language_id] => 33
[created] => 2007-09-25 10:41:26
[expiry] => 2007-09-25 10:40:00
)
)
)
)
As you can see, both arrays have $var[$key]['Paste']['paste'], but
when I try view the language page, I get that paste_formatted is not
defined. Can anyone see what i'm doing wrong in my behavior? Both
models have var $actsAs = array('Geshi');
Thanks,
--
Tane Piper
http://digitalspaghetti.me.uk
This email is: [ ] blogable [ x ] ask first [ ] private
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---