public static function getSourceModel($source_id) { if (!isset(SourceModel::$_source_cache[$source_id])) { $sources = new Sources(); $source = $sources->getSource($source_id); $model = SourceModel::newInstance($source['service'], $source); SourceModel::$_source_cache[$source_id] = $model; } return SourceModel::$_source_cache[$source_id]; }
public function keywordAction() { // Action parameters $search = $this->getRequest()->getParam("search"); // A bit of filtering $search = substr($search, 0, 50); // Get all the items; if we are an admin, we also get the hidden one $data = new Data(); $sourcesTable = new Sources(); $sources = $sourcesTable->getSources(); $items = array(); foreach ($sources as $source) { $s = SourceModel::newInstance($source['service'], $source); $index = $s->getSearchIndex(); $prefix = $s->getServicePrefix(); $id = $s->getID(); if ($index != '') { $r = $data->search($id, $prefix, $index, $search, $this->_admin); if ($r) { $items = array_merge($items, $r); } } } // Sort the result of the search $sorter = new Stuffpress_SortItems(); $sorter->sort($items, 1); // Prepare the common elements $this->common(); // Add specifics entries $this->view->search = $search; $this->view->items = $items; $this->view->models = $this->getModels(); // Set page title $title = $this->_properties->getProperty('title'); $subtitle = $this->_properties->getProperty('subtitle'); $separator = $title ? "|" : ""; $this->view->headTitle("{$title} {$separator} Search results for {$search}"); // Add specific styles and javascripts $this->view->headScript()->appendFile('js/controllers/timeline.js'); $this->view->headLink()->appendStylesheet('style/lightbox.css'); // Render the index $this->render('index'); }
public function indexAction() { // Get, check and setup the parameters if (!($widget_id = $this->getRequest()->getParam("id"))) { throw new Stuffpress_Exception("No widget id provided to the widget controller"); } // Verify if the requested widget exist and get its data $widgets = new Widgets(); if (!($widget = $widgets->getWidget($widget_id))) { throw new Stuffpress_Exception("Invalid widget id"); } // Get all sources configured for that user $sources = new Sources(array(Stuffpress_Db_Table::USER => $widget['user_id'])); $mysources = $sources->getSources(); // If sources are configured, get the links for each source $links = array(); if ($mysources) { foreach ($mysources as $source) { if (!($source['public'] && $source['enabled'])) { continue; } if ($source['service'] == 'stuffpress') { continue; } $model = SourceModel::newInstance($source['service']); $model->setSource($source); $link['prefix'] = $model->getServicePrefix(); $link['url'] = $model->getServiceURL(); $link['name'] = $model->getTitle(); $links[] = $link; } } // Get the widget properties $properties = new WidgetsProperties(array(Properties::KEY => $widget_id)); $title = $properties->getProperty('title'); $this->view->title = $title ? $title : "My 2.0 Life"; // Prepare the view for rendering $this->view->links = $links; }
public function editAction() { // Get the item to edit from the parameters $item_id = $this->_getParam('item'); $source_id = $this->_getParam('source'); //Verify if the requested item exist $data = new Data(); if (!($item = $data->getItem($source_id, $item_id))) { throw new Stuffpress_NotFoundException("This item does not exist."); } // Get the user $users = new Users(); $attributes = $item->getAttributes(); $user = $users->getUser($attributes['user_id']); // Check if we are the owner if ($this->_application->user->id != $user->id) { throw new Stuffpress_NotFoundException("Not the owner"); } // Get the source to provide visual feedback $model = SourceModel::newInstance($item->getPrefix()); $this->view->service = $model->getServiceName(); // Prepare the form $form = $this->getForm($item->getType(), true, $item); $this->view->form = $form; $this->view->edit = true; // If it is an image, we display it if ($item->getType() == 'image') { $this->view->image_url = $item->getImageUrl(ImageItem::SIZE_THUMBNAIL); } // If it already has location, tell it if ($item->hasLocation()) { $this->view->has_location = true; $this->view->location = "Lat: " . $item->getLatitude() . " - Lng: " . $item->getLongitude(); } // forward to the appropriate action $this->_forward($item->getType()); }
shuffle($sources); $success = 0; $failure = 0; $total = count($sources); foreach ($sources as $source) { if ($source['service'] == 'stuffpress') { continue; } if (!$source['enabled']) { continue; } if (!is_null($cl_source) && $source['service'] != $cl_source) { continue; } echo "Memory: " . memory_get_usage() . "\r\n"; $model = SourceModel::newInstance($source['service'], $source); try { if ($source['imported']) { echo "Updating source {$source['service']} for user {$user->username} [" . ($success + $failure) . "/{$total}] ({$source['id']})...."; $items = $model->updateData(); $model->onNewItems($items); echo " found " . count($items) . " items\r\n"; } else { echo "Importing source {$source['service']} ({$source['id']}).\r\n"; $items = $model->importData(); } $success++; } catch (Exception $e) { echo "Could not update source {$source['id']}: " . $e->getMessage(); $logger->log("Could not update source {$source['id']}: " . $e->getMessage(), Zend_Log::ERR); echo $e->getTraceAsString();
private function getAvailableSources() { $sourcesTable = new Sources(); $sources = $sourcesTable->getSources(); $s = array(); if ($sources) { foreach ($sources as $source) { $model = SourceModel::newInstance($source['service']); $model->setSource($source); if ($model->isStoryElement()) { $s[$source['id']] = $model->getServiceName() . "(" . $model->getAccountName() . ")"; } } } return $s; }
private function getAvailableSources() { $sourcesTable = new Sources(); $sources = $sourcesTable->getSources(); $s = array(); if ($sources) { foreach ($sources as $source) { $model = SourceModel::newInstance($source['service'], $source); $account = $model->getAccountName() ? "(" . $model->getAccountName() . ")" : ""; $s[$source['id']] = $model->getServiceName() . $account; } } return $s; }
public function settimestampAction() { // Get, check and setup the parameters $source_id = $this->getRequest()->getParam("source"); $item_id = $this->getRequest()->getParam("item"); $timestamp = $this->getRequest()->getParam("timestamp"); //Verify if the requested source exist $sources = new Sources(); if (!($source = $sources->getSource($source_id))) { throw new Stuffpress_Exception("Error updating timestamp for source {$source} - Invalid source"); } // Get the user $users = new Users(); $user = $users->getUser($source['user_id']); // Check if we are the owner if ($this->_application->user->id != $user->id) { throw new Stuffpress_Exception("Error updating timestamp for source {$source} - Not the owner"); } // Get the user properties $properties = new Properties($user->id); // Check if the date is valid if (!($date = Stuffpress_Date::strToTimezone($timestamp, $properties->getProperty('timezone')))) { $this->addErrorMessage("Could not understand the provided date/time"); return $this->_forward('index'); } // Ok, we can change the title of the item $model = SourceModel::newInstance($source['service'], $source); $model->setTimestamp($item_id, $date); $this->addStatusMessage("Life is cool"); // We redirect to the stream $this->_forward('index'); }
public function deleteAction() { // Get the source from the request and initialize the model $id = $this->_getParam('id'); // Get the sources database $sources = new Sources(); // Check if the source exists if (!($source = $sources->getSource($id))) { return $this->_helper->json->sendJson(true); } // Check if we own the source if ($source['user_id'] != $this->_application->user->id) { return $this->_helper->json->sendJson(true); } // Instantiate a model and remove all the data $model = SourceModel::newInstance($source['service']); $model->setSource($source); $model->deleteItems(); // Delete the duplicated from the Data table $data = new Data(); $data->deleteItems($source['id']); // Delete the source settings $properties = new SourcesProperties(array(Properties::KEY => $source['id'])); $properties->deleteAllProperties(); // Delete the tags $tags = new Tags(); $tags->deleteSource($source['id']); // Remove the source $sources->deleteSource($id); // We should also delete the associated comments $comments = new Comments(); $comments->deleteComments($source['id']); // Forward to the list view with a success message return $this->_helper->json->sendJson(false); }
protected function getModels() { $sources = new Sources(); $s = $sources->getSources(); $models = array(); if ($s) { foreach ($s as $source) { $model = SourceModel::newInstance($source['service'], $source); $models[$source['id']] = $model; } } return $models; }
function updateUser($user, &$success, &$failure, &$total) { $this->logger->log("Updating data for " . $user->username, Zend_Log::INFO); // Assign the proper shard id Zend_Registry::set("shard", $user->id); // Get the user sources $sourcesTable = new Sources(); $sources = $sourcesTable->getSources(); if (!$sources) { echo "No sources found to update.\r\n"; $this->logger->log("No sources found to update.", Zend_Log::INFO); die; } // Log an entry $this->logger->log("Updating {$user->username}", Zend_Log::INFO); shuffle($sources); $total = $total + count($sources); foreach ($sources as $source) { if ($source['service'] == 'stuffpress') { continue; } if (!$source['enabled']) { continue; } if (!is_null($this->cl_source) && $source['service'] != $this->cl_source) { continue; } $model = SourceModel::newInstance($source['service'], $source); if (!$model->isActive()) { echo "Skipping unactive source {$source['service']}.\r\n "; continue; } try { if ($source['imported']) { echo "Updating source {$source['service']} for user {$user->username} [" . ($success + $failure) . "/{$total}] ({$source['id']})...."; $items = $model->updateData(); $model->onNewItems($items); echo " found " . count($items) . " items\r\n"; } else { echo "Importing source {$source['service']} ({$source['id']}).\r\n"; $items = $model->importData(); } $success++; } catch (Exception $e) { echo "Could not update source {$source['id']}: " . $e->getMessage(); $this->logger->log("Could not update source {$source['id']}: " . $e->getMessage(), Zend_Log::ERR); echo $e->getTraceAsString(); $failure++; } } }
public function excelAction() { // Get the request parameters $id = $this->_getParam('id'); // Get the requested source $sources = new Sources(); if (!($source = $sources->getSource($id))) { throw new Stuffpress_Exception("Unknown source id {$id}"); } // Are we the owner of the source if ($source['user_id'] != $this->_application->user->id) { throw new Stuffpress_Exception("You are not authorized to download this source"); } // Get the source meta data $model = SourceModel::newInstance($source['service']); $model->setSource($source); $service = $model->getServiceName(); $desc = $model->getServiceDescription(); // Get the actual data to backup $data = new Data(); $items = $data->getAllItems($source['id']); // This is not a layout or rendered page $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout->disableLayout(); // Start the writer to Excel require_once 'PHPExcel.php'; require_once 'PHPExcel/Writer/Excel2007.php'; $objPHPExcel = new PHPExcel(); // Set properties $objPHPExcel->getProperties()->setCreator("storytlr.com"); $objPHPExcel->getProperties()->setLastModifiedBy("storytlr.com"); $objPHPExcel->getProperties()->setTitle("Backup of {$this->_application->user->username}'s {$service} account"); //$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document"); $objPHPExcel->getProperties()->setDescription($desc); // Add some data $objPHPExcel->setActiveSheetIndex(0); // Write the column titles $keys = array_keys($items[0]->getBackup()); for ($i = 0; $i < count($keys); $i++) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $keys[$i]); } // Write the data for ($i = 0; $i < count($items); $i++) { $item = $items[$i]->getBackup(); for ($j = 0; $j < count($keys); $j++) { $value = $item[$keys[$j]]; if ($value) { $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($j, $i + 2)->setValueExplicit("'{$value}", PHPExcel_Cell_DataType::TYPE_STRING); } } } // Rename sheet $objPHPExcel->getActiveSheet()->setTitle($service); // Save Excel 2007 file $root = Zend_Registry::get("root"); $key = Stuffpress_Token::create(6); $file = "{$service}-{$this->_application->user->username}-[{$key}].xls"; $path = $root . "/public/files/{$file}"; $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save($path); $this->_redirect($this->_base . "/files/{$file}"); }
public function indexAction() { // To do before anything else $this->initPage(); $from = $this->getRequest()->getParam("from"); $this->view->tab = $this->getRequest()->getParam("tab"); switch (trim(strtolower($from))) { default: case 'twentyfour': $start = time() - 24 * 60 * 60; $this->view->interval_name = "24 Hours"; break; case 'week': // Ugh... $start = strtotime(date("Y-m-d 00:00:00", time() - 7 * 24 * 60 * 60)); $this->view->interval_name = "Week"; break; case 'month': $start = time() - 28 * 24 * 60 * 60; $this->view->interval_name = "4 Weeks"; break; case 'year': $start = time() - 365 * 24 * 60 * 60; $this->view->interval_name = "Year"; break; case 'all': $this->view->interval_name = false; $start = 0; break; } $data = new Data(); $this->view->items = $data->getItemsByDate($start, time()); if (false !== $this->view->items) { $this->view->total = count($this->view->items); $this->view->latest = $this->view->items[0]; $this->view->latest_source = SourceModel::getSourceModel($this->view->latest->getSource())->getTitle(); $this->view->latest_ago = $this->ago($this->view->latest->getTimestamp()); $this->view->models = $this->getModels(); ///// Type Distribution ///// $type_distribution = array(); foreach ($this->view->items as $item) { if (!isset($type_distribution[$item->getType()])) { $type_distribution[$item->getType()] = 0; } ++$type_distribution[$item->getType()]; } $data = array(); $labels = array(); foreach ($type_distribution as $type => $count) { $data[] = round($count / $this->view->total * 100); $labels[] = ucwords($type); } $this->view->type_distribution_url = "http://chart.apis.google.com/chart?cht=p3&chd=t:" . implode(',', $data) . "&chs=500x200&chl=" . implode('|', $labels) . '&chco=' . implode('|', $this->colors); ///// Source Distribution ///// $source_distribution = array(); foreach ($this->view->items as $item) { $source = SourceModel::getSourceModel($item->getSource())->getTitle(); if (!isset($source_distribution[$source])) { $source_distribution[$source] = 0; } ++$source_distribution[$source]; } $data = array(); $labels = array(); foreach ($source_distribution as $type => $count) { $data[] = round($count / $this->view->total * 100); $labels[] = ucwords($type); } $this->view->source_distribution_url = "http://chart.apis.google.com/chart?cht=p3&chd=t:" . implode(',', $data) . "&chs=500x200&chl=" . implode('|', $labels) . '&chco=' . implode('|', $this->colors); ///// Source Over Interval ///// $intervals = array(); switch (trim(strtolower($from))) { default: case 'twentyfour': for ($i = 24; $i > 0; --$i) { $s = $start + (23 - $i) * 3600; $e = $s + 3599; $intervals[] = array(strval($i), $s, $e); } $this->view->total_by_interval = "Total Posts By Source Per Previous Hour"; break; case 'week': // Not "precise", but it looks good :-) for ($i = 7; $i > 0; --$i) { $s = $start + (8 - $i) * 86400; $e = $s + 86399; $intervals[] = array(date('D', $e), $s, $e); } $this->view->total_by_interval = "Total Posts By Source Per Previous Day"; break; case 'month': // Not "precise", but it looks good :-) for ($i = 4; $i > 0; --$i) { $s = $start + (4 - $i) * 604800; $e = $s + 604799; $intervals[] = array(strval($i), $s, $e); } $this->view->total_by_interval = "Total Posts By Source Per Previous Week"; break; case 'year': // Not "precise", but it looks good :-) for ($i = 12; $i > 0; --$i) { $s = $start + (12 - $i) * 2419200; $e = $s + 2419199; $intervals[] = array(date('M', floor(($s + $e) / 2)), $s, $e); } $this->view->total_by_interval = "Total Posts By Source Per Month"; break; case 'all': $oldest = $this->view->items[count($this->view->items) - 1]->getTimestamp(); $intervals[] = array(date('M Y', $oldest), 0, $oldest - 1); $intervals[] = array('Now', $oldest, time()); $this->view->total_by_interval = "Total Posts By Source"; break; } $series = array(); $labels = array(); foreach ($intervals as $interval) { $labels[] = $interval[0]; } foreach ($this->view->items as $item) { $source = SourceModel::getSourceModel($item->getSource())->getTitle(); // Initialize the series if it doesn't exist if (!isset($series[$source])) { $series[$source] = array(); foreach ($intervals as $interval) { $series[$source][] = 0; } } foreach ($intervals as $key => $interval) { if ($item->getTimestamp() >= $interval[1] && $item->getTimestamp() < $interval[2]) { ++$series[$source][$key]; break; } } } // Determine maximum y-range bound $max = 1; foreach ($series as $serie) { foreach ($serie as $datum) { if ($datum > $max) { $max = $datum; } } } $data = array(); $legend = array(); foreach ($series as $name => $serie) { $data[] = implode(',', $serie); $legend[] = $name; } $this->view->total_by_interval_url = "http://chart.apis.google.com/chart?cht=lc&chd=t:" . implode('|', $data) . "&chs=500x200&chl=" . implode('|', $labels) . "&chds=0," . ($max + floor($max / 5)) . "&chxt=y&chxr=0,0," . ($max + floor($max / 5)) . "," . ceil($max / 5) . "&chco=" . implode(',', $this->colors) . '&chdl=' . implode('|', $legend); } $this->common(); $this->view->headScript()->appendFile('js/controllers/timeline.js'); }
public function getIcon() { return SourceModel::getSourceModel($this->getSource())->getProperty('icon', 'images/rss.png'); }