private function GetDataSetItems($displayId, $isPreview, $text) { $db =& $this->db; // Extra fields for data sets $dataSetId = $this->GetOption('datasetid'); $upperLimit = $this->GetOption('upperLimit'); $lowerLimit = $this->GetOption('lowerLimit'); $filter = $this->GetOption('filter'); $ordering = $this->GetOption('ordering'); Debug::LogEntry('audit', 'Then template for each row is: ' . $text); // Set an expiry time for the media $media = new Media(); $layout = new Layout(); $expires = time() + $this->GetOption('updateInterval', 3600) * 60; // Combine the column id's with the dataset data $matches = ''; preg_match_all('/\\[(.*?)\\]/', $text, $matches); $columnIds = array(); foreach ($matches[1] as $match) { // Get the column id's we are interested in Debug::LogEntry('audit', 'Matched column: ' . $match); $col = explode('|', $match); $columnIds[] = $col[1]; } // Get the dataset results $dataSet = new DataSet($db); if (!($dataSetResults = $dataSet->DataSetResults($dataSetId, implode(',', $columnIds), $filter, $ordering, $lowerLimit, $upperLimit, $displayId))) { return; } // Create an array of header|datatypeid pairs $columnMap = array(); foreach ($dataSetResults['Columns'] as $col) { $columnMap[$col['Text']] = $col; } Debug::Audit(var_export($columnMap, true)); $items = array(); foreach ($dataSetResults['Rows'] as $row) { // For each row, substitute into our template $rowString = $text; foreach ($matches[1] as $sub) { // Pick the appropriate column out $subs = explode('|', $sub); // The column header $header = $subs[0]; $replace = $row[$header]; // Check in the columns array to see if this is a special one if ($columnMap[$header]['DataTypeID'] == 4) { // Download the image, alter the replace to wrap in an image tag $file = $media->addModuleFileFromUrl(str_replace(' ', '%20', htmlspecialchars_decode($replace)), 'ticker_dataset_' . md5($dataSetId . $columnMap[$header]['DataSetColumnID'] . $replace), $expires); // Tag this layout with this file $layout->AddLk($this->layoutid, 'module', $file['mediaId']); $replace = $isPreview ? '<img src="index.php?p=module&mod=image&q=Exec&method=GetResource&mediaid=' . $file['mediaId'] . '" />' : '<img src="' . $file['storedAs'] . '" />'; } $rowString = str_replace('[' . $sub . ']', $replace, $rowString); } $items[] = $rowString; } return $items; }
protected function getTwitterFeed($displayId = 0, $isPreview = true) { if (!extension_loaded('curl')) { trigger_error(__('cURL extension is required for Twitter')); return false; } // Do we need to add a geoCode? $geoCode = ''; $distance = $this->GetOption('tweetDistance'); if ($distance != 0) { // Use the display ID or the default. if ($displayId != 0) { // Look up the lat/long $display = new Display(); $display->displayId = $displayId; $display->Load(); $defaultLat = $display->latitude; $defaultLong = $display->longitude; } else { $defaultLat = Config::GetSetting('DEFAULT_LAT'); $defaultLong = Config::GetSetting('DEFAULT_LONG'); } // Built the geoCode string. $geoCode = implode(',', array($defaultLat, $defaultLong, $distance)) . 'mi'; } // Connect to twitter and get the twitter feed. $key = md5($this->GetOption('searchTerm') . $this->GetOption('resultType') . $this->GetOption('tweetCount', 15) . $geoCode); if (!Cache::has($key) || Cache::get($key) == '') { Debug::Audit('Querying API for ' . $this->GetOption('searchTerm')); // We need to search for it if (!($token = $this->getToken())) { return false; } // We have the token, make a tweet if (!($data = $this->searchApi($token, $this->GetOption('searchTerm'), $this->GetOption('resultType'), $geoCode, $this->GetOption('tweetCount', 15)))) { return false; } // Cache it Cache::put($key, $data, $this->GetSetting('cachePeriod')); } else { Debug::Audit('Served from Cache'); $data = Cache::get($key); } Debug::Audit(var_export(json_encode($data), true)); // Get the template $template = $this->GetRawNode('template'); // Parse the text template $matches = ''; preg_match_all('/\\[.*?\\]/', $template, $matches); // Build an array to return $return = array(); // Media Object to get profile images $media = new Media(); $layout = new Layout(); // Expiry time for any media that is downloaded $expires = time() + $this->GetSetting('cachePeriodImages') * 60 * 60; // Remove URL setting $removeUrls = $this->GetOption('removeUrls', 1); // If we have nothing to show, display a no tweets message. if (count($data->statuses) <= 0) { // Create ourselves an empty tweet so that the rest of the code can continue as normal $user = new stdClass(); $user->name = ''; $user->screen_name = ''; $user->profile_image_url = ''; $tweet = new stdClass(); $tweet->text = $this->GetOption('noTweetsMessage', __('There are no tweets to display')); $tweet->created_at = date("Y-m-d H:i:s"); $tweet->user = $user; // Append to our statuses $data->statuses[] = $tweet; } // This should return the formatted items. foreach ($data->statuses as $tweet) { // Substitute for all matches in the template $rowString = $template; foreach ($matches[0] as $sub) { // Always clear the stored template replacement $replace = ''; // Maybe make this more generic? switch ($sub) { case '[Tweet]': // Get the tweet text to operate on $tweetText = $tweet->text; // Replace URLs with their display_url before removal if (isset($tweet->entities->urls)) { foreach ($tweet->entities->urls as $url) { $tweetText = str_replace($url->url, $url->display_url, $tweetText); } } // Handle URL removal if requested if ($removeUrls == 1) { $tweetText = preg_replace("((https?|ftp|gopher|telnet|file|notes|ms-help):((\\/\\/)|(\\\\))+[\\w\\d:#\\@%\\/;\$()~_?\\+-=\\\\.&]*)", '', $tweetText); } $replace = emoji_unified_to_html($tweetText); break; case '[User]': $replace = $tweet->user->name; break; case '[ScreenName]': $replace = $tweet->user->screen_name; break; case '[Date]': $replace = date($this->GetOption('dateFormat', Config::GetSetting('DATE_FORMAT')), DateManager::getDateFromGregorianString($tweet->created_at)); break; case '[ProfileImage]': // Grab the profile image if ($tweet->user->profile_image_url != '') { $file = $media->addModuleFileFromUrl($tweet->user->profile_image_url, 'twitter_' . $tweet->user->id, $expires); // Tag this layout with this file $layout->AddLk($this->layoutid, 'module', $file['mediaId']); $replace = $isPreview ? '<img src="index.php?p=module&mod=image&q=Exec&method=GetResource&mediaid=' . $file['mediaId'] . '" />' : '<img src="' . $file['storedAs'] . '" />'; } break; case '[Photo]': // See if there are any photos associated with this tweet. if (isset($tweet->entities->media) && count($tweet->entities->media) > 0) { // Only take the first one $photoUrl = $tweet->entities->media[0]->media_url; if ($photoUrl != '') { $file = $media->addModuleFileFromUrl($photoUrl, 'twitter_photo_' . $tweet->user->id . '_' . $tweet->entities->media[0]->id_str, $expires); $replace = $isPreview ? '<img src="index.php?p=module&mod=image&q=Exec&method=GetResource&mediaid=' . $file['mediaId'] . '" />' : '<img src="' . $file['storedAs'] . '" />'; // Tag this layout with this file $layout->AddLk($this->layoutid, 'module', $file['mediaId']); } } break; default: $replace = ''; } $rowString = str_replace($sub, $replace, $rowString); } // Substitute the replacement we have found (it might be '') $return[] = $rowString; } // Return the data array return $return; }
public function DataSetTableHtml($displayId = 0, $isPreview = true) { $db =& $this->db; // Show a preview of the data set table output. $dataSetId = $this->GetOption('datasetid'); $upperLimit = $this->GetOption('upperLimit'); $lowerLimit = $this->GetOption('lowerLimit'); $filter = $this->GetOption('filter'); $ordering = $this->GetOption('ordering'); $columnIds = $this->GetOption('columns'); $showHeadings = $this->GetOption('showHeadings'); $rowsPerPage = $this->GetOption('rowsPerPage'); if ($columnIds == '') { return 'No columns'; } // Set an expiry time for the media $media = new Media(); $layout = new Layout(); $expires = time() + $this->GetOption('updateInterval', 3600) * 60; // Create a data set view object, to get the results. $dataSet = new DataSet($db); if (!($dataSetResults = $dataSet->DataSetResults($dataSetId, $columnIds, $filter, $ordering, $lowerLimit, $upperLimit, $displayId))) { return; } $rowCount = 1; $rowCountThisPage = 1; $totalRows = count($dataSetResults['Rows']); if ($rowsPerPage > 0) { $totalPages = $totalRows / $rowsPerPage; } else { $totalPages = 1; } $table = '<div id="DataSetTableContainer" totalRows="' . $totalRows . '" totalPages="' . $totalPages . '">'; foreach ($dataSetResults['Rows'] as $row) { if ($rowsPerPage > 0 && $rowCountThisPage >= $rowsPerPage || $rowCount == 1) { // Reset the row count on this page $rowCountThisPage = 0; if ($rowCount > 1) { $table .= '</tbody>'; $table .= '</table>'; } // Output the table header $table .= '<table class="DataSetTable">'; if ($showHeadings == 1) { $table .= '<thead>'; $table .= ' <tr class="HeaderRow">'; foreach ($dataSetResults['Columns'] as $col) { $table .= '<th class="DataSetColumnHeaderCell">' . $col['Text'] . '</th>'; } $table .= ' </tr>'; $table .= '</thead>'; } $table .= '<tbody>'; } $table .= '<tr class="DataSetRow DataSetRow' . ($rowCount % 2 ? 'Odd' : 'Even') . '" id="row_' . $rowCount . '">'; // Output each cell for these results for ($i = 0; $i < count($dataSetResults['Columns']); $i++) { // Pull out the cell for this row / column $replace = $row[$i]; // What if this column is an image column type? if ($dataSetResults['Columns'][$i]['DataTypeID'] == 4) { // Download the image, alter the replace to wrap in an image tag $file = $media->addModuleFileFromUrl(str_replace(' ', '%20', htmlspecialchars_decode($replace)), 'datasetview_' . md5($dataSetId . $dataSetResults['Columns'][$i]['DataSetColumnID'] . $replace), $expires); // Tag this layout with this file $layout->AddLk($this->layoutid, 'module', $file['mediaId']); $replace = $isPreview ? '<img src="index.php?p=module&mod=image&q=Exec&method=GetResource&mediaid=' . $file['mediaId'] . '" />' : '<img src="' . $file['storedAs'] . '" />'; } $table .= '<td class="DataSetColumn" id="column_' . ($i + 1) . '"><span class="DataSetCellSpan" id="span_' . $rowCount . '_' . ($i + 1) . '">' . $replace . '</span></td>'; } $table .= '</tr>'; $rowCount++; $rowCountThisPage++; } $table .= '</tbody>'; $table .= '</table>'; $table .= '</div>'; return $table; }