示例#1
0
 /**
  * DOCUMENT ME
  * @param sfRequest $request
  * @return mixed
  */
 public function executeEdit(sfRequest $request)
 {
     $this->editSetup();
     $value = $this->getRequestParameter('slot-form-' . $this->id);
     $this->options['multiline'] = $this->getOption('multiline', true);
     $this->form = new aTextForm($this->id, $this->slot->value, $this->options);
     $this->form->bind($value);
     if ($this->form->isValid()) {
         // TODO: this might make a nice validator
         $value = $this->form->getValue('value');
         if (!$this->options['multiline']) {
             $value = preg_replace("/\\s/", " ", $value);
         }
         // We store light markup for "plain text" slots. We DO NOT store the mailto: obfuscation though
         $value = aHtml::textToHtml($value);
         $maxlength = $this->getOption('maxlength');
         if ($maxlength !== false) {
             $value = substr(0, $maxlength);
         }
         $this->slot->value = $value;
         $result = $this->editSave();
         return $result;
     } else {
         // Makes $this->form available to the next iteration of the
         // edit view so that validation errors can be seen (although there
         // aren't any in this case)
         return $this->editRetry();
     }
 }
 /**
  * DOCUMENT ME
  * @param mixed $arguments
  * @param mixed $options
  */
 protected function execute($arguments = array(), $options = array())
 {
     // We need a basic context so we can call helpers to format text
     $context = sfContext::createInstance($this->configuration);
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
     // PDO connection not so useful, get the doctrine one
     $conn = Doctrine_Manager::connection();
     $accounts = Doctrine::getTable('aEmbedMediaAccount')->findAll();
     foreach ($accounts as $a) {
         $perPage = 50;
         $service = aMediaTools::getEmbedService($a->service);
         if (!$service) {
             // An account for a service that has been deconfigured
             continue;
         }
         $total = null;
         $page = 1;
         $serviceUrls = array();
         while (true) {
             $results = $service->browseUser($a->username, $page, $perPage);
             if ($results === false) {
                 // We hit the rate limit, the account is bad, etc. Just
                 // be tolerant and retry later. Would be nice to distinguish
                 // these cases but it's not that hard to figure out an
                 // account is gone
                 break;
             }
             foreach ($results['results'] as $result) {
                 $serviceUrls[] = $result['url'];
             }
             // We hit the end of the results for this user
             if (!count($results['results'])) {
                 break;
             }
             $page++;
         }
         if (count($serviceUrls)) {
             $existingServiceUrls = Doctrine::getTable('aMediaItem')->createQuery('m')->select('m.service_url')->andWhereIn('m.service_url', $serviceUrls)->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);
         } else {
             $existingServiceUrls = array();
         }
         $existingServiceUrls = array_flip($existingServiceUrls);
         foreach ($serviceUrls as $serviceUrl) {
             if (!isset($existingServiceUrls[$serviceUrl])) {
                 // If Doctrine becomes a performance problem I could use PDO
                 // and set lucene_dirty to let that clean itself up later
                 $id = $service->getIdFromUrl($serviceUrl);
                 $info = $service->getInfo($id);
                 if (!$info) {
                     // We are not actually allowed meaningful access to this video. Password protected for example
                     continue;
                 }
                 $item = new aMediaItem();
                 $item->setTitle($info['title']);
                 // We want tags to be lower case, and slashes break routes in most server configs.
                 $info['tags'] = str_replace('/', '-', aString::strtolower($info['tags']));
                 $item->setTags($info['tags']);
                 $item->setDescription(aHtml::textToHtml($info['description']));
                 $item->setCredit($info['credit']);
                 $item->setServiceUrl($info['url']);
                 $item->setType($service->getType());
                 // The dance is this: get the thumbnail if there is one;
                 // call preSaveFile to learn the width, height and format
                 // before saving; save; and then saveFile to copy it to a
                 // filename based on the slug, which is unknown until after save
                 $thumbnail = $service->getThumbnail($id);
                 if ($thumbnail) {
                     // Grab a local copy of the thumbnail, and get the pain
                     // over with all at once in a predictable way if
                     // the service provider fails to give it to us.
                     $thumbnailCopy = aFiles::getTemporaryFilename();
                     if (copy($thumbnail, $thumbnailCopy)) {
                         $item->preSaveFile($thumbnailCopy);
                     }
                 }
                 $item->save();
                 if ($thumbnail) {
                     $item->saveFile($thumbnailCopy);
                 }
                 $item->free();
             }
         }
     }
 }
示例#3
0
 /**
  * DOCUMENT ME
  * @param sfWebRequest $request
  * @return mixed
  */
 public function executeEditVideo(sfWebRequest $request)
 {
     $this->forward404Unless(aMediaTools::userHasUploadPrivilege());
     $item = null;
     $this->slug = false;
     $this->popularTags = PluginTagTable::getPopulars(null, array('sort_by_popularity' => true), false, 10);
     if (sfConfig::get('app_a_all_tags', true)) {
         $this->allTags = PluginTagTable::getAllTagNameWithCount();
     } else {
         $this->allTags = array();
     }
     if ($request->hasParameter('slug')) {
         $item = $this->getItem();
         $this->slug = $item->getSlug();
     }
     if ($item) {
         $this->forward404Unless($item->userHasPrivilege('edit'));
     }
     $this->item = $item;
     $embed = false;
     $parameters = $request->getParameter('a_media_item');
     if ($parameters) {
         $files = $request->getFiles('a_media_item');
         $this->form = new aMediaVideoForm($item);
         if (isset($parameters['embed'])) {
             // We need to do some prevalidation of the embed code so we can prestuff the
             // file, title, tags and description widgets
             $result = $this->form->classifyEmbed($parameters['embed']);
             if (isset($result['thumbnail'])) {
                 $thumbnail = $result['thumbnail'];
                 if (!isset($parameters['title']) && !isset($parameters['tags']) && !isset($parameters['description']) && !isset($parameters['credit'])) {
                     $parameters['title'] = $result['serviceInfo']['title'];
                     // We want tags to be lower case, and slashes break routes in most server configs.
                     $parameters['tags'] = str_replace('/', '-', aString::strtolower($result['serviceInfo']['tags']));
                     $parameters['description'] = aHtml::textToHtml($result['serviceInfo']['description']);
                     $parameters['credit'] = $result['serviceInfo']['credit'];
                 }
             }
         }
         // On the first pass with a youtube video we just make the service's thumbnail the
         // default thumbnail. We don't force them to use it. This allows more code reuse
         // (Moving this after the bind is necessary to keep it from being overwritten)
         if (isset($thumbnail)) {
             // OMG file widgets can't have defaults! Ah, but our persistent file widget can
             $tmpFile = aFiles::getTemporaryFilename();
             file_put_contents($tmpFile, file_get_contents($thumbnail));
             $vfp = new aValidatorFilePersistent();
             $guid = aGuid::generate();
             $vfp->clean(array('newfile' => array('tmp_name' => $tmpFile), 'persistid' => $guid));
             // You can't mess about with widget defaults after a bind, but you
             // *can* tweak the array you're about to bind with
             $parameters['file']['persistid'] = $guid;
         }
         $this->form->bind($parameters, $files);
         do {
             // first_pass forces the user to interact with the form
             // at least once. Used when we're coming from a
             // YouTube search and we already technically have a
             // valid form but want the user to think about whether
             // the title is adequate and perhaps add a description,
             // tags, etc.
             if ($this->hasRequestParameter('first_pass') || !$this->form->isValid()) {
                 break;
             }
             $thumbnail = $this->form->getValue('file');
             // The base implementation for saving files gets confused when
             // $file is not set, a situation that our code tolerates as useful
             // because if you're updating a record containing an image you
             // often don't need to submit a new one.
             unset($this->form['file']);
             $object = $this->form->getObject();
             if ($thumbnail) {
                 $object->preSaveFile($thumbnail->getTempName());
             }
             $this->form->save();
             if ($thumbnail) {
                 $object->saveFile($thumbnail->getTempName());
             }
             return $this->redirect("aMedia/resumeWithPage");
         } while (false);
     }
 }