wordsonline_connector-1.0.x-dev/wordsonline_connector.install

wordsonline_connector.install
<?php

/**
 * @file
 * Install, update and uninstall functions for the WordsOnline Connector.
 */

use Drupal;
use Drupal\Core\Database\Database;

/**
 * Schema of wordsonline_connector.
 */
function wordsonline_connector_schema() {
  $schema['wordsonline_connector_jobs'] = [
    'description' => 'Stores value in tmgmt wordsonline jobs table',
    'fields' => [
      'job_id' => [
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique id for wordsonline_connector_jobs',
      ],
      'file_guid' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'file guid',
      ],
      'request_guid' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Request Guid',
      ],
      'request_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Request Guid',
      ],
      'project_guid' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Project Guid',
      ],
      'status' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Status',
      ],
    ],
    'primary key' => ['job_id'],
  ];

  $schema['wordsonline_connector_configs'] = [
    'description' => 'Stores value in tmgmt wordsonline jobs table',
    'fields' => [
      'config_id' => [
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique id for wordsonline_connector_configs',
      ],
      'api_url' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Api Url',
      ],
      'username' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'UserName',
      ],
      'password' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Password',
      ],
      'scope' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Scope',
      ],
      'grant_type' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'grant_type',
      ],
      'project_key' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Project Guid',
      ],
    ],
    'primary key' => ['config_id'],
  ];
  $schema['wordsonline_connector_support_languges'] = [
    'description' => 'Stores value in tmgmt wordsonline jobs table',
    'fields' => [
      'code' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique id for wordsonline_connector_support_languges',
      ],
      'lang_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'language name',
      ],
      'wol_code' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'language name',
      ],
    ],
    'primary key' => ['code'],
  ];
  $schema['wordsonline_connector_job_files'] = [
    'description' => 'Stores value in tmgmt wordsonline jobs files table',
    'fields' => [
      'job_id' => [
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique id for wordsonline_connector_job_files',
      ],
      'job_Item_id' => [
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique id for wordsonline_connector_job_files',
      ],
      'file_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'file name',
      ],
      'file_path' => [
        'type' => 'text',
        'description' => 'file data',
      ],
      'source_language' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'file name',
      ],
      'target_language' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'file name',
      ],
    ],
    'primary key' => ['job_id', 'job_Item_id'],
  ];
  return $schema;
}

/**
 * Handle wordsonline_connector install.
 */
function wordsonline_connector_install() {
  $fields = [
    'code',
    'lang_name',
    'wol_code',
  ];
  $npath = \Drupal::service('extension.list.module')->getPath('wordsonline_connector');
  // $import = \Drupal::service('file_url_generator')->generateAbsoluteString($npath . '/js/languages.json');
  $import = \Drupal::service('file_system')->realpath($npath . '/js/languages.json');
  $content = file_get_contents($import);
  $langs = json_decode($content);
  $values=[];
  $conn = Database::getConnection();
  foreach ($langs as $lang) {
    $is_exists = false;
    foreach ($values as $record) {
      if($record['code']==$lang->code){
        $is_exists = true;
        break;
      }
    }
    if($is_exists == true){
      continue;
    }
    $results = $conn
            ->query(
                "SELECT code,lang_name,wol_code
                FROM wordsonline_connector_support_languges
                WHERE code =:code  LIMIT 1 ",
                [":code" => (string) $lang->code]
            )
            ->fetchAssoc();
    if ($results != NULL) {
      continue;
    }
    $values[] = [
      'code' => $lang->code,
      'lang_name' => $lang->lang_name,
      'wol_code' => $lang->wol_code,
    ];
  }

  $query = $conn->insert('wordsonline_connector_support_languges')->fields($fields);
  foreach ($values as $record) {
    $query->values($record);
  }
  $query->execute();
}

/**
 * Updates the schema for wordsonline_connector_jobs table.
 */
function wordsonline_connector_update_8001()
{
  $connection = Database::getConnection();
  $schema = $connection->schema();
  $table = 'wordsonline_connector_jobs';

  // Remove file_guid column
  if ($schema->fieldExists($table, 'file_guid')) {
    $schema->dropField($table, 'file_guid');
  }

  // Add type column (tinyint)
  if (!$schema->fieldExists($table, 'type')) {
    $schema->addField($table, 'type', [
      'type' => 'int',
      'size' => 'tiny',
      'not null' => TRUE,
      'default' => 0,
      'description' => 'Type of job',
    ]);
  }

  // Add id column , primary
  if (!$schema->fieldExists($table, 'id')) {
    $schema->dropPrimaryKey($table);
    $connection->query("ALTER TABLE {$table} ADD id INT auto_increment PRIMARY KEY FIRST");
  }

  // Add type index
  if (!$schema->indexExists($table, 'job_type_idx')) {
    $schema->addIndex($table, 'job_type_idx', ['type'], [
      'fields'=>[
        'type'=>['type' => 'tinyint'],
      ],
    ]);
  }

  // Add job_id index
  if (!$schema->indexExists($table, 'job_id_idx')) {
    $schema->addIndex($table, 'job_id_idx', ['job_id'], [
      'fields'=>[
        'job_id'=>['type' => 'int', 'length' => 11],
      ],
    ]);
  }

  // Add request_guid index
  if (!$schema->indexExists($table, 'request_guid_idx')) {
    $schema->addIndex($table, 'request_guid_idx', ['request_guid'], [
      'fields'=>[
        'request_guid'=>['type' => 'varchar', 'length' => 255, 'not null' => FALSE],
      ],
    ]);
  }

  // Add created column (timestamp)
  if (!$schema->fieldExists($table, 'created')) {
    $schema->addField($table, 'created', [
      'type' => 'int',
      'length' => '11',
      'not null' => TRUE,
      'default' => 0,
      'description' => 'Timestamp when job was created',
    ]);
    // try to set job created time
    foreach($connection->select($table)->fields($table, ['job_id'])->execute()->fetchCol() as $job_id) {
      try{
        $job = \Drupal\tmgmt\Entity\Job::load($job_id);
        $created = 0;
        if ($job) {
          $created = $job->created->value;
          if ($created) {
            $query = $connection->update($table)
              ->fields(['created' => $created])
              ->condition('job_id', $job_id)
              ->execute();
          }
        }

      } catch (\Exception $e) {
        \Drupal::logger('wordsonline_connector')->error('Error updating created time for job ID @job_id: @message', [
          '@job_id' => $job_id,
          '@message' => $e->getMessage(),
        ]);
      }
    }
  }
}

/**
 * Create the wordsonline_connector_job_items table and migrate the files data.
 */
function wordsonline_connector_update_8002()
{
  $connection = Database::getConnection();
  $schema = $connection->schema();
  $table = 'wordsonline_connector_jobs_items';
  $file_table = 'wordsonline_connector_job_files';

  // Rename the file table if it exists
  if ($schema->tableExists($file_table)) {
    $schema->renameTable($file_table, $table);
  }

  // Add wol_job_id column
  if (!$schema->fieldExists($table, 'wol_job_id')) {
    $schema->addField($table, 'wol_job_id', [
      'type' => 'int',
      'length' => '11',
      'not null' => TRUE,
      'default' => 0,
      'description' => 'WordsOnline connector job id',
    ]);
  }

  // Add job_id & job_item_id index
  if (!$schema->indexExists($table, 'job_id_job_item_id_idx')) {
    $schema->addIndex($table, 'job_id_job_item_id_idx', ['job_id', 'job_Item_id'], [
      'fields'=>[
        'job_id'=>['type' => 'int', 'length' => 11],
        'job_Item_id'=>['type' => 'int', 'length' => 11],
      ],
    ]);
  }

  // Add wol_job_id index
  if (!$schema->indexExists($table, 'wol_job_id_idx')) {
    $schema->addIndex($table, 'wol_job_id_idx', ['wol_job_id'], [
      'fields'=>[
        'wol_job_id'=>['type' => 'int', 'length' => 11],
      ],
    ]);
  }

  // Add updated column
  if (!$schema->fieldExists($table, 'updated')) {
      $schema->addField($table, 'updated', [
        'type' => 'int',
        'length' => '11',
        'default' => 0,
        'description' => 'last updated time',
      ]);
  }

  // Add updated index
  if (!$schema->indexExists($table, 'updated_idx')) {
    $schema->addIndex($table, 'updated_idx', ['updated'], [
      'fields'=>[
        'updated'=>['type' => 'int', 'length' => 11, 'not null' => FALSE],
      ],
    ]);
  }

  // Add revision_id column
  if (!$schema->fieldExists($table, 'revision_id')) {
    $schema->addField($table, 'revision_id', [
      'type' => 'int',
      'length' => '10',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
      'description' => 'entity revision id',
    ]);
  }

   // Add id column, primary
   if (!$schema->fieldExists($table, 'id')) {
    $schema->dropPrimaryKey($table);
    $connection->query("ALTER TABLE {$table} ADD id INT auto_increment PRIMARY KEY FIRST");
  }

  $job_table = 'wordsonline_connector_jobs';
  $job_data = $connection->select($job_table)
    ->fields($job_table, ['id', 'job_id', 'created'])
    ->execute()
    ->fetchAllAssoc('job_id');
  // try to migrate the files data
  foreach($connection->select($table)->fields($table)->execute()->fetchAll() as $item) {
    try{
      $updated = 0;
      $wol_job_id = 0;
      if(!empty($item->job_Item_id)){
        $job_item = \Drupal\tmgmt\Entity\JobItem::load($item->job_Item_id);
        if ($job_item) {
          $updated = $job_item->changed->value;
        }
      }
      if(isset($job_data[$item->job_id])){
        $wol_job_id = $job_data[$item->job_id]->id;
      }

      $connection->update($table)
        ->fields(['updated' => $updated, 'wol_job_id' => $wol_job_id])
        ->condition('id', $item->id)
        ->execute();
    } catch (\Exception $e) {
      \Drupal::logger('wordsonline_connector')->error('Error updating WOL job items for Id @id: @message', [
        '@id' => $item->id,
        '@message' => $e->getMessage(),
      ]);
    }
  }
}

/**
 * Create the wordsonline_connector_pending_job_items table.
 */
function wordsonline_connector_update_8003()
{
  $table = 'wordsonline_connector_pending_job_items';
  if(\Drupal::database()->schema()->tableExists($table)) {
    return;
  }
  $schema = [
    'fields' => [
      'id' => [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'job_id' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'length' => '10',
        'not null' => TRUE,
      ],
      'plugin' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'item_type' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'item_id' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'is_processed' => [
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'job_item_id' => [
        'type' => 'int',
        'length' => '10',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'created' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
    ],
    'primary key' => ['id'],
    'indexes' => [
      'job_id_idx' => ['job_id'],
      'jobid_processed_idx' => ['job_id', 'is_processed'],
      'plugin_itemtype_itemid_idx' => ['plugin', 'item_type', 'item_id'],
    ],
  ];

  \Drupal::database()->schema()->createTable($table, $schema);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc