/** * getInstance. * * @return Object. */ public static function &getInstance() { if (!self::$_instance) { self::$_instance = new RuleEngineHelper(); } return self::$_instance; }
/** * sendRequest * * @param string &$request Param * @param string &$post Param * @param object $userid Param * * @return boolean */ protected function sendRequest(&$request, &$post, $userid = null) { $success = false; $this->logger->log(JLog::INFO, 'sendRequest request', $request); $this->logger->log(JLog::INFO, 'sendRequest post', $post); $rule_engine = RuleEngineHelper::getInstance(); $plugin = $request->plugin; $rule_engine->load($plugin); // Channels - Rules $channel_rules = $rule_engine->getChannels($request->plugin, $post); $hasRules = !empty($channel_rules); if ($hasRules) { $channel_rules_ids = array_keys($channel_rules); $this->logger->log(JLog::INFO, 'getChannels:Rules found for plugin ' . $plugin . ' n=' . count($channel_rules)); } else { $this->logger->log(JLog::INFO, 'getChannels: No rules found for plugin ' . $plugin); $channel_rules_ids = array(); } $author = $post->xtform->get('author', null); $channels = ChannelFactory::getInstance()->getChannels($author); if (AUTOTWEETNG_JOOCIAL) { $params = AdvancedattrsHelper::getAdvancedAttrByReq($request->id); if (isset($params->channels) && is_array($params->channels) && count($params->channels) > 0) { $filtered_channels = array(); foreach ($params->channels as $c) { if (array_key_exists($c, $channels)) { $filtered_channels[$c] = $channels[$c]; } } $channels = $filtered_channels; } } if ($this->denyall_rulemode) { $this->logger->log(JLog::INFO, 'sendRequest denyall_rulemode'); // Only rule channels are processed $remaining_channels_ids = array(); $success = true; } else { // Rest of the Channels $channels_ids = array_keys($channels); $remaining_channels_ids = array_diff($channels_ids, $channel_rules_ids); } // Save orginal url for log and other usages $post->org_url = $post->url; // A request for each Channel - Rule $result_msg = ''; $initial_autopublish_state = $post->autopublish; $initial_show_url_state = $post->show_url; $initial_target_id = $post->xtform->get('target_id'); $this->logger->log(JLog::INFO, 'sendRequest channel_rules', $channel_rules_ids); $this->logger->log(JLog::INFO, 'sendRequest remaining_channels_ids', $remaining_channels_ids); foreach ($channel_rules as $channel_id => $rule) { $this->logger->log(JLog::INFO, 'sendRequest channel_rules processing: ' . $channel_id); // There's a rule, but the channel is not enabled if (!isset($channels[$channel_id])) { continue; } $channel = $channels[$channel_id]; $channelpost = clone $post; $channelpost->id = 0; $channelpost->channel_id = $channel_id; $channelpost->autopublish = $channel->isAutopublish() && $initial_autopublish_state; $channelpost->show_url = $initial_show_url_state; $channelpost->xtform->set('target_id', $initial_target_id); $rule_engine->executeRule($rule, $channel, $channelpost); $success = $this->_sendRequest($channel, $channelpost); // If one channel fails, it's stopped if (!$success) { $this->logger->log(JLog::INFO, 'sendRequest: failed, stopping process (1).'); return false; } } // A request for each of the remaining Channels foreach ($remaining_channels_ids as $channel_id) { $this->logger->log(JLog::INFO, 'sendRequest remaining_channels_ids processing: ' . $channel_id); $channel = $channels[$channel_id]; $post->id = 0; $post->channel_id = $channel_id; $post->autopublish = $channel->isAutopublish() && $initial_autopublish_state; $post->show_url = $initial_show_url_state; if ($initial_target_id) { $post->xtform->set('target_id', $initial_target_id); } else { $post->xtform->set('target_id', $channel->getTargetId()); } $success = $this->_sendRequest($channel, $post, $userid); // If one channel fails, it's stopped if (!$success) { $this->logger->log(JLog::INFO, 'sendRequest: failed, stopping process (2).'); return false; } } $success = true; $this->logger->log(JLog::INFO, 'sendRequest: success, no more channels to process.'); // True when message is sent return $success; }