[eluser]popovich[/eluser]
Hello,
though I have already "talked to myself" on this forum, I hope this case will turn out somewhat differently.
I am working on a custom CMS, built entirely on CodeIgniter and am looking for a graceful solution for the following issue.
To add custom functionality to a page, I am using so called "modules", which consist of a class and a view file. The modules are being loaded on demand by searching a textfield for a definite pattern, for example [mod: search_results]. The controller looks for such a pattern, and if found, gets a "search_results" part, loads class and view files, parses them and inserts the parsed html in place of the pattern. The pattern might also include variables, like [mod: search_results, show=5, exclude=images] or something. The variable are also being searched for and passed onto the class file.
I have couple of functions inside the controller, which do all the work and they seem to work alright, but only if there is one [mod:] pattern in the textfield. Any further pattern is being ignored. Sure, I have not had that in mind initially.
Now, what is the best way to retrieve multiple occurrences of a pattern (in my case, a string starting with "[mod:" and ending with the next available "]")? How would you handle inserting parsed code in place of such pattern, if there are multiple patterns present?
Here is a piece of code, used for retrieving, parsing and inserting just one module pattern:
[eluser]popovich[/eluser]
Code: function load_modules ( $content ){
$retour = array();
$counter = 0;
$needle = "[mod:";
$thread = "]";
#
foreach ($content as $a){
$retour[$counter] = $a;
# if bodycopy contains text
if ( count( $a['bodycopy'] ) > 0 ) {
foreach($a['bodycopy'] as $b) {
$retour[$counter]['bodycopy'][0]['txt'] = $this->init_module($b['txt'], $needle, $thread);
}
}
#
$counter++;
}
return $retour;
}
#
# search and parse [mod:] pattern
# load classes and views
# parse content
#
function init_module( $body, $needle, $thread ) {
# search for [mod:] pattern
$rawmod_array = $this->retrieve_module($body, $needle, $thread);
foreach ( $rawmod_array as $rawmod ) {
$module = $this->parse_rawmod($rawmod);
# if [mod: ] found
if( $module[0] != '' ){
$dynamic_class = 'mc_'.$module[0];
$dynamic_view = 'modules/mv_'.$module[0];
# do we have params to pass to class?
$params = array();
if ( count($module) > 1 ) {
$params = $this->parse_rawparams($module);
}
# load class
$this->load->library($dynamic_class, $params, 'templibrary');
# evoke the assemble() function
$data = $this->templibrary->assemble();
# merge data with view template
$ready = $this->parser->parse($dynamic_view, $data, true);
# replace $this->GLO vars
$this->replace_globals($data);
# replace any text in the $body with $vessel value
$body = $this->replace_vessel($body, $data);
# insert parsed module content into introcopy/bodycopy
$body = $this->insert_module($body, $ready, $needle, $thread);
# clear
unset($this->templibrary);
}
}
return $body;
}
#
# search for [mod: ] pattern in a string
# and return it, if found
#
function retrieve_module ( $body, $needle, $thread ){
$retour = array();
$mn = stristr($body, $needle);
if ( $mn != false ){
$mn = trim($mn);
$mn = str_replace(', ',',',$mn);
$mn = str_replace($needle." ", $needle, $mn);
$mn = str_replace(" ".$thread, $thread, $mn);
$start = stripos($mn, $needle);
$rm = substr ( $mn, strlen($needle));
$end = stripos( $rm, $thread);
$length = $end - strlen($thread)+1;
$rm = substr ( $rm, 0, $length );
$rm = trim ( $rm );
$retour[] = $rm;
}
return $retour;
}
#
# replaces values of $this->GLO
# with the values from a module, if any are set
#
# searches for 'globals' key in a module data
#
function replace_globals ( $data ){
if ( isset( $data['globals'] ) && count( $data['globals'] ) > 0 ) {
foreach ( $data['globals'] as $key=>$val ){
if ( isset($this->GLO[$key]) ){
$this->GLO[$key] = $val;
}
}
}
}
#
# replace text within the text body
# which contains the module pattern
function replace_vessel ($body, $data) {
if ( isset($data['vessel']) && is_string($data['vessel']) ) {
return $data['vessel'];
}else{
return $body;
}
}
#
# insert generated and parsed module contents
# in place of [mod:] pattern
#
function insert_module ( $body, $ready, $needle, $thread ){
$start = stripos($body, $needle);
$end = stripos($body, $thread, $start);
$p1 = substr($body,0,$start);
$p2 = substr($body,$end+3);
return $p1.$ready.$p2;
}
#
# returns array, where [0] is the class name
#
function parse_rawmod ( $rawmod ) {
return explode(",",$rawmod);
}
#
# return array with keys=>vals for params
#
function parse_rawparams ( $arr ) {
$retour = array();
array_shift($arr);
foreach ( $arr as $pair ) {
$temp = str_replace(', ',',',$pair);
$temp = explode('=',$temp);
$retour[$temp[0]] = $temp[1];
}
return $retour;
}
|