]> git.mxchange.org Git - friendica.git/blobdiff - object/Item.php
Should fix the like problem
[friendica.git] / object / Item.php
index c92b5e19eb3655fa6b9cd786a5a6be3341bb9249..b78d9b22019b2c519099fb08b611638754074315 100644 (file)
@@ -16,13 +16,23 @@ class Item extends BaseObject {
                'wall' => 'wall_thread.tpl',
                'wall2wall' => 'wallwall_thread.tpl'
        );
-       private $mode = null;
-       private $page_writeable = false;
-       private $profile_owner = 0;
+       private $toplevel = false;
+       private $writeable = false;
+       private $children = array();
+       private $parent = null;
+       private $conversation = null;
 
        public function __construct($data) {
                $this->data = $data;
                $this->set_template('wall');
+               $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
+               $this->writeable = ($this->get_data_value('writeable') || $this->get_data_value('self'));
+
+               // Prepare the children
+               foreach($data['children'] as $item) {
+                       $child = new Item($item);
+                       $this->add_child($child);
+               }
        }
 
        /**
@@ -32,13 +42,11 @@ class Item extends BaseObject {
         *              _ The data requested on success
         *              _ false on failure
         */
-       public function get_template_data($cmnt_tpl, $mode, $alike, $dlike) {
+       public function get_template_data($cmnt_tpl, $alike, $dlike) {
                $result = array();
 
                $a = $this->get_app();
 
-               $this->set_mode($mode);
-
                $item = $this->get_data();
 
                $comment = '';
@@ -53,17 +61,17 @@ class Item extends BaseObject {
                $osparkle = '';
                $lastcollapsed = false;
                $firstcollapsed = false;
-               $total_children += count_descendants($item);
+               $total_children = $this->count_descendants();
 
-               $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
-               $item_writeable = (($item['writable'] || $item['self']) ? true : false);
-               $show_comment_box = ((($this->is_page_writeable()) && ($item_writeable)) ? true : false);
+               $conv = $this->get_conversation();
+
+               $show_comment_box = ((($conv->is_writeable()) && ($this->is_writeable())) ? true : false);
                $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
                        || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
                        ? t('Private Message')
                        : false);
                $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
-               $shareable = ((($this->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
+               $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
                if(local_user() && link_compare($a->contact['url'],$item['author-link']))
                        $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
                else
@@ -77,7 +85,7 @@ class Item extends BaseObject {
                        'delete' => t('Delete'),
                );
                
-               $filer = (($this->get_profile_owner() == local_user()) ? t("save to folder") : false);
+               $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
 
                $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
                $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
@@ -97,7 +105,7 @@ class Item extends BaseObject {
                if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
                        $profile_avatar = $a->contacts[$normalised]['thumb'];
                else
-                       $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_thumb()));
+                       $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($this->get_data_value('thumb')));
 
                $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
                call_hooks('render_location',$locate);
@@ -108,15 +116,12 @@ class Item extends BaseObject {
                        $tag = trim($tag);
                        if ($tag!="") $tags[] = bbcode($tag);
                }
-               
-               like_puller($a,$item,$alike,'like');
-               like_puller($a,$item,$dlike,'dislike');
 
                $like    = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
                $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
 
-               if($toplevelpost) {
-                       if((! $item['self']) && ($this->get_mode() !== 'profile')) {
+               if($this->is_toplevel()) {
+                       if((! $item['self']) && ($conv->get_mode() !== 'profile')) {
                                if($item['wall']) {
 
                                        // On the network page, I am the owner. On the display page it will be the profile owner.
@@ -162,7 +167,7 @@ class Item extends BaseObject {
                                                $owner_url = zrl($owner_url);
                                }
                        }
-                       if($this->get_profile_owner() == local_user()) {
+                       if($conv->get_profile_owner() == local_user()) {
                                $isstarred = (($item['starred']) ? "starred" : "unstarred");
 
                                $star = array(
@@ -193,7 +198,7 @@ class Item extends BaseObject {
                        }
                }
 
-               if($this->is_page_writeable()) {
+               if($conv->is_writeable()) {
                        $buttons = array(
                                'like' => array( t("I like this \x28toggle\x29"), t("like")),
                                'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
@@ -210,12 +215,12 @@ class Item extends BaseObject {
                                }
                                $comment = replace_macros($cmnt_tpl,array(
                                        '$return_path' => '', 
-                                       '$jsreload' => (($this->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
-                                       '$type' => (($this->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
+                                       '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
+                                       '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
                                        '$id' => $item['item_id'],
                                        '$parent' => $item['item_id'],
                                        '$qcomment' => $qcomment,
-                                       '$profile_uid' =>  $this->get_profile_owner(),
+                                       '$profile_uid' =>  $conv->get_profile_owner(),
                                        '$mylink' => $a->contact['url'],
                                        '$mytitle' => t('This is you'),
                                        '$myphoto' => $a->contact['thumb'],
@@ -231,7 +236,7 @@ class Item extends BaseObject {
                                        '$edvideo' => t('Video'),
                                        '$preview' => t('Preview'),
                                        '$sourceapp' => t($a->sourcename),
-                                       '$ww' => (($this->get_mode() === 'network') ? $commentww : '')
+                                       '$ww' => (($conv->get_mode() === 'network') ? $commentww : '')
                                ));
                        }
                }
@@ -299,10 +304,10 @@ class Item extends BaseObject {
 
                $item_result['children'] = array();
                if(count($item['children'])) {
-                       $item_result['children'] = prepare_threads_body($a, $item['children'], $cmnt_tpl, $this->is_page_writeable(), $this->get_mode(), $this->get_profile_owner(), $alike, $dlike, ($thread_level + 1));
+                       $item_result['children'] = prepare_threads_body($a, $item['children'], $cmnt_tpl, $conv->is_writeable(), $conv->get_mode(), $conv->get_profile_owner(), $alike, $dlike, ($thread_level + 1));
                }
                $item_result['private'] = $item['private'];
-               $item_result['toplevel'] = ($toplevelpost ? 'toplevel_item' : '');
+               $item_result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
 
                if(get_config('system','thread_allow')) {
                        $item_result['flatten'] = false;
@@ -311,7 +316,7 @@ class Item extends BaseObject {
                else {
                        $item_result['flatten'] = true;
                        $item_result['threaded'] = false;
-                       if(!$toplevelpost) {
+                       if(!$htis->is_toplevel()) {
                                $item_result['comment'] = false;
                        }
                }
@@ -324,17 +329,104 @@ class Item extends BaseObject {
        public function get_id() {
                return $this->get_data_value('id');
        }
-       
-       public function get_network() {
-               return $this->get_data_value('network');
+
+       /**
+        * Add a child item
+        */
+       public function add_child($item) {
+               $item_id = $item->get_id();
+               if(!$item_id) {
+                       logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
+                       return false;
+               }
+               if($this->get_child($item->get_id())) {
+                       logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
+                       return false;
+               }
+               $item->set_parent($this);
+               $this->children[] = $item;
+               return end($this->children);
        }
-       
-       public function get_uid() {
-               return $this->get_data_value('uid');
+
+       /**
+        * Get a child by its ID
+        */
+       public function get_child($id) {
+               foreach($this->get_children() as $child) {
+                       if($child->get_id() == $id)
+                               return $child;
+               }
+               return null;
        }
-       
-       public function get_thumb() {
-               return $this->get_data_value('thumb');
+
+       /**
+        * Get all ou children
+        */
+       public function get_children() {
+               return $this->children;
+       }
+
+       /**
+        * Set our parent
+        */
+       protected function set_parent($item) {
+               $parent = $this->get_parent();
+               if($parent) {
+                       $parent->remove_child($this);
+               }
+               $this->parent = $item;
+               $this->set_conversation($item->get_conversation());
+       }
+
+       /**
+        * Remove our parent
+        */
+       protected function remove_parent() {
+               $this->parent = null;
+               $this->conversation = null;
+       }
+
+       /**
+        * Remove a child
+        */
+       public function remove_child($item) {
+               $id = $item->get_id();
+               foreach($this->get_children() as $key => $child) {
+                       if($child->get_id() == $id) {
+                               $child->remove_parent();
+                               unset($this->children[$key]);
+                               // Reindex the array, in order to make sure there won't be any trouble on loops using count()
+                               $this->children = array_values($this->children);
+                               return true;
+                       }
+               }
+               logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
+               return false;
+       }
+
+       /**
+        * Get parent item
+        */
+       protected function get_parent() {
+               return $this->parent;
+       }
+
+       /**
+        * set conversation
+        */
+       public function set_conversation($conv) {
+               $this->conversation = $conv;
+
+               // Set it on our children too
+               foreach($this->get_children() as $child)
+                       $child->set_conversation($conv);
+       }
+
+       /**
+        * get conversation
+        */
+       public function get_conversation() {
+               return $this->conversation;
        }
 
        /**
@@ -353,7 +445,7 @@ class Item extends BaseObject {
         *              _ value on success
         *              _ false on failure
         */
-       private function get_data_value($name) {
+       public function get_data_value($name) {
                if(!x($this->data, $name)) {
                        logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
                        return false;
@@ -363,70 +455,49 @@ class Item extends BaseObject {
        }
 
        /**
-        * Set the mode we'll be displayed on
+        * Set template
         */
-       private function set_mode($mode) {
-               if($this->get_mode() == $mode)
-                       return;
-
-               switch($mode) {
-                       case 'network':
-                       case 'notes':
-                               $this->profile_owner = local_user();
-                               $this->page_writeable = true;
-                               break;
-                       case 'profile':
-                               $this->profile_owner = $a->profile['profile_uid'];
-                               $this->page_writeable = can_write_wall($a,$this->profile_owner);
-                               break;
-                       case 'display':
-                               $this->profile_owner = $a->profile['uid'];
-                               $this->page_writeable = can_write_wall($a,$this->profile_owner);
-                               break;
-                       default:
-                               logger('[ERROR] Item::set_mode : Unhandled mode ('. $mode .').', LOGGER_DEBUG);
-                               return false;
-                               break;
+       private function set_template($name) {
+               if(!x($this->available_templates, $name)) {
+                       logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
+                       return false;
                }
+               $this->template = $this->available_templates[$name];
        }
 
        /**
-        * Get mode
+        * Get template
         */
-       private function get_mode() {
-               return $this->mode;
+       private function get_template() {
+               return $this->template;
        }
 
        /**
-        * Get profile owner
+        * Check if this is a toplevel post
         */
-       private function get_profile_owner() {
-               return $this->profile_owner;
+       private function is_toplevel() {
+               return $this->toplevel;
        }
 
        /**
-        * Get page writable
+        * Check if this is writeable
         */
-       private function is_page_writeable() {
-               return $this->page_writeable;
+       private function is_writeable() {
+               return $this->writeable;
        }
 
        /**
-        * Set template
+        * Count the total of our descendants
         */
-       private function set_template($name) {
-               if(!x($this->available_templates, $name)) {
-                       logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
-                       return false;
+       private function count_descendants() {
+               $children = $this->get_children();
+               $total = count($children);
+               if($total > 0) {
+                       foreach($children as $child) {
+                               $total += $child->count_descendants();
+                       }
                }
-               $this->template = $this->available_templates[$name];
-       }
-
-       /**
-        * Get template
-        */
-       private function get_template() {
-               return $this->template;
+               return $total;
        }
 }
 ?>