You are here

function asset_youtube_asset_type in Asset 6

Same name and namespace in other branches
  1. 5 asset_youtube/asset_youtube.module \asset_youtube_asset_type()

File

asset_youtube/asset_youtube.module, line 40

Code

function asset_youtube_asset_type($op = 'info', $delta = 0, $form_values = array()) {
  switch ($op) {
    case 'info':
      return array(
        'embed' => array(
          'value' => t('YouTube Embed'),
          'title' => t('Use an existing YouTube video.'),
          'src' => drupal_get_path('module', 'asset_youtube') . '/misc/red_y.gif',
        ),
        'sync' => array(
          'value' => t('YouTube Sync'),
          'title' => t('Sync up with your YouTube Account.'),
          'src' => drupal_get_path('module', 'asset_youtube') . '/misc/red_y.gif',
        ),
      );
    case 'form':
      switch ($delta) {
        case 'embed':
          $form['parent'] = array(
            '#type' => 'hidden',
            '#value' => $form_values['parent'] ? $form_values['parent'] : $_GET['dir'],
          );
          $form[] = array(
            '#type' => 'item',
            '#title' => 'Directory',
            '#value' => str_replace(file_directory_path(), 'assets', $form['parent']['#value']),
            '#prefix' => '<div class="container-inline">',
            '#suffix' => '</div>',
          );
          $form['embed'] = array(
            '#type' => 'textarea',
            '#title' => t('YouTube Embed Code'),
            '#rows' => 3,
          );
          $form['title'] = array(
            '#type' => 'textfield',
            '#title' => t('Title'),
          );
          $form['author'] = array(
            '#type' => 'textfield',
            '#title' => t('Author'),
          );
          $form['description'] = array(
            '#type' => 'textarea',
            '#title' => t('Description'),
            '#rows' => 3,
          );
          $form['status'] = array(
            '#type' => 'radios',
            '#title' => t('Status'),
            '#required' => true,
            '#default_value' => ASSET_PUBLIC,
            '#options' => array(
              ASSET_PRIVATE => t('Private'),
              ASSET_PUBLIC => t('Public'),
            ),
          );
          break;
        case 'sync':
          global $user;
          if ($user->asset_youtube['username']) {
            $form['help'] = array(
              '#value' => '<p>Click next to import your YouTube Videos and Favorites.</p>',
            );
          }
          else {
            $form['help'] = array(
              '#value' => '<p>Please provide you YouTube user name and click next to import your YouTube Videos and Favorites.</p>',
            );

            // match user edit form structure
            $form['asset_youtube']['#tree'] = true;
            $form['asset_youtube']['username'] = array(
              '#type' => 'textfield',
              '#title' => t('YouTube user name'),
            );
          }
          break;
      }
      return $form;
    case 'validate':

      // return a valid asset aid
      switch ($delta) {
        case 'embed':
          if ($ytid = asset_youtube_extract_id($form_values['embed'])) {
            $path = $form_values['parent'] . '/' . $ytid . '.youtube';
            $asset->filepath = $path;
            $asset->status = $form_values['status'];
            $asset->title = $form_values['title'];
            $asset->author = $form_values['author'];
            $asset->description = $form_values['description'];
            $asset->type = 'youtube';
            $asset = asset_save($asset);
            return $asset->aid;
          }
          else {
            form_set_error('embed', 'Error extracting YouTube video ID.</em>');
          }
          break;
        case 'sync':
          global $user;
          if ($form_values['asset_youtube']['username']) {
            asset_youtube_user('update', $form_values, $user);
            $user = user_load(array(
              'uid' => $user->uid,
            ));
          }
          asset_youtube_sync();
          $query = $_GET;
          unset($query['q']);
          $query['dir'] = $user->name . '/My YouTube';
          drupal_goto($_GET['q'], asset_build_query($query));
          break;
      }
      break;
    case 'submit':

      // must return a valid asset aid
      switch ($delta) {
      }
      break;
  }
}