/lib/task/carpetbeggersFlickrimportTask.class.php

https://github.com/staunchRobots/Symfony-Groovy-Ecom · PHP · 110 lines · 76 code · 32 blank · 2 comment · 2 complexity · 70dfd29a95fcf5a928c83360793fac91 MD5 · raw file

  1. <?php
  2. class carpetbeggersFlickrimportTask extends sfBaseTask
  3. {
  4. protected function configure()
  5. {
  6. $this->addArguments(array(
  7. new sfCommandArgument('userId', sfCommandArgument::REQUIRED, 'User Id'),
  8. new sfCommandArgument('setId', sfCommandArgument::REQUIRED, 'Set Id'),
  9. new sfCommandArgument('id', sfCommandArgument::REQUIRED, 'Unique Id'),
  10. new sfCommandArgument('category', sfCommandArgument::REQUIRED, 'Category Id'),
  11. ));
  12. $this->addOptions(array(
  13. new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
  14. new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
  15. new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
  16. // add your own options here
  17. ));
  18. $this->namespace = 'carpetbeggers';
  19. $this->name = 'flickr-import';
  20. $this->briefDescription = '';
  21. $this->detailedDescription = <<<EOF
  22. The [carpetbeggers:flickr-import|INFO] task does things.
  23. Call it with:
  24. [php symfony carpetbeggers:flickr-import|INFO]
  25. EOF;
  26. }
  27. protected function execute($arguments = array(), $options = array())
  28. {
  29. ini_set('memory_limit', '512M');
  30. $exec = 'touch ' . sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $arguments['id'] . '.lock';
  31. system($exec);
  32. require_once(dirname(__FILE__).'/../../config/ProjectConfiguration.class.php');
  33. $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true);
  34. sfContext::createInstance($configuration);
  35. $databaseManager = new sfDatabaseManager($this->configuration);
  36. $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
  37. $category = Doctrine::getTable('Category')->find($arguments['category']);
  38. $flickr = new sfFlickr;
  39. $setCount = $flickr->getPhotoset($arguments['setId'])->getPhotoCount();
  40. $photos = $flickr->getPhotoset($arguments['setId'])->getPhotoList()->getPhotos();
  41. $i = 0;
  42. foreach ($photos as $photo)
  43. {
  44. // if ($i == 1) break;
  45. $product = ProductTable::getInstance()->findOneByFlickrId($photo->getId());
  46. $i++;
  47. $sizes = $photo->getSizes();
  48. $filehash = sha1($photo->getId());
  49. $extension = $sizes['o']['type'];
  50. $filename1 = $filehash.'.'.$extension;
  51. $filename2 = $filehash.'_original.'.$extension;
  52. $photo->saveAs(sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $filename1, 'o');
  53. $photo->saveAs(sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $filename2, 'o');
  54. if (!$product)
  55. {
  56. $product = new Product;
  57. }
  58. $product->setName($photo->getTitle());
  59. $product->setFlickrId($photo->getId());
  60. $product->setNotes($photo->getDescription());
  61. $product->setCreatedAt($photo->getPostedDate());
  62. $product->setStatus('incomplete');
  63. $dimensions = @getimagesize(sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $filename1);
  64. $product->setPhoto($filename1);
  65. $product->updatePhoto();
  66. $productCategory = Doctrine::getTable('ProductCategory')->find(array($product->getId(), $category->getId()));
  67. if (!$productCategory)
  68. {
  69. $productCategory = new ProductCategory;
  70. }
  71. $productCategory->setProductId($product->getId());
  72. $productCategory->setCategoryId($category->getId());
  73. $productCategory->save();
  74. echo ".";
  75. }
  76. unlink('data/' . $arguments['id'] . '.lock');
  77. }
  78. }