Ignore:
Timestamp:
Oct 19, 2010, 11:57:57 PM (14 years ago)
Author:
plg
Message:

feature 1941: make PY Gvideo plugin compatible with virtual categories (and remote sites as well)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/gvideo/admin/add_page.php

    r3946 r7284  
    22
    33if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
     4
     5include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
    46
    57$video_types = array('google', 'youtube', 'dailymotion', 'wideo', 'vimeo', 'wat');
     
    120122    else
    121123    {
    122       $cat = $_POST['parent'];
     124      // current date
     125      list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
     126
    123127      $video['name'] = str_replace(" ", "_", $_POST['pywaie_add_name']);
    124       $catpath = get_fulldirs(array($cat));
    125       $path_file = $catpath[$cat] . '/' . $video['name'] . '.' . $video['ext'];
    126       $thefile = substr($path_file, 2);
    127       if (file_exists($path_file))
    128       {
    129         array_push($page['errors'], sprintf(l10n('py_error6'), $thefile));
    130       }
     128      $file_name = $video['name'].'.'.$video['ext'];
     129
     130      // prepare database registration
     131      $insert = array(
     132        'file' => $file_name,
     133        'date_available' => $dbnow,
     134        );
     135     
     136      $optional_fields = array('author', 'name', 'comment');
     137      foreach ($optional_fields as $field)
     138      {
     139        if (isset($_POST[$field]) and !empty($_POST[$field]))
     140        {
     141          $insert[$field] = $_POST[$field];
     142        }
     143      }
     144     
     145      check_input_parameter('parent', $_POST, false, PATTERN_ID);
     146      $category_id = $_POST['parent'];
     147     
     148      $query = '
     149SELECT
     150    c.id,
     151    c.name,
     152    c.permalink,
     153    c.dir,
     154    c.site_id,
     155    s.galleries_url
     156  FROM '.CATEGORIES_TABLE.' AS c
     157    LEFT JOIN '.SITES_TABLE.' AS s ON s.id = c.site_id
     158  WHERE c.id = '.$category_id.'
     159;';
     160      $result = pwg_query($query);
     161      $category = pwg_db_fetch_assoc($result);
     162
     163      // is the category virtual or is the category on a remote site?
     164      $use_galleries_directory = true;
     165      if (empty($category['dir']))
     166      {
     167        $use_galleries_directory = false;
     168      }
     169      if (!empty($category['galleries_url']) and url_is_remote($category['galleries_url']))
     170      {
     171        $use_galleries_directory = false;
     172      }
     173     
     174      if (!$use_galleries_directory)
     175      {
     176        list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
     177 
     178        // upload directory hierarchy
     179        $upload_dir = sprintf(
     180          PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s',
     181          $year,
     182          $month,
     183          $day
     184          );
     185        prepare_directory($upload_dir);
     186
     187        $file_path = $upload_dir.'/'.$file_name;
     188        $thumb_path = file_path_for_type($file_path, 'thumb');
     189        $thumb_dir = dirname($thumb_path);
     190        prepare_directory($thumb_dir);
     191      }
     192      // or physical and local
    131193      else
    132194      {
    133         $file = @fopen($thefile , 'w');
    134 
    135         // Ecriture du fichier et attribution des messages
    136         if (@!fwrite ($file, stripslashes($video['id']) . '/' . $_POST['pywaie_add_h'] . '/' . $_POST['pywaie_add_w'] . '/' . $_POST['pywaie_add_start']))
     195        $catpath = get_fulldirs(array($category_id));
     196        $file_path = $catpath[$category_id].'/'.$file_name;
     197
     198        $insert['storage_category_id'] = $category_id;
     199      }
     200
     201      $insert['path'] = $file_path;
     202     
     203      if (file_exists($file_path))
     204      {
     205        array_push($page['errors'], sprintf(l10n('py_error6'), $file_path));
     206      }
     207      else
     208      {
     209        // Write fake file with video settings inside
     210        //
     211        // TODO: store these information in a specific database table instead
     212        $file_content = stripslashes($video['id']);
     213        $file_content.= '/'.$_POST['pywaie_add_h'];
     214        $file_content.= '/'.$_POST['pywaie_add_w'];
     215        $file_content.= '/'.$_POST['pywaie_add_start'];
     216
     217        $bytes_written = file_put_contents($file_path, $file_content);       
     218        if (false === $bytes_written)
    137219        {
    138           array_push($page['errors'], sprintf(l10n('py_error7'), $thefile), sprintf(l10n('py_error8'), $catpath[$cat]));
     220          array_push(
     221            $page['errors'],
     222           
     223            sprintf(
     224              l10n('py_error7'),
     225              $file_path
     226              ),
     227           
     228            sprintf(
     229              l10n('py_error8'),
     230              dirname($file_path)
     231              )
     232            );
     233
    139234        }
    140235        else
    141236        {
    142           // Miniatures
    143           $thumb_extension = 'NULL';
     237          // thumbnail
     238          $thumb_extension = null;
    144239          if ($_POST['thumbnail'] == 'thumb_from_server' or $_POST['thumbnail'] == 'thumb_from_user')
    145240          {
    146                                           include_once ('thumbnails.php');
     241            include_once ('thumbnails.php');
     242            $insert['tn_ext'] = $thumb_extension;
    147243          }
     244
     245          // database registration
     246          mass_inserts(
     247            IMAGES_TABLE,
     248            array_keys($insert),
     249            array($insert)
     250            );
    148251         
    149           // Synchronisation avec la base de donnees
    150           $infos['name'] = (!empty($_POST['name']) ? '"' . $_POST['name'] . '"' : 'NULL');
    151           $infos['description'] = (!empty($_POST['description']) ? '"' . $_POST['description'] . '"' : 'NULL');
    152           $infos['author'] = (!empty($_POST['author']) ? '"' . $_POST['author'] . '"' : 'NULL');
    153 
    154           $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id  FROM ' . IMAGES_TABLE . ' ;';
    155           list($next_element_id) = mysql_fetch_array(pwg_query($query));
    156 
    157           pwg_query('INSERT INTO ' . IMAGES_TABLE . ' ( id , file , date_available , date_creation , tn_ext , name , comment , author , hit , filesize , width , height , representative_ext , date_metadata_update , average_rate , has_high , path , storage_category_id , high_filesize )
    158                                           VALUES ( ' . $next_element_id . ', "' . $video['name'] . '.' . $video['ext'] . '", NOW() , NULL , ' . $thumb_extension . ' ,  ' . $infos['name'] . ' , ' . $infos['description'] . ' , ' . $infos['author'] . ' , 0 , NULL , NULL , NULL , NULL , NULL , NULL , NULL , "' . $path_file . '", ' . $cat . ', NULL);');
    159           pwg_query('INSERT INTO ' . IMAGE_CATEGORY_TABLE . ' ( image_id , category_id )
    160                                           VALUES ( ' . $next_element_id . ', ' . $cat . ');');
    161 
    162           $query = 'SELECT representative_picture_id FROM ' . CATEGORIES_TABLE . ' WHERE id =' .  $cat . ';';
    163           list($result) = mysql_fetch_array(pwg_query($query));
    164           if ($result === NULL)
    165           {
    166             pwg_query('UPDATE ' . CATEGORIES_TABLE . ' SET representative_picture_id=' . $next_element_id . ' WHERE id = ' . $cat . ' LIMIT 1');
    167           }
    168 
     252          $image_id = pwg_db_insert_id(IMAGES_TABLE);
     253          associate_images_to_categories(array($image_id), array($category_id));
    169254          invalidate_user_cache();
    170           array_unshift($page['infos'], sprintf(l10n('py_info3'), $thefile));
    171           array_push($page['infos'], sprintf(l10n('py_show_file'), PHPWG_ROOT_PATH . 'picture.php?/' . $next_element_id . '/category/' . $cat));
    172           @fclose($file);
     255
     256          // success information to display
     257          array_unshift($page['infos'], sprintf(l10n('py_info3'), $file_path));
     258         
     259          array_push(
     260            $page['infos'],
     261            sprintf(
     262              l10n('py_show_file'),
     263              make_picture_url(
     264                array(
     265                  'image_id' => $image_id,
     266                  'section' => 'categories',
     267                  'category' => $category,
     268                  )
     269                )
     270              )
     271            );
    173272        }
    174273      }
     
    178277
    179278
    180 // Affichage de la liste des categories
    181 $site_locaux = array();
     279// display list of all categories
    182280$query = '
    183 SELECT id , galleries_url
    184 FROM ' . SITES_TABLE . '
    185 ORDER by id';
    186 $result = pwg_query($query);
    187 
    188 if (mysql_num_rows($result) > 0)
    189 {
    190   while (list($id , $galleries_url) = mysql_fetch_row($result))
    191   {
    192     if (!url_is_remote($galleries_url)) array_push($site_locaux , $id);
    193   }
    194 }
    195 if (empty($site_locaux))
    196 {
    197   array_push($page['errors'], l10n('py_error1'));
    198   $site_locaux = array(0);
    199 }
    200 
    201 $query = '
    202 SELECT id,name,uppercats,global_rank
    203   FROM ' . CATEGORIES_TABLE . '
    204   WHERE site_id IN (' . implode("," , $site_locaux) . ');';
    205 
    206 if (isset($_POST['parent'])) $selected = array($_POST['parent']);
    207 else $selected = array();
     281SELECT
     282    id,
     283    name,
     284    uppercats,
     285    global_rank
     286  FROM '.CATEGORIES_TABLE.'
     287;';
     288
     289if (isset($_POST['parent']))
     290{
     291  $selected = array($_POST['parent']);
     292}
     293else
     294{
     295  $selected = array();
     296}
    208297
    209298display_select_cat_wrapper($query, $selected , 'category_option_parent', false);
     
    224313    'NAME' => $_POST['name'],
    225314    'AUTHOR' => $_POST['author'],
    226     'DESCRIPTION' => $_POST['description']));
     315    'COMMENT' => $_POST['comment']));
    227316}
    228317else
Note: See TracChangeset for help on using the changeset viewer.