Esempio n. 1
0
	protected function showPlugin($catid, $thread, &$row, $linkOnly) {
		// Show a simple form to allow posting to forum from the plugin
		$plgShowForm = $this->params->get ( 'form', 1 );
		// Default is to put QuickPost at the very bottom.
		$formLocation = $this->params->get ( 'form_location', 0 );

		// Don't repeat the CSS for each instance of this plugin in a page!
		if (! self::$includedCss) {
			$doc = JFactory::getDocument ();
			$doc->addStyleSheet ( KUNENA_JLIVEURL . $this->basepath .'/kunenadiscuss/discuss.css' );
			self::$includedCss = true;
		}

		$subject = $row->title;
		$published = JFactory::getDate(isset($row->publish_up) ? $row->publish_up : 'now')->toUnix();
		$now = JFactory::getDate()->toUnix();

		// Find cross reference and the real topic
		$query = "SELECT d.content_id, d.thread_id, m.id AS mesid, t.thread, t.time
			FROM #__kunenadiscuss AS d
			LEFT JOIN #__kunena_messages AS m ON d.thread_id = m.id
			LEFT JOIN #__kunena_messages AS t ON t.id = m.thread
			WHERE d.content_id = {$this->_db->quote($row->id)}";
		$this->_db->setQuery ( $query );
		$result = $this->_db->loadObject ();
		CKunenaTools::checkDatabaseError ();

		if ( is_object($result) ) {
			if ($thread && $thread != $result->mesid) {
				// Custom Topic is not the same as cross reference, additional check needed
				$query = "SELECT t.thread
					FROM #__kunena_messages AS m
					LEFT JOIN #__kunena_messages AS t ON t.id = m.thread
					WHERE m.id = {$this->_db->quote($thread)}";
				$this->_db->setQuery ( $query );
				$result->thread = $this->_db->loadResult ();
				CKunenaTools::checkDatabaseError ();
				if (!$result->thread) {
					$this->debug ( "showPlugin: Custom Topic does not exist, aborting" );
					return '';
				}
				$this->debug ( "showPlugin: Custom Topic points to new location {$result->thread}" );
			}
			if ($result->thread != $result->thread_id) {
				// Topic has been moved, has been deleted or tag inside message has been changed
				$this->debug ( "showPlugin: Removing cross reference record pointing to topic {$result->thread_id}" );
				$query = "DELETE FROM #__kunenadiscuss WHERE content_id={$this->_db->quote($result->content_id)}";
				$this->_db->setQuery ( $query );
				$this->_db->query ();
				CKunenaTools::checkDatabaseError ();
				// We may need to add new cross reference or create new topic
				$thread = $result->thread;
				$result = null;
			}
		}
		if ( !is_object($result) && $thread) {
			// Find the real topic
			$query = "SELECT {$this->_db->quote($row->id)} AS content_id, t.id AS thread_id, m.id AS mesid, t.thread, t.time
				FROM #__kunena_messages AS m
				LEFT JOIN #__kunena_messages AS t ON t.id = m.thread
				WHERE m.id = {$this->_db->quote($thread)}";
			$this->_db->setQuery ( $query );
			$result = $this->_db->loadObject ();
			CKunenaTools::checkDatabaseError ();

			if ( !is_object($result) ) {
				$this->createReference ( $row, $thread );
				$this->debug ( "showPlugin: First hit to Custom Topic, created cross reference to topic {$thread}" );
			} else {
				$this->debug ( "showPlugin: First hit to Custom Topic, cross reference not created to topic {$thread} because it exist already" );
			}
		} else if (! is_object($result) ) {
			$thread = 0;
			$create = $this->params->get ( 'create', 0 );
			$createTime = $this->params->get ( 'create_time', 0 )*604800; // Weeks in seconds
			if ($createTime && $published+$createTime < $now) {
				$this->debug ( "showPlugin: Topic creation time expired, cannot start new discussion anymore" );
				return '';
			}
			if ($create) {
				$thread = $this->createTopic ( $row, $catid, $subject );
				$this->debug ( "showPlugin: First hit, created new topic {$thread} into forum" );
			}
		} else {
			$thread = $result->thread_id;
			$this->debug ( "showPlugin: Topic {$thread} exists in the forum" );
		}

		// Do we allow answers into the topic?
		$closeTime = $this->params->get ( 'close_time', 0 ) * 604800; // Weeks in seconds or 0
		if ($closeTime) {
			$this->debug ( "showPlugin: Check if topic is closed" );
			if ($result) {
				$closeReason = $this->params->get ( 'close_reason', 0 );
				if ($closeReason) {
					// Close topic by last post time
					$query = "SELECT MAX(time)
						FROM #__kunena_messages
						WHERE thread = {$this->_db->quote($result->thread)}";
					$this->_db->setQuery ( $query );
					$closeTime = $this->_db->loadResult () + $closeTime;
					CKunenaTools::checkDatabaseError ();
					$this->debug ( "showPlugin: Close time by last post" );
				} else {
					// Close topic by cration time
					$closeTime = $result->time + $closeTime;
					$this->debug ( "showPlugin: Close time by topic creation" );
				}
			} else {
				$closeTime = $now;
			}
		}

		if ($linkOnly && $thread) {
			$this->debug ( "showPlugin: Link only" );

			$sql = "SELECT COUNT(*) FROM #__kunena_messages WHERE hold=0 AND parent!=0 AND thread={$this->_db->quote($thread)}";
			$this->_db->setQuery ( $sql );
			$postCount = $this->_db->loadResult ();
			CKunenaTools::checkDatabaseError ();
			$linktitle = JText::sprintf ( 'PLG_KUNENADISCUSS_DISCUSS_ON_FORUMS', $postCount );
			require_once (KPATH_SITE . '/lib/kunena.link.class.php');
			$link = CKunenaLink::GetThreadLink ( 'view', $catid, $thread, $linktitle, $linktitle );
			return '<div class="kunenadiscuss">' . $link . '</div>';
		}

		// ************************************************************************
		// Process the QuickPost form

		$quickPost = '';
		if (!$plgShowForm) {
			$this->debug ( "showPlugin: Form has been disabled" );
		} elseif (!$closeTime || $closeTime >= $now) {
			$canPost = $this->canPost ( $catid, $thread );
			if ($canPost && JRequest::getInt ( 'kdiscussContentId', 0, 'POST' ) == $row->id) {
				$this->debug ( "showPlugin: Reply topic!" );
				$quickPost .= $this->replyTopic ( $row, $catid, $thread, $subject );
			} else {
				$quickPost .= $this->showForm ( $row, $catid, $thread, $subject );
			}
		}

		// This will be used all the way through to tell users how many posts are in the forum.
		$content = $this->showTopic ( $catid, $thread );

		if (!$content && !$quickPost) {
			return '';
		}
		if ($formLocation) {
			$content = '<div class="kunenadiscuss">' . $content . '<br />' . $quickPost . '</div>';
		} else {
			$content = '<div class="kunenadiscuss">' . $quickPost . "<br />" . $content . '</div>';
		}

		return $content;
	}
Esempio n. 2
0
 /**
  * @param  int    $catid
  * @param  int    $topic_id
  * @param  object $row
  * @param  bool   $linkOnly
  *
  * @return mixed|string
  */
 protected function showPlugin($catid, $topic_id, &$row, $linkOnly)
 {
     // Show a simple form to allow posting to forum from the plugin
     $plgShowForm = $this->params->get('form', 1);
     // Default is to put QuickPost at the very bottom.
     $formLocation = $this->params->get('form_location', 0);
     // Don't repeat the CSS for each instance of this plugin in a page!
     if (!self::$includedCss) {
         $doc = JFactory::getDocument();
         $doc->addStyleSheet(JUri::root(true) . "/plugins/content/kunenadiscuss/css/discuss.css");
         self::$includedCss = true;
     }
     // Find cross reference and the real topic
     $db = $this->db;
     $query = $db->getQuery(true)->select($db->quoteName('thread_id'))->from('#__kunenadiscuss')->where("content_id = {$db->quote($row->id)}");
     $this->db->setQuery($query);
     $result = $this->db->loadResult();
     KunenaError::checkDatabaseError();
     if ($topic_id) {
         // Custom topic found
         $this->debug("showPlugin: Loading Custom Topic {$topic_id}");
         $id = $topic_id;
     } elseif ($result) {
         // Reference found
         $this->debug("showPlugin: Loading Stored Topic {$result}");
         $id = $result;
     } else {
         // No topic exists
         $this->debug("showPlugin: No topic found");
         $id = 0;
     }
     $topic = KunenaForumTopicHelper::get($id);
     // If topic has been moved, find the real topic
     while ($topic->moved_id) {
         $this->debug("showPlugin: Topic {$topic->id} has been moved to {$topic->moved_id}");
         $topic = KunenaForumTopicHelper::get($topic->moved_id);
     }
     if ($result) {
         if (!$topic->exists()) {
             $this->debug("showPlugin: Topic does not exist, removing reference to {$result}");
             $this->deleteReference($row);
         } elseif ($topic->id != $id) {
             $this->debug("showPlugin: Topic has been moved or changed, updating reference to {$topic->id}");
             $this->updateReference($row, $topic->id);
         }
     } elseif ($topic_id && $topic->exists()) {
         $this->debug("showPlugin: First hit to Custom Topic, created reference to topic {$topic_id}");
         $this->createReference($row, $topic_id);
     }
     // Initialise some variables
     $subject = $row->title;
     if (isset($row->publish_up) && $row->publish_up != '0000-00-00 00:00:00') {
         $published = JFactory::getDate($row->publish_up)->toUnix();
         // take start publishing date
     } else {
         $published = JFactory::getDate($row->created)->toUnix();
         // or created date if publish_up is empty
     }
     $now = JFactory::getDate()->toUnix();
     if ($topic->exists()) {
         // If current user doesn't have authorisation to read existing topic, we are done
         if ($id && !$topic->authorise('read')) {
             $this->debug("showPlugin: Topic said {$topic->getError()}");
             return '';
         }
         $category = $topic->getCategory();
     } else {
         $this->debug("showPlugin: Let's see what we can do..");
         // If current user doesn't have authorisation to read category, we are done
         $category = KunenaForumCategoryHelper::get($catid);
         if (!$category->authorise('read')) {
             $this->debug("showPlugin: Category {$catid} said {$category->getError()}");
             return '';
         }
         $create = $this->params->get('create', 0);
         $createTime = $this->params->get('create_time', 0) * 604800;
         // Weeks in seconds
         if ($createTime && $published + $createTime < $now) {
             $this->debug("showPlugin: Topic creation time expired, cannot start new discussion anymore");
             return '';
         }
         if ($create) {
             $this->debug("showPlugin: First hit, created new topic {$topic_id} into forum");
             $topic = $this->createTopic($row, $category, $subject);
             if ($topic === false) {
                 return '';
             }
         }
     }
     // Do we allow answers into the topic?
     $closeTime = $this->params->get('close_time', 0) * 604800;
     // Weeks in seconds or 0 (forever)
     if ($closeTime && $topic->exists()) {
         $closeReason = $this->params->get('close_reason', 0);
         if ($closeReason) {
             $this->debug("showPlugin: Close time by last post");
             $closeTime += $topic->last_post_time;
         } else {
             $this->debug("showPlugin: Close time by topic creation");
             $closeTime += $topic->first_post_time;
         }
     } else {
         // Topic has not yet been created or will kept open forever
         $closeTime = $now + 1;
     }
     $linktopic = '';
     if ($topic->exists() && $linkOnly) {
         $this->debug("showPlugin: Displaying only link to the topic");
         $linktitle = JText::sprintf('PLG_KUNENADISCUSS_DISCUSS_ON_FORUMS', $topic->getReplies());
         return JHtml::_('kunenaforum.link', $topic->getUri($category), $linktitle, $linktitle);
     } elseif ($topic->exists() && !$plgShowForm) {
         $this->debug("showPlugin: Displaying link to the topic because the form is disabled");
         $linktitle = JText::sprintf('PLG_KUNENADISCUSS_DISCUSS_ON_FORUMS', $topic->getReplies());
         $linktopic = JHtml::_('kunenaforum.link', $topic->getUri($category), $linktitle, $linktitle);
     } elseif (!$topic->exists() && !$plgShowForm) {
         $linktopic = JText::_('PLG_KUNENADISCUSS_NEW_TOPIC_NOT_CREATED');
     }
     // ************************************************************************
     // Process the QuickPost form
     $quickPost = '';
     $canPost = $this->canPost($category, $topic);
     if ($canPost && $plgShowForm && (!$closeTime || $closeTime >= $now)) {
         if (JFactory::getUser()->get('guest')) {
             $this->debug("showPlugin: Guest can post: this feature doesn't work well if Joomla caching or Cache Plugin is enabled!");
         }
         if (JRequest::getInt('kdiscussContentId', -1, 'POST') == $row->id) {
             $this->debug("showPlugin: Reply topic!");
             $quickPost .= $this->replyTopic($row, $category, $topic, $subject);
         } else {
             $this->debug("showPlugin: Displaying form");
             $quickPost .= $this->showForm($row, $category, $topic, $subject);
         }
     }
     // This will be used all the way through to tell users how many posts are in the forum.
     $content = $this->showTopic($category, $topic, $linktopic);
     if (!$content && !$quickPost) {
         return $linktopic;
     }
     if ($formLocation) {
         $content = '<div class="kunenadiscuss">' . $content . '<br />' . $quickPost . '</div>';
     } else {
         $content = '<div class="kunenadiscuss">' . $quickPost . "<br />" . $content . '</div>';
     }
     return $content;
 }