示例#1
0
 public function replace_content()
 {
     $content = $this->content;
     $this->add_tags($this->template_key, $this->values);
     // add todays date values
     if (!isset($this->values['day'])) {
         $this->values['day'] = date('d');
     }
     if (!isset($this->values['month'])) {
         $this->values['month'] = date('m');
     }
     if (!isset($this->values['year'])) {
         $this->values['year'] = date('y');
     }
     if (!isset($this->values['current_user']) && module_security::is_logged_in()) {
         $this->values['current_user'] = module_security::get_loggedin_id();
         $user_details = module_user::get_replace_fields(module_security::get_loggedin_id());
         foreach ($user_details as $key => $val) {
             $this->values['current_' . $key . ''] = $val;
         }
     }
     // basic conditional tags - eg: {if:name}Dear {name},{else:name}Hello,{endif:name}
     // todo - support nested if - pass to template module.
     $debug = false;
     if (preg_match_all('#\\{if:([^\\}=]+)([^\\}]*)\\}(.*){endif:\\1\\}.*#imsU', $content, $matches)) {
         foreach ($matches[1] as $key => $template_tag) {
             // does this first if have a conditional statement (eg: {if:STATUS=New}sdfasdf{else}asdfasdf{/if:STATUS}
             $bits = preg_split('#{else[^\\}]*}#', $matches[3][$key]);
             preg_match_all('#\\{(else)?(if:([^\\}=]+)([^\\}]*))?\\}#ims', $matches[0][$key], $elseif_matches);
             $new_content = false;
             if ($debug) {
                 echo $matches[0][$key] . "\n";
             }
             if ($debug) {
                 print_r($bits);
             }
             if ($debug) {
                 print_r($elseif_matches);
             }
             foreach ($elseif_matches[0] as $elseif_key => $elseif_condition) {
                 $template_tag_value = isset($this->values[strtolower($elseif_matches[3][$elseif_key])]) ? trim($this->values[strtolower($elseif_matches[3][$elseif_key])]) : '';
                 if (!strlen(trim($elseif_matches[3][$elseif_key]))) {
                     // we are at the final {else} statement, this means all other checks before have failed. we use this content.
                     if ($debug) {
                         echo "Reached final {else} statement, using this value\n";
                     }
                     $new_content = $bits[$elseif_key];
                     break;
                 } else {
                     if ($debug) {
                         echo "Checking {$elseif_condition} against " . $elseif_matches[3][$elseif_key] . " which has the value: " . $template_tag_value . "\n";
                     }
                     if (strlen($elseif_matches[4][$elseif_key]) && $elseif_matches[4][$elseif_key][0] == '=') {
                         // the if/elseif tag is checking a condition, rather than just an "does exist" check
                         $check_matching_value = ltrim(trim($elseif_matches[4][$elseif_key]), '=');
                         if ($debug) {
                             echo "Checking if it matches {$check_matching_value} \n";
                         }
                         if ($check_matching_value == $template_tag_value) {
                             if ($debug) {
                                 echo "YES WE HAVE A MATCH \n";
                             }
                             // this first if statement matches this query! yes!
                             // use its value in the final $new_content
                             $new_content = $bits[$elseif_key];
                             break;
                         } else {
                             if ($debug) {
                                 echo "No match this time \n";
                             }
                         }
                     } else {
                         // we're just checking if this value exists or not.
                         if (strlen($template_tag_value) > 0 && $template_tag_value != '0000-00-00' && $template_tag_value != _l('N/A')) {
                             // it's a match!
                             $new_content = $bits[$elseif_key];
                             break;
                         } else {
                             // no match, move onto next bit.
                         }
                     }
                 }
             }
             if ($debug) {
                 echo "Final content to use will be: \n" . $new_content;
             }
             $content = str_replace($matches[0][$key], $new_content, $content);
         }
     }
     foreach ($this->values as $key => $val) {
         if (is_array($val)) {
             continue;
         }
         // if this isn't a html field we add newlines.
         if (!preg_match('#<[^>]+>#', $val)) {
             // raw text. nl2br
             $val = nl2br($val);
         }
         $content = str_replace('{' . strtoupper($key) . '}', $val, $content);
         // we perform some basic arithmetic on some replace fields.
         if (preg_match_all('#\\{(currency:)?' . preg_quote(strtoupper($key), '#') . '([*+-])([\\d\\.]+)\\}#', $content, $matches)) {
             // pull the "number" portion out of this string for math processing.
             // string could look like this: "$150.10 USD"
             $mathval = $originalval = $val;
             if (preg_match('#([\\d.,]+)#', $val, $mathvalmatches)) {
                 $mathval = $originalval = $mathvalmatches[1];
             }
             foreach ($matches[0] as $i => $v) {
                 $mathval = $originalval;
                 if ($matches[2][$i] == '-') {
                     $mathval = $mathval - $matches[3][$i];
                 } else {
                     if ($matches[2][$i] == '+') {
                         $mathval = $mathval + $matches[3][$i];
                     } else {
                         if ($matches[2][$i] == '*') {
                             $mathval = $mathval * $matches[3][$i];
                         }
                     }
                 }
                 if (strtolower($matches[1][$i]) == 'currency:') {
                     $mathval = dollar($mathval, true, isset($this->values['currency_id']) ? $this->values['currency_id'] : false);
                 }
                 $newval = str_replace($originalval, $mathval, $val);
                 $content = str_replace($v, $newval, $content);
             }
         }
         if (preg_match_all('#\\{currency:(' . preg_quote(strtoupper($key), '#') . ')\\}#', $content, $matches)) {
             foreach ($matches[0] as $i => $v) {
                 $content = str_replace($v, dollar($val, true, isset($this->values['currency_id']) ? $this->values['currency_id'] : false), $content);
             }
         }
         // we perform some arithmetic on date fields.
         $matches = false;
         if (stripos($key, 'date') !== false && $val && strlen($val) > 6 && preg_match_all('#' . preg_quote('{' . strtoupper($key), '#') . '((?>[+-]\\d+[ymd])*)\\}#', $content, $matches)) {
             //$processed_date = (input_date($val)); $processed_date_timeo =
             $processed_date_time = strtotime(input_date($val));
             foreach ($matches[0] as $i => $v) {
                 if (preg_match_all('#([+-])(\\d+)([ymd])#', $matches[1][$i], $date_math)) {
                     foreach ($date_math[1] as $di => $dv) {
                         $period = $date_math[3][$di];
                         $period = $period == 'd' ? 'day' : ($period == 'm' ? 'month' : ($period == 'y' ? 'year' : 'days'));
                         //echo $dv.$date_math[2][$di]." ".$period."\n";
                         $processed_date_time = strtotime($dv . $date_math[2][$di] . " " . $period, $processed_date_time);
                     }
                     $content = str_replace($v, print_date($processed_date_time), $content);
                     //echo "Processing date: $val - $processed_date (time: $processed_date_timeo / ".print_date($processed_date_timeo).") with result of: ".print_date($processed_date_time); exit;
                 }
             }
         }
         // we perform some date splitting
         $matches = false;
         if (stripos($key, 'date') !== false && $val && strlen($val) > 6 && preg_match_all('#' . preg_quote('{' . strtoupper($key), '#') . '-([ymdYMDjlSWFn])\\}#', $content, $matches)) {
             $processed_date_time = strtotime(input_date($val));
             foreach ($matches[0] as $i => $v) {
                 $content = str_replace($v, date($matches[1][$i], $processed_date_time), $content);
             }
         }
         //$val = str_replace(array('\\', '$'), array('\\\\', '\$'), $val);
         //$content = preg_replace('/\{'.strtoupper(preg_quote($key,'/')).'\}/',$val,$content);
     }
     if (preg_match_all('#\\{l:([^\\}]+)\\}#', $content, $matches)) {
         foreach ($matches[1] as $key => $label) {
             $content = str_replace($matches[0][$key], _l($label), $content);
         }
     }
     return $content;
 }