Created
March 2, 2017 18:32
-
-
Save cyberlex404/ef2ecaaa345c6a23490f338f5a19a465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\gallery_port\Form; | |
| use Drupal\Core\File\FileSystem; | |
| use Drupal\Core\Form\FormBase; | |
| use Drupal\Core\Form\FormStateInterface; | |
| use Drupal\file\Entity\File; | |
| use Drupal\gallery\Entity\PhotoGallery; | |
| use GuzzleHttp\Client; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| use Drupal\Core\Entity\EntityTypeManager; | |
| use Drupal\Core\Entity\Query\QueryFactory; | |
| use Drupal\Core\Session\AccountProxy; | |
| use Drupal\Core\Database\Database; | |
| use Throwable; | |
| /** | |
| * Class DefaultForm. | |
| * | |
| * @package Drupal\gallery_port\Form | |
| */ | |
| class GalleryPort extends FormBase { | |
| const DB_SOURCE_NAME = 'user1106073_tradiciya_prod'; | |
| const DB_SOURCE_USER = 'tradiciya'; | |
| const DB_SOURCE_PASS = 'EbmRn8Nw'; | |
| const PHOTO_TARGET = 'sites/default/files/gallery/photos/'; | |
| const SOURCE_PATH = 'http://klevo.by/sites/default/files/photos/'; | |
| /** | |
| * Drupal\Core\Entity\EntityTypeManager definition. | |
| * | |
| * @var \Drupal\Core\Entity\EntityTypeManager | |
| */ | |
| protected $entityTypeManager; | |
| /** | |
| * Drupal\Core\Entity\Query\QueryFactory definition. | |
| * | |
| * @var \Drupal\Core\Entity\Query\QueryFactory | |
| */ | |
| protected $entityQuery; | |
| /** | |
| * Drupal\Core\Session\AccountProxy definition. | |
| * | |
| * @var \Drupal\Core\Session\AccountProxy | |
| */ | |
| protected $currentUser; | |
| public function __construct( | |
| EntityTypeManager $entity_type_manager, | |
| QueryFactory $entity_query, | |
| AccountProxy $current_user | |
| ) { | |
| $this->entityTypeManager = $entity_type_manager; | |
| $this->entityQuery = $entity_query; | |
| $this->currentUser = $current_user; | |
| } | |
| public static function create(ContainerInterface $container) { | |
| return new static( | |
| $container->get('entity_type.manager'), | |
| $container->get('entity.query'), | |
| $container->get('current_user') | |
| ); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getFormId() { | |
| return 'default_form'; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function buildForm(array $form, FormStateInterface $form_state) { | |
| dpm(self::DB_SOURCE_NAME); | |
| $form['submit'] = [ | |
| '#type' => 'submit', | |
| '#value' => $this->t('Submit'), | |
| ]; | |
| return $form; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function validateForm(array &$form, FormStateInterface $form_state) { | |
| parent::validateForm($form, $form_state); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function submitForm(array &$form, FormStateInterface $form_state) { | |
| $gallery = $this->getGalleryList(); | |
| dpm($gallery); | |
| $folder = \Drupal::service('file_system')->mkdir(self::PHOTO_TARGET); | |
| dpm($folder); | |
| // Prepare batch operations array. | |
| foreach ($gallery as $item) { | |
| if ($item instanceof \stdClass) { | |
| $operations[] = array( | |
| 'Drupal\gallery_port\Form\GalleryPort::execute', | |
| array($item), | |
| ); | |
| } | |
| } | |
| if (!empty($operations)) { | |
| $batch = array( | |
| 'operations' => $operations, | |
| 'title' => t('Update housing nodes'), | |
| 'init_message' => t('Job housing is starting.'), | |
| 'progress_message' => t('Processed @current out of @total.'), | |
| 'error_message' => t('error.'), | |
| 'finished' => 'Drupal\gallery_port\Form\GalleryPort::finishedCallback', | |
| ); | |
| batch_set($batch); | |
| } | |
| else { | |
| drupal_set_message($this->t('No data for job')); | |
| } | |
| } | |
| protected function getGalleryList() { | |
| $list = []; | |
| self::MigrateDB(); | |
| $result = db_select('taxonomy_term_data', 'g'); | |
| $result->fields('g'); | |
| $result->condition('g.vid', '6'); | |
| $list = $result->execute()->fetchAllAssoc('tid'); | |
| self::MigrateDB(FALSE); | |
| return $list; | |
| } | |
| protected function getPhotos(\stdClass $gallery) { | |
| $photos = []; | |
| // user1106073_vival.field_data_album user1106073_vival.field_data_album.album_target_id | |
| // user1106073_vival.field_data_photo | |
| // user1106073_vival.file_managed | |
| $alb_field = 'photo_tag'; | |
| self::MigrateDB(); | |
| $result = db_select('node', 'n'); | |
| $result->leftJoin('field_data_photo_tag', 'a', 'n.nid = a.entity_id'); | |
| $result->leftJoin('field_data_photo', 'p', 'n.nid = p.entity_id'); | |
| $result->leftJoin('file_managed', 'f', 'p.photo_fid = f.fid'); | |
| $result->fields('a', ['entity_id']); | |
| //$result->fields('f'); | |
| $result->fields('n', ['nid', 'title']); | |
| $result->addField('p','photo_fid', 'photo_fid'); | |
| $result->addField('f','uri', 'uri'); | |
| $result->addField('f','filename', 'filename'); | |
| $result->condition('a.photo_tag_tid', $gallery->tid); | |
| $photos = $result->execute()->fetchAllAssoc('nid'); | |
| self::MigrateDB(FALSE); | |
| return $photos; | |
| } | |
| private function guzzleLoad(\stdClass $photo) { | |
| // dpm($photo, $target); | |
| $target = self::PHOTO_TARGET . $photo->filename; | |
| $source = self::SOURCE_PATH . $photo->filename; | |
| $client = new Client(); | |
| $resource = fopen($target, 'w'); | |
| $client->request('GET', $source, ['sink' => $resource]); | |
| $params = new \stdClass(); | |
| $params->filename = $photo->filename; | |
| $params->uri = 'public://gallery/photos/' . $photo->filename; | |
| $file = self::fileSave($params, $target); | |
| return $file; | |
| } | |
| /** | |
| * @param $params | |
| * @param $path | |
| * @return \Drupal\Core\Entity\EntityInterface|static | |
| */ | |
| private function fileSave($params, $path) { | |
| if(file_exists($path)) { | |
| $file = File::create([ | |
| 'uid' => 1, | |
| 'filename' => $params->filename, | |
| 'uri' => $params->uri, | |
| 'status' => 1, | |
| ]); | |
| $file->setPermanent(); | |
| $file->save(); | |
| return $file; | |
| } | |
| } | |
| protected static function MigrateDB($db = TRUE) { | |
| $other_database = array( | |
| 'database' => self::DB_SOURCE_NAME, | |
| 'username' => self::DB_SOURCE_USER, | |
| 'password' => self::DB_SOURCE_PASS, | |
| 'host' => '178.124.130.230', | |
| 'driver' => 'mysql', | |
| ); | |
| if($db) { | |
| try { | |
| Database::addConnectionInfo('other', 'default', $other_database); | |
| Database::setActiveConnection('other'); | |
| // Code that may throw an Exception or Error. | |
| } catch (Throwable $t) { | |
| drupal_set_message($t->getMessage()); | |
| // Handle exception | |
| } | |
| }else { | |
| Database::setActiveConnection(); | |
| } | |
| } | |
| public static function execute(\stdClass $gallery, &$context) { | |
| $message = $gallery->tid; | |
| $photoGallery = PhotoGallery::create([ | |
| 'name' => $gallery->name, | |
| ]); | |
| $ga = $photoGallery->toArray(); | |
| $photos = self::getPhotos($gallery); | |
| $files = []; | |
| foreach ($photos as $item) { | |
| $files[] = self::guzzleLoad($item); | |
| } | |
| $photoGallery->setPhotos($files); | |
| $photoGallery->save(); | |
| $context['message'] = $message; | |
| $context['results']['items'][] = [ | |
| 'id' => $gallery->tid, | |
| 'ga' => $ga, | |
| 'photos' => $photos, | |
| // 'alias' => $root_alias, | |
| // 'oid' => $oid, | |
| // 'values' => $values, | |
| // 'items' => $items, | |
| ]; | |
| } | |
| public static function finishedCallback($success, $results, $operations) { | |
| // The 'success' parameter means no fatal PHP errors were detected. All | |
| // other error management should be handled using 'results'. | |
| if ($success) { | |
| dpm($results, 'result'); | |
| $message = \Drupal::translation()->formatPlural( | |
| count($results['items']), | |
| 'One post processed.', '@count posts processed.' | |
| ); | |
| } | |
| else { | |
| $message = t('Finished with an error.'); | |
| } | |
| drupal_set_message($message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment