/** * Tweet on save of item */ public function hookSaveItem(&$pa_params) { $t_subject = $pa_params['instance']; // get instance from params $bitly = new bitly('collectiveaccess', 'R_8a0ecd6ea746c58f787d6769329b9976'); // check access $va_access_values = $this->opo_config->getList($t_subject->tableName() . '_tweet_when_access'); if (is_array($va_access_values) && sizeof($va_access_values)) { if (!in_array($t_subject->get('access'), $va_access_values)) { // Skip record because it is not publicly accessible return $pa_params; } } // check update threshold (prevents repeated tweeting when a record is repeatedly saved over a short period of time) if (($vn_threshold = $this->opo_config->get($t_subject->tableName() . "_tweet_update_threshold")) <= 0) { // seconds $vn_threshold = 3600; // default to one hour if no threshold is specified } $vb_access_changed = $t_subject->get('access') != $this->opn_old_access_value; $this->opn_old_access_value = null; if (!$vb_access_changed && time() - $this->opn_last_update_timestamp < $vn_threshold) { return $pa_params; } // Get Twitter token. If it doesn't exist silently skip posting. if ($o_token = @unserialize(file_get_contents(__CA_APP_DIR__ . '/tmp/twitter.token'))) { try { $o_twitter = new Zend_Service_Twitter(array('consumerKey' => $this->opo_config->get('consumer_key'), 'consumerSecret' => $this->opo_config->get('consumer_secret'), 'username' => $this->opo_config->get('twitter_username'), 'accessToken' => $o_token)); // Post to Twitter $vn_id = $t_subject->getPrimaryKey(); $vs_url = $this->opo_config->get($t_subject->tableName() . '_public_url') . $vn_id; $vs_url_bitly = $bitly->shorten($vs_url); $vs_tweet = $this->opo_config->get($pa_params['is_insert'] ? $t_subject->tableName() . '_new_message' : $t_subject->tableName() . '_updated_message'); if ($vs_tweet) { // substitute tags $vs_tweet = caProcessTemplate($vs_tweet, array('BITLY_URL' => $vs_url_bitly, 'URL' => $vs_url, 'ID' => $vn_id), array('getFrom' => $t_subject, 'delimiter' => ', ')); if (mb_strlen($vs_tweet) > 140) { $vs_tweet = mb_substr($vs_tweet, 0, 140); } $o_response = $o_twitter->status->update($vs_tweet); } } catch (Exception $e) { // Don't post error to user - Twitter failing is not a critical error // But let's put it in the event log so you have some chance of knowing what's going on //print "Post to Twitter failed: ".$e->getMessage(); $o_log = new Eventlog(); $o_log->log(array('SOURCE' => 'twitter plugin', 'MESSAGE' => _t('Post to Twitter failed: %1', $e->getMessage()), 'CODE' => 'ERR')); } } return $pa_params; }
/** * Manipulates posted form data for insertion into database * * @param mixed $val this elements posted form data * @param array $data posted form data * * @return mixed */ public function storeDatabaseFormat($val, $data) { /* $$$ hugh - added 'normalization' of links, to add http:// if no :// in the link. * not sure if we really want to do it here, or only when rendering? * $$$ hugh - quit normalizing links. */ $params = $this->getParams(); if (is_array($val)) { if ($params->get('use_bitly')) { require_once JPATH_SITE . '/components/com_fabrik/libs/bitly/bitly.php'; $login = $params->get('bitly_login'); $key = $params->get('bitly_apikey'); $bitly = new bitly($login, $key); } foreach ($val as $key => &$v) { if (is_array($v)) { if ($params->get('use_bitly')) { /* bitly will return an error if you try and shorten a shortened link, * and the class file we are using doesn't check for this */ if (!strstr($v['link'], 'bit.ly/') && $v['link'] !== '') { $v['link'] = (string) $bitly->shorten($v['link']); } } } else { if ($key == 'link') { $v = FabrikString::encodeurl($v); } // Not in repeat group if ($key == 'link' && $params->get('use_bitly')) { if (!strstr($v, 'bit.ly/') && $v !== '') { $v = (string) $bitly->shorten($v); } } } } } else { if (json_decode($val)) { return $val; } } $return = json_encode($val); return $return; }
/** * manupulates posted form data for insertion into database * @param mixed thie elements posted form data * @param array posted form data */ function storeDatabaseFormat($val, $data) { // $$$ hugh - added 'normalization' of links, to add http:// if no :// in the link. // not sure if we really want to do it here, or only when rendering? // $$$ hugh - quit normalizing links. $return = ''; $params = $this->getParams(); if (is_array($val)) { if ($params->get('use_bitly')) { require_once JPATH_SITE . DS . 'components' . DS . 'com_fabrik' . DS . 'libs' . DS . 'bitly' . DS . 'bitly.php'; $login = $params->get('bitly_login'); $key = $params->get('bitly_apikey'); $bitly = new bitly($login, $key); } foreach ($val as $key => &$v) { if (is_array($v)) { if ($params->get('use_bitly')) { // bitly will return an error if you try and shorten a shortened link, // and the class file we are using doesn't check for this if (!strstr($v['link'], 'bit.ly/') && $v['link'] !== '') { $v['link'] = $bitly->shorten($v['link']); } } /*$return .= implode(GROUPSPLITTER2, $v); $return .= GROUPSPLITTER;*/ } else { // not in repeat group if ($key == 'link' && $params->get('use_bitly')) { if (!strstr($v, 'bit.ly/') && $v !== '') { $v = $bitly->shorten($v); } } } } } else { $return = $val; } $return = json_encode($val); return $return; }