]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Implement Smarty3
[friendica.git] / include / text.php
index 85ba99dd3163218832ede6053b9f2effaa6f5791..0591390a84f5a628736ddb137d1f584a6ff2eeb6 100644 (file)
@@ -9,19 +9,38 @@
 // depending on the order in which they were declared in the array.   
 
 require_once("include/template_processor.php");
+require_once("include/friendica_smarty.php");
 
 if(! function_exists('replace_macros')) {  
 function replace_macros($s,$r) {
        global $t;
-       
-       //$ts = microtime();
-       $r =  $t->replace($s,$r);
-       //$tt = microtime() - $ts;
-       
-       //$a = get_app();
-       //$a->page['debug'] .= "$tt <br>\n";
-       return template_unescape($r);
 
+//     $ts = microtime();
+       $a = get_app();
+
+       if($a->theme['template_engine'] === 'smarty3') {
+               $template = '';
+               if(gettype($s) === 'string') {
+                       $template = $s;
+                       $s = new FriendicaSmarty();
+               }
+               foreach($r as $key=>$value) {
+                       if($key[0] === '$') {
+                               $key = substr($key, 1);
+                       }
+                       $s->assign($key, $value);
+               }
+               $output = $s->parsed($template);
+       }
+       else {
+               $r =  $t->replace($s,$r);
+       
+               $output = template_unescape($r);
+       }
+//     $tt = microtime() - $ts;
+//     $a = get_app();
+//     $a->page['debug'] .= "$tt <br>\n";
+       return $output;
 }}
 
 
@@ -292,11 +311,11 @@ function alt_pager(&$a, $i) {
         $o .= '<div class="pager">';
 
        if($a->pager['page']>1)
-         $o .= "<a href=\"$url"."&page=".($a->pager['page'] - 1).'">' . t('newer') . '</a>';
+         $o .= "<a href=\"$url"."&page=".($a->pager['page'] - 1).'" class="pager_newer">' . t('newer') . '</a>';
         if($i>0) {
           if($a->pager['page']>1)
                  $o .= "&nbsp;-&nbsp;";
-         $o .= "<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('older') . '</a>';
+         $o .= "<a href=\"$url"."&page=".($a->pager['page'] + 1).'" class="pager_older">' . t('older') . '</a>';
        }
 
 
@@ -336,11 +355,18 @@ function sanitise_acl(&$item) {
 
 
 // Convert an ACL array to a storable string
+// Normally ACL permissions will be an array.
+// We'll also allow a comma-separated string.
 
 if(! function_exists('perms2str')) {
 function perms2str($p) {
        $ret = '';
-       $tmp = $p;
+
+       if(is_array($p))
+               $tmp = $p;
+       else
+               $tmp = explode(',',$p);
+
        if(is_array($tmp)) {
                array_walk($tmp,'sanitise_acl');
                $ret = implode('',$tmp);
@@ -414,29 +440,78 @@ if(! function_exists('get_intltext_template')) {
 function get_intltext_template($s) {
        global $lang;
 
+       $a = get_app();
+       $engine = '';
+       if($a->theme['template_engine'] === 'smarty3')
+               $engine = "/smarty3";
+
        if(! isset($lang))
                $lang = 'en';
 
-       if(file_exists("view/$lang/$s"))
-               return file_get_contents("view/$lang/$s");
-       elseif(file_exists("view/en/$s"))
-               return file_get_contents("view/en/$s");
+       if(file_exists("view/$lang$engine/$s"))
+               return file_get_contents("view/$lang$engine/$s");
+       elseif(file_exists("view/en$engine/$s"))
+               return file_get_contents("view/en$engine/$s");
        else
-               return file_get_contents("view/$s");
+               return file_get_contents("view$engine/$s");
 }}
 
 if(! function_exists('get_markup_template')) {
-function get_markup_template($s) {
-       $a=get_app();
+function get_markup_template($s, $root = '') {
+//     $ts = microtime();
+       $a = get_app();
+
+       if($a->theme['template_engine'] === 'smarty3') {
+               $template_file = get_template_file($a, 'smarty3/' . $s, $root);
+
+               $template = new FriendicaSmarty();
+               $template->filename = $template_file;
+
+//             $tt = microtime() - $ts;
+//             $a->page['debug'] .= "$tt <br>\n";
+               return $template;
+       }
+       else {
+               $template_file = get_template_file($a, $s, $root);
+//             $file_contents = file_get_contents($template_file);
+//             $tt = microtime() - $ts;
+//             $a->page['debug'] .= "$tt <br>\n";
+//             return $file_contents;
+               return file_get_contents($template_file);
+       }       
+}}
+
+if(! function_exists("get_template_file")) {
+function get_template_file($a, $filename, $root = '') {
        $theme = current_theme();
-       
-       if(file_exists("view/theme/$theme/$s"))
-               return file_get_contents("view/theme/$theme/$s");
-       elseif (x($a->theme_info,"extends") && file_exists("view/theme/".$a->theme_info["extends"]."/$s"))
-               return file_get_contents("view/theme/".$a->theme_info["extends"]."/$s");
+
+       // Make sure $root ends with a slash /
+       if($root !== '' && $root[strlen($root)-1] !== '/')
+               $root = $root . '/';
+
+       if(file_exists($root . "view/theme/$theme/$filename"))
+               $template_file = $root . "view/theme/$theme/$filename";
+       elseif (x($a->theme_info,"extends") && file_exists($root . "view/theme/".$a->theme_info["extends"]."/$filename"))
+               $template_file = $root . "view/theme/".$a->theme_info["extends"]."/$filename";
        else
-               return file_get_contents("view/$s");
+               $template_file = $root . "view/$filename";
+
+       return $template_file;
+}}
 
+if(! function_exists("set_template_includes")) {
+function set_template_includes($engine, $includes) {
+       if($engine === 'smarty3') {
+               $a = get_app();
+               foreach($includes as $name=>$path) {
+//                     $sm_includes[$name] = $_SERVER['DOCUMENT_ROOT'] . '/' . get_template_file($a, 'smarty3/' . $path);
+                       $sm_includes[$name] = get_template_file($a, 'smarty3/' . $path);
+               }
+               return $sm_includes;
+       }
+       else {
+               return $includes;
+       }
 }}
 
 
@@ -727,6 +802,39 @@ function get_poke_verbs() {
        return $arr;
 }
 
+function get_mood_verbs() {
+       
+       // index is present tense verb
+       // value is array containing past tense verb, translation of present, translation of past
+
+       $arr = array(
+               'happy'      => t('happy'),
+               'sad'        => t('sad'),
+               'mellow'     => t('mellow'),
+               'tired'      => t('tired'),
+               'perky'      => t('perky'),
+               'angry'      => t('angry'),
+               'stupefied'  => t('stupified'),
+               'puzzled'    => t('puzzled'),
+               'interested' => t('interested'),
+               'bitter'     => t('bitter'),
+               'cheerful'   => t('cheerful'),
+               'alive'      => t('alive'),
+               'annoyed'    => t('annoyed'),
+               'anxious'    => t('anxious'),
+               'cranky'     => t('cranky'),
+               'disturbed'  => t('disturbed'),
+               'frustrated' => t('frustrated'),
+               'motivated'  => t('motivated'),
+               'relaxed'    => t('relaxed'),
+               'surprised'  => t('surprised'),
+       );
+
+       call_hooks('mood_verbs', $arr);
+       return $arr;
+}
+
+
 /**
  * 
  * Function: smilies
@@ -793,7 +901,6 @@ function smilies($s, $sample = false) {
                ':facepalm',
                ':like',
                ':dislike',
-               '~friendika', 
                '~friendica'
 
        );
@@ -831,7 +938,6 @@ function smilies($s, $sample = false) {
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-facepalm.gif" alt=":facepalm" />',
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/like.gif" alt=":like" />',
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/dislike.gif" alt=":dislike" />',
-               '<a href="http://project.friendika.com">~friendika <img class="smiley" src="' . $a->get_baseurl() . '/images/friendika-16.png" alt="~friendika" /></a>',
                '<a href="http://friendica.com">~friendica <img class="smiley" src="' . $a->get_baseurl() . '/images/friendica-16.png" alt="~friendica" /></a>'
        );
 
@@ -924,13 +1030,11 @@ if(! function_exists('prepare_body')) {
 function prepare_body($item,$attach = false) {
 
        $a = get_app();
-       call_hooks('prepare_body_init', $item); 
+       call_hooks('prepare_body_init', $item);
 
-       $cache = get_config('system','itemcache');
-
-       if (($cache != '')) {
-               $cachefile = $cache."/".$item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']);
+       $cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
 
+       if (($cachefile != '')) {
                if (file_exists($cachefile))
                        $s = file_get_contents($cachefile);
                else {
@@ -946,6 +1050,9 @@ function prepare_body($item,$attach = false) {
        $s = $prep_arr['html'];
 
        if(! $attach) {
+               // Replace the blockquotes with quotes that are used in mails
+               $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
+               $s = str_replace(array('<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'), array($mailquote, $mailquote, $mailquote), $s);
                return $s;
        }
 
@@ -972,7 +1079,7 @@ function prepare_body($item,$attach = false) {
                                        }
                                        $title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]));
                                        $title .= ' ' . $mtch[2] . ' ' . t('bytes');
-                                       if((local_user() == $item['uid']) && $item['contact-id'] != $a->contact['id'])
+                                       if((local_user() == $item['uid']) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN))
                                                $the_url = $a->get_baseurl() . '/redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1];
                                        else
                                                $the_url = $mtch[1];
@@ -983,35 +1090,8 @@ function prepare_body($item,$attach = false) {
                }
                $s .= '<div class="clear"></div></div>';
        }
-       $matches = false;
-       $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
-       if($cnt) {
-//             logger('prepare_text: categories: ' . print_r($matches,true), LOGGER_DEBUG);
-               foreach($matches as $mtch) {
-                       if(strlen($x))
-                               $x .= ',';
-                       $x .= xmlify(file_tag_decode($mtch[1])) 
-                               . ((local_user() == $item['uid']) ? ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])) . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>' : '');
-               }
-               if(strlen($x))
-                       $s .= '<div class="categorytags"><span>' . t('Categories:') . ' </span>' . $x . '</div>'; 
 
 
-       }
-       $matches = false;
-       $x = '';
-       $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
-       if($cnt) {
-//             logger('prepare_text: filed_under: ' . print_r($matches,true), LOGGER_DEBUG);
-               foreach($matches as $mtch) {
-                       if(strlen($x))
-                               $x .= '&nbsp;&nbsp;&nbsp;';
-                       $x .= xmlify(file_tag_decode($mtch[1])) . ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&term=' . xmlify(file_tag_decode($mtch[1])) . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>';
-               }
-               if(strlen($x) && (local_user() == $item['uid']))
-                       $s .= '<div class="filesavetags"><span>' . t('Filed under:') . ' </span>' . $x . '</div>'; 
-       }
-
        // Look for spoiler
        $spoilersearch = '<blockquote class="spoiler">';
 
@@ -1065,6 +1145,73 @@ function prepare_text($text) {
 }}
 
 
+/**
+ * returns 
+ * [
+ *    //categories [
+ *          {
+ *               'name': 'category name',
+ *              'removeurl': 'url to remove this category',
+ *             'first': 'is the first in this array? true/false',
+ *               'last': 'is the last in this array? true/false',
+ *           } ,
+ *           ....
+ *       ],
+ *       // folders [
+ *               'name': 'folder name',
+ *               'removeurl': 'url to remove this folder',
+ *               'first': 'is the first in this array? true/false',
+ *               'last': 'is the last in this array? true/false',
+ *           } ,
+ *           ....       
+ *       ]
+ *   ]
+ */
+function get_cats_and_terms($item) {
+    $a = get_app();
+    $categories = array();
+    $folders = array();
+
+    $matches = false; $first = true;
+    $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
+    if($cnt) {
+        foreach($matches as $mtch) {
+            $categories[] = array(
+                'name' => xmlify(file_tag_decode($mtch[1])),
+                'url' =>  "#",
+                'removeurl' => ((local_user() == $item['uid'])?$a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])):""),
+                'first' => $first,
+                'last' => false
+            );
+            $first = false;
+        }
+    }
+    if (count($categories)) $categories[count($categories)-1]['last'] = true;
+    
+
+       if(local_user() == $item['uid']) {
+           $matches = false; $first = true;
+       $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
+           if($cnt) {
+           foreach($matches as $mtch) {
+                   $folders[] = array(
+                   'name' => xmlify(file_tag_decode($mtch[1])),
+                        'url' =>  "#",
+                       'removeurl' => ((local_user() == $item['uid'])?$a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&term=' . xmlify(file_tag_decode($mtch[1])):""),
+                   'first' => $first,
+                       'last' => false
+               );
+                   $first = false;
+                       }
+        }
+    }
+
+    if (count($folders)) $folders[count($folders)-1]['last'] = true;
+
+    return array($categories, $folders);
+}
+
+
 /**
  * return atom link elements for all of our hubs
  */