/**
  * sampleAutoTweetNGIntegration
  *
  * @param   int    $productId  Param
  * @param   array  &$data      Param
  *
  * Example of how to save AutoTweetNG Request
  * Copy-paste into your extension, and customize freely
  *
  * @return	void
  */
 private static function sampleAutoTweetNGIntegration($productId, &$data)
 {
     // If product is not published, nothing else to do
     if (!$data['published']) {
         return;
     }
     $typeinfo = 99;
     $native_object = json_encode($data);
     // Product Url
     $viewProductUrl = EshopRoute::getProductRoute($productId, EshopHelper::getProductCategory($productId));
     // Image Url
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->clear();
     $query->select('image')->from('#__eshop_productimages')->where('product_id = ' . intval($productId))->order('ordering');
     $db->setQuery($query);
     $product_image = $db->loadResult();
     $image_url = null;
     if ($product_image && JFile::exists(JPATH_ROOT . '/media/com_eshop/products/' . $product_image)) {
         $image_url = 'media/com_eshop/products/' . $product_image;
     }
     $langmgmt_default_language = EParameter::getComponentParam(CAUTOTWEETNG, 'langmgmt_default_language');
     if ($langmgmt_default_language) {
         $key = 'product_name_' . $langmgmt_default_language;
         if (array_key_exists($key, $data)) {
             $title = $data[$key];
         }
         $key = 'product_desc_' . $langmgmt_default_language;
         if (array_key_exists($key, $data)) {
             $description = TextUtil::cleanText($data[$key]);
         }
     }
     if (empty($title)) {
         $title = $data['product_name'];
     }
     if (empty($description)) {
         $description = TextUtil::cleanText($data['product_desc']);
     }
     if (empty($title)) {
         return;
     }
     // Saving Advanced Attributes
     $autotweet_advanced = $data['autotweet_advanced_attrs'];
     $advanced_attrs = AdvancedattrsHelper::retrieveAdvancedAttrs($autotweet_advanced);
     AdvancedattrsHelper::saveAdvancedAttrs($advanced_attrs, $productId);
     // AutotweetAPI
     $id = self::insertRequest($productId, 'autotweetpost', JFactory::getDate()->toSql(), $title, $typeinfo, $viewProductUrl, $image_url, $native_object, $advanced_attrs);
     // Adding more information to the request
     $requestsModel = F0FModel::getTmpInstance('Requests', 'AutoTweetModel');
     $request = $requestsModel->getItem($id);
     $request = (array) $request;
     $request['xtform']->set('title', $title);
     $request['xtform']->set('fulltext', $description);
     $request['xtform']->set('author', JFactory::getUser()->username);
     $requestsModel->save($request);
 }
Exemple #2
0
 /**
  * queueMessage
  *
  * @param   string  $articleid         Param
  * @param   string  $source_plugin     Param
  * @param   string  $publish_up        Param
  * @param   string  $description       Param
  * @param   string  $typeinfo          Param
  * @param   string  $url               Param
  * @param   string  $image_url         Param
  * @param   object  &$native_object    Param
  * @param   string  &$advanced_attrs   Param
  * @param   string  &$params           Param
  * @param   string  $content_language  Param
  *
  * @return	mixed - false, or id of request
  */
 public static function insertRequest($articleid, $source_plugin, $publish_up, $description, $typeinfo = 0, $url = '', $image_url = '', &$native_object = null, &$advanced_attrs = null, &$params = null, $content_language = null)
 {
     $logger = AutotweetLogger::getInstance();
     // Check if message is already queued (it makes no sense to queue message more than once when modfied)
     // if message is already queued, correct the publish date
     $requestsModel = F0FModel::getTmpInstance('Requests', 'AutoTweetModel');
     $requestsModel->set('ref_id', $articleid);
     $requestsModel->set('plugin', $source_plugin);
     $requestsModel->set('typeinfo', $typeinfo);
     $row = $requestsModel->getFirstItem();
     $id = $row->id;
     // Avoid databse warnings when desc is longer then expected
     if (!empty($description)) {
         $description = TextUtil::cleanText($description);
         $description = JString::substr($description, 0, SharingHelper::MAX_CHARS_TITLE);
     }
     $routeHelp = RouteHelp::getInstance();
     if ($content_language) {
         $routeHelp->setContentLanguage($content_language);
     }
     if (AUTOTWEETNG_JOOCIAL && EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_mode') && EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_donot_post_url')) {
         $url = 'index.php';
     }
     $url = $routeHelp->getAbsoluteUrl($url);
     if (AUTOTWEETNG_JOOCIAL && EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_mode') && EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_donot_image_url')) {
         $image_url = null;
     }
     if (empty($image_url)) {
         // Default image: used in media mode when no image is available
         $image_url = EParameter::getComponentParam(CAUTOTWEETNG, 'default_image', '');
     }
     if (!empty($image_url)) {
         $image_url = $routeHelp->getAbsoluteUrl($image_url, true);
     }
     $row->reset();
     if ($id) {
         $row->load($id);
     }
     // If there's no date, it means now
     if (empty($publish_up)) {
         $publish_up = JFactory::getDate()->toSql();
     }
     $request = array('id' => $id, 'ref_id' => $articleid, 'plugin' => $source_plugin, 'publish_up' => $publish_up, 'description' => $description, 'typeinfo' => $typeinfo, 'url' => $url, 'image_url' => $image_url, 'native_object' => $native_object, 'params' => $params, 'published' => 0);
     $logger->log(JLog::INFO, 'Enqueued request', $request);
     // Saving the request
     $queued = $row->save($request);
     if (!$queued) {
         $logger->log(JLog::ERROR, 'queueMessage: error storing message to database message queue, article id = ' . $articleid . ', error message = ' . $row->getError());
     } else {
         $logger->log(JLog::INFO, 'queueMessage: message stored/updated to database message queue, article id = ' . $articleid);
     }
     if (!$id) {
         $id = $row->id;
     }
     if ($advanced_attrs && isset($advanced_attrs->attr_id)) {
         $row = F0FModel::getTmpInstance('Advancedattrs', 'AutoTweetModel')->getTable();
         $row->reset();
         $row->load($advanced_attrs->attr_id);
         $attr = array('id' => $advanced_attrs->attr_id, 'request_id' => $id);
         // Updating attr
         $result = $row->save($attr);
         if (!$result) {
             $logger->log(JLog::ERROR, 'Updating attr, attr_id = ' . $advanced_attrs->attr_id . ', error message = ' . $row->getError());
         } else {
             $logger->log(JLog::INFO, 'Updating attr, attr_id = ' . $advanced_attrs->attr_id);
         }
     }
     $app = JFactory::getApplication();
     if ($app->isAdmin() && JFactory::getConfig()->get('show_req_notification', true)) {
         $msg = VersionHelper::getFlavourName() . ': ' . JText::sprintf('COM_AUTOTWEET_REQUEST_ENQUEUED_MSG', $id);
         $app->enqueueMessage($msg);
     }
     return $queued ? $id : false;
 }
Exemple #3
0
 /**
  * publishRequest
  *
  * @param   array   $request  Param
  * @param   object  $userid   Param
  *
  * @return	boolean
  */
 public function publishRequest($request, $userid = null)
 {
     $this->logger->log(JLog::INFO, 'publishRequest request', $request);
     // Default: generate new entry for repost of entry with state success
     $postid = 0;
     $data = $this->_getContentData($request);
     if (!$data) {
         return false;
     }
     // Convert array to object
     $json = json_encode($data);
     $post = json_decode($json);
     if (AUTOTWEETNG_JOOCIAL) {
         $params = AdvancedattrsHelper::getAdvancedAttrByReq($request->id);
     }
     $post->ref_id = $request->ref_id;
     $post->plugin = $request->plugin;
     $post->postdate = $request->publish_up;
     $post->message = $post->text;
     unset($post->text);
     // Url
     if (isset($post->url) && !empty($post->url)) {
         $routeHelp = RouteHelp::getInstance();
         $url = $routeHelp->getAbsoluteUrl($post->url);
         $post->url = $url;
     } elseif (isset($request->url) && !empty($request->url)) {
         $post->url = $request->url;
     } else {
         $this->logger->log(JLog::INFO, 'publishRequest: No url');
         $post->url = '';
     }
     $this->logger->log(JLog::INFO, 'publishRequest: url = ' . $post->url);
     // Image url
     if (isset($post->image_url) && !empty($post->image_url)) {
         // If defined in getExtendedData, use this image_url
         $routeHelp = RouteHelp::getInstance();
         $url = $routeHelp->getAbsoluteUrl($post->image_url);
         // Only if it's a valid Url
         if (!ImageUtil::getInstance()->isValidImageUrl($url)) {
             $url = null;
         }
         $post->image_url = $url;
     } elseif (isset($request->image_url) && !empty($request->image_url)) {
         $url = $request->image_url;
         // Only if it's a valid Url
         if (!ImageUtil::getInstance()->isValidImageUrl($url)) {
             $url = null;
         }
         // Use this image_url (it's already routed)
         $post->image_url = $url;
     } else {
         $this->logger->log(JLog::INFO, 'publishRequest: No image url');
         $post->image_url = null;
     }
     $this->logger->log(JLog::INFO, 'publishRequest: image url = ' . $post->image_url);
     // Title
     // Truncate title and fulltext for new messages ('if' is for backward compatibillity)
     if (isset($post->title)) {
         $title = TextUtil::cleanText($post->title);
         $post->title = TextUtil::truncString($title, self::MAX_CHARS_TITLE);
     } else {
         $post->title = '';
     }
     // Fulltext
     if (!isset($post->fulltext)) {
         $post->fulltext = '';
     }
     if (AUTOTWEETNG_JOOCIAL) {
         if (isset($params->fulltext) && !empty($params->fulltext)) {
             $post->fulltext = $params->fulltext;
         }
     }
     if (AUTOTWEETNG_JOOCIAL && EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_mode') && ($fulltext = EParameter::getComponentParam(CAUTOTWEETNG, 'paywall_titleonly'))) {
         $post->fulltext = $fulltext;
     }
     $fulltext = TextUtil::cleanText($post->fulltext);
     $post->fulltext = TextUtil::truncString($fulltext, self::MAX_CHARS_FULLTEXT);
     $post->xtform = new JRegistry();
     // Catids
     if (isset($post->catids)) {
         $catids = $post->catids;
         unset($post->catids);
         $post->xtform->set('catids', $catids);
     } else {
         $post->xtform->set('catids', array());
     }
     // Author
     if (isset($post->author)) {
         $author = $post->author;
         unset($post->author);
         $post->xtform->set('author', $author);
     }
     // Language
     if (isset($post->language)) {
         $language = $post->language;
         unset($post->language);
         $post->xtform->set('language', $language);
     }
     // Access
     if (isset($post->access)) {
         $access = $post->access;
         unset($post->access);
         $post->xtform->set('access', $access);
     }
     // Target_id
     if (isset($post->target_id)) {
         $target_id = $post->target_id;
         unset($post->target_id);
         $post->xtform->set('target_id', $target_id);
     }
     // Hashtags
     if (isset($post->hashtags)) {
         $hashtags = $post->hashtags;
         unset($post->hashtags);
         $post->xtform->set('hashtags', $hashtags);
     }
     // Native object
     if (isset($request->native_object)) {
         $native_object = $request->native_object;
         $native_object = json_decode($native_object);
         if ($native_object) {
             $post->xtform->set('native_object', $native_object);
         } else {
             $this->logger->log(JLog::INFO, 'publishRequest: Inavlid JSON native_object');
         }
     }
     // Featured
     if (isset($post->featured)) {
         $post->xtform->set('featured', $post->featured);
         unset($post->featured);
     }
     return $this->sendRequest($request, $post, $userid);
 }
 /**
  * executeRule.
  *
  * @param   object  &$rule     Params
  * @param   object  &$channel  Params
  * @param   object  &$post     Params
  *
  * @return	void
  */
 public static function executeRule(&$rule, &$channel, &$post)
 {
     // Correct autopublish options when rules engine is used
     $post->nextstate = self::getValue($rule, self::RULE_AUTOPUBLISH);
     switch ($post->nextstate) {
         case 'on':
             $post->autopublish = true;
             break;
         case 'off':
             $post->autopublish = false;
             break;
         case 'cancel':
             $post->autopublish = false;
             break;
         case 'default':
             // Use default value from plugin/channel: do nothing
             break;
     }
     // Correct url link mode options when rules engine is used
     $show_url = self::getValue($rule, self::RULE_SHOW_URL);
     if ('default' != $show_url) {
         $post->show_url = $show_url;
     }
     // Target_id
     if (!isset($rule->xtform)) {
         $rule->xtform = Eform::paramsToRegistry($rule);
     }
     $target_id = $rule->xtform->get(self::RULE_TARGET_ID);
     if ($target_id) {
         $post->xtform->set('target_id', $target_id);
     }
     $message = TextUtil::cleanText($post->message);
     // Create message for new post (logged posts uses existing message text)
     // Filter first full and msgtext if there is an regex in rule
     $rule_reg_ex = self::getValue($rule, self::RULE_REG_EX);
     if (!empty($rule_reg_ex)) {
         $is_json = json_decode($rule_reg_ex);
         if ($is_json) {
             $rule_reg_ex = $is_json;
         }
         $rule_reg_replace = self::getValue($rule, self::RULE_REG_REPLACE);
         $is_json = json_decode($rule_reg_replace);
         if ($is_json) {
             $rule_reg_replace = $is_json;
         }
         $message = preg_replace($rule_reg_ex, $rule_reg_replace, $message);
         $post->title = preg_replace($rule_reg_ex, $rule_reg_replace, $post->title);
         $post->fulltext = preg_replace($rule_reg_ex, $rule_reg_replace, $post->fulltext);
     }
     $post->message = $message;
     // Apply a custom pattern to the text
     $pattern = self::getValue($rule, self::RULE_RMC_TEXTPATTERN);
     if (!empty($pattern)) {
         AutotweetBaseHelper::applyTextPattern($pattern, $post);
     }
     $message = $post->message;
     // Add static text from rules engine
     $show_static_text = self::getValue($rule, self::RULE_SHOW_STATIC_TEXT);
     $rule_static_text = self::getValue($rule, self::RULE_STATIC_TEXT);
     $message = AutotweetBaseHelper::addStatictext($show_static_text, $message, $rule_static_text);
     $post->message = $message;
 }
 /**
  * getMessageWithUrl
  *
  * @param   object  &$channel         Param
  * @param   object  &$post            Param
  * @param   string  $short_url        Param
  * @param   bool    $shorturl_always  Param
  *
  * @return	array
  */
 public static function getMessageWithUrl(&$channel, &$post, $short_url, $shorturl_always)
 {
     $includeHashTags = $channel->includeHashTags();
     $hashtags_chars = 0;
     $hashtags = null;
     if ($includeHashTags) {
         $hashtags = $post->xtform->get('hashtags');
         if ($hashtags) {
             $hashtags = trim($hashtags);
             $hashtags = TextUtil::cleanText($hashtags);
             $hashtags_chars = JString::strlen($hashtags) + 1;
         }
     }
     $message = TextUtil::cleanText($post->message);
     $message_len = JString::strlen($message);
     $long_url = $post->org_url;
     $is_showing_url = $post->show_url != AutotweetPostHelper::SHOWURL_OFF && !empty($long_url);
     $has_media = $channel->getMediaMode() != 'message' && isset($post->image_url);
     $has_weight = $channel->hasWeight();
     $max_chars = $channel->getMaxChars();
     $url = null;
     $url_len = 0;
     $totalmsg_len = $message_len;
     // Url Required and there's a Long Url
     if ($is_showing_url) {
         // Let's try with the long url
         $url = $long_url;
         $url_len = self::_getUrlLength($long_url, $has_weight, $is_showing_url);
         $totalmsg_len = $message_len + $url_len;
         // If always use ShortUrl or message len > channel max
         if ($shorturl_always || $totalmsg_len > $max_chars) {
             $url = $short_url;
             $url_len = self::_getUrlLength($short_url, $has_weight, $is_showing_url);
             $totalmsg_len = $message_len + $url_len;
         }
     }
     $totalmsg_len = $totalmsg_len + $hashtags_chars;
     $max_chars = self::_getMaxCharsAvailable($max_chars, $has_weight, $is_showing_url, $has_media);
     // Trunc text if needed, when Message Len > Max Channel Chars
     if ($totalmsg_len > $max_chars) {
         // Available chars for Message text
         $available_chars = $max_chars - $url_len;
         // Needs 3 chars for replacement with 3 dots
         $available_chars = $available_chars - 3;
         // And, the final cut
         $message = JString::substr($message, 0, $available_chars) . '...';
     }
     if ($includeHashTags && $hashtags) {
         $message = $message . ' ' . $hashtags;
     }
     // Construct status message
     switch ($post->show_url) {
         case AutotweetPostHelper::SHOWURL_OFF:
             // Dont show url, do nothing
             break;
         case AutotweetPostHelper::SHOWURL_BEGINNING:
             // Show url at beginning of message
             $message = $url . ' ' . $message;
             break;
         case AutotweetPostHelper::SHOWURL_END:
             // Show url at end of message
             $message = $message . ' ' . $url;
             break;
     }
     return array('url' => $url, 'message' => $message);
 }
 /**
  * Replaces spaces for hashtags
  *
  * @param   string  $word  Param
  *
  * @return	string
  */
 public static function getAsHashtag($word)
 {
     if ('' != $word) {
         $word = TextUtil::cleanText($word);
         $word = str_ireplace(' ', '', $word);
         $word = str_ireplace('-', '', $word);
         $hash = '#' . $word;
     } else {
         $hash = '';
     }
     return $hash;
 }