/[drupal]/contributions/modules/flickrup/flickrup.module
ViewVC logotype

Diff of /contributions/modules/flickrup/flickrup.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Thu Oct 25 11:20:11 2007 UTC revision 1.1.2.1, Wed Jul 30 20:19:24 2008 UTC
# Line 49  function flickrup_menu($may_cache) { Line 49  function flickrup_menu($may_cache) {
49        'type' => MENU_NORMAL_ITEM,        'type' => MENU_NORMAL_ITEM,
50        'description' => t('Change settings for the flickr upload module.')        'description' => t('Change settings for the flickr upload module.')
51      );      );
52        $items[] = array(
53          'path' => 'flickrup/js',
54          'callback' => 'flickrup_js',
55          'access' => user_access('upload photos to all nodes') || user_access('upload photos to own nodes'),
56          'type' => MENU_CALLBACK,
57        );
58    
59      // Testing      // Testing
60      /*      /*
61      $items[] = array(      $items[] = array(
# Line 100  function flickrup_admin_settings() { Line 107  function flickrup_admin_settings() {
107      '#default_value' => variable_get('flickr_global_tag', ''),      '#default_value' => variable_get('flickr_global_tag', ''),
108      '#description' => t('Enter space separated global tags to apply to all the uploads from this site.'),      '#description' => t('Enter space separated global tags to apply to all the uploads from this site.'),
109    );    );
110      $form['flickrup_remove'] = array(
111        '#type' => 'checkbox',
112        '#title' => t('Delete photos from Flickr when they are removed from a node'),
113        '#default_value' => variable_get('flickrup_remove', ''),
114      );
115    
116    // Upload options    // Upload options
117    $form['upload'] = array('#type' => 'fieldset', '#title' => t('Upload options'));    $form['upload'] = array('#type' => 'fieldset', '#title' => t('Upload options'));
118    $form['upload']['flickr_upload_fields'] = array(    $form['upload']['flickr_upload_fields'] = array(
# Line 161  function flickrup_perm() { Line 174  function flickrup_perm() {
174   * Implementation of hook_form_alter()   * Implementation of hook_form_alter()
175   */   */
176  function flickrup_form_alter($form_id, &$form) {  function flickrup_form_alter($form_id, &$form) {
177      if (isset($form['type'])) {
178        if ($form['type']['#value'] .'_node_form' == $form_id) {
179          $type = content_types($form['type']['#value']);
180          foreach ($type['fields'] as $name => $field) {
181            if ($field['type'] == 'flickrfield' && $field['widget']['type'] == 'flickrup') {
182              drupal_add_js('misc/progress.js');
183              drupal_add_js('misc/upload.js');
184              $form['#attributes']['enctype'] = 'multipart/form-data';
185            }
186          }
187        }
188      }
189    
190    if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {    if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
191      $type = $form['#node_type']->type;      $type = $form['#node_type']->type;
192    
# Line 516  function flickrup_validate_picture($file Line 542  function flickrup_validate_picture($file
542    }    }
543    
544    if (!$error) {    if (!$error) {
545      if ($file = file_save_upload($file, variable_get('flickr_picture_path', 'flickr').'/'.$file->filename, 1)) {      if ($file = file_save_upload($file, variable_get('flickr_picture_path', 'flickr').'/'.$file->filename, FILE_EXISTS_RENAME)) {
546        $form_values['files'][$field] = $file;        $form_values['files'][$field] = $file;
547        // Store in session until it has been uploaded to flickr        // Store in session until it has been uploaded to flickr
548        $_SESSION['flickr_upload'][$field] = $file;        $_SESSION['flickr_upload'][$field] = $file;
# Line 602  function flickrup_node_tags($node, $disp Line 628  function flickrup_node_tags($node, $disp
628  /**  /**
629   * Actually upload photo to flickr   * Actually upload photo to flickr
630   */   */
631  function flickrup_upload($path, $title, $tags, $description = '') {  function flickrup_upload($path, $title = '', $tags = array(), $description = '') {
632    static $flickr; // Static caching for the flickr object to support multiple uploads    static $flickr; // Static caching for the flickr object to support multiple uploads
633    
634    if (!isset($flickr)) {    if (!isset($flickr)) {
# Line 613  function flickrup_upload($path, $title, Line 639  function flickrup_upload($path, $title,
639    }    }
640    
641    // Prepare tag string    // Prepare tag string
642    $tagstring = implode(' ', $tags);    if (is_array($tags) && !empty($tags)) {
643        $tagstring = implode(' ', $tags);
644      }
645    
646    // Do sync or async uploading depending on options    // Do sync or async uploading depending on options
647    if ($async = variable_get('flickr_upload_async', 0)) {    if ($async = variable_get('flickr_upload_async', 0)) {
# Line 717  function flickrup_cleanstring($string) { Line 745  function flickrup_cleanstring($string) {
745    $output = drupal_substr($output, 0, $maxlength);    $output = drupal_substr($output, 0, $maxlength);
746    
747    return $output;    return $output;
748    }
749    
750    /**
751     * Implementation of hook_widget_info().
752     */
753    function flickrup_widget_info() {
754      return array(
755        'flickrup' => array(
756          'label' => 'Flickr Upload',
757          'field types' => array('flickrfield'),
758        ),
759      );
760    }
761    
762    /**
763     * Implementation of hook_widget().
764     */
765    function flickrup_widget($op, &$node, $field, &$items) {
766      static $submitted; // Work around for 'process form values' being called twice.
767    
768      switch ($op) {
769        case 'prepare form values':
770          _flickrup_prepare($node, $field, $items);
771          break;
772        case 'form':
773          return _flickup_widget_form($node, $field, $items);
774        case 'validate':
775          break;
776        case 'submit':
777          $submitted = TRUE; // Work around for 'process form values' being called twice.
778          break;
779        case 'process form values':
780          if ($submitted) {
781            _flickrup_process($node, $field, $items);
782          }
783          break;
784      }
785    }
786    
787    /**
788     * Implementation of hook_widget_settings().
789     */
790    function flickrup_widget_settings($op, $widget) {
791      switch ($op) {
792        case 'form':
793          $form['tags'] = array(
794            '#type' => 'textfield',
795            '#title' => t('Tags'),
796            '#description' => t('These tags will be assigned to photos uploaded using this field. Separate by spaces. This will only apply to future uploads.'),
797            '#size' => 40,
798            '#maxlength' => 255,
799            '#default_value' => $widget['tags'],
800          );
801          return $form;
802        case 'save':
803          return array('tags');
804      }
805    }
806    
807    function _flickup_widget_form($node, $field, $items) {
808      require_once(drupal_get_path('module', 'flickr') .'/flickr.inc');
809      $form['flickrup'] = array(
810        '#type' => 'fieldset',
811        '#title' => t('Flickr upload'),
812        '#collapsible' => TRUE,
813        '#description' => t('Changes are not permanent until you save this post. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('flickr_picture_dimensions', '800x600'), '%size' => variable_get('flickr_picture_file_size', '1000')))
814      );
815    
816      $form['flickrup']['fieldname'] = array(
817        '#type' => 'hidden',
818        '#value' => $field['field_name'],
819        '#name' => 'flickrup-fieldname',
820        '#id' => 'flickrup-fieldname',
821      );
822    
823      // Wrapper for fieldset contents (used by upload JS).
824      $form['flickrup']['wrapper'] = array(
825        '#prefix' => '<div id="flickrup-wrapper">',
826        '#suffix' => '</div>',
827      );
828    
829      $form['flickrup']['wrapper'] += _flickrup_form($node, $field, $items);
830    
831      return $form;
832    }
833    
834    function _flickrup_form($node, $field = NULL, $items = array()) {
835      $form['#theme'] = 'flickrup_form_new';
836    
837      if (count($items)) {
838        $form[$field['field_name']]['#theme'] = 'flickrup_form_current';
839        $form[$field['field_name']]['#tree'] = TRUE;
840        foreach ($items as $key => $item) {
841          $preview = $title = '';
842          if ($item['type'] == 'photo_id') {
843            // Remove photos that weren't found on flickr
844            if (!$photo = flickr_photo_get_info($item['id'])) {
845              unset($items[$key]);
846              break;
847            }
848            $item['title'] = $photo['title']['_content'];
849            $item['preview'] = flickr_img($photo, 's');
850            $form[$field['field_name']][$key]['flickr_photo'] = array('#type' => 'value', '#value' => $photo);
851            $form[$field['field_name']][$key]['id'] = array('#type' => 'value', '#value' => $item['id']);
852          }
853          elseif (is_object($item['file'])) {
854            $item['title'] = basename($item['file']->filename);
855            $item['preview'] = theme('image', $item['file']->filepath, '', '', array('height' => 75, 'width' => 75), FALSE);
856            $form[$field['field_name']][$key]['file'] = array('#type' => 'value', '#value' => $item['file']);
857    
858          }
859          $form[$field['field_name']][$key]['type'] = array('#type' => 'value', '#value' => 'photo_id');
860          $form[$field['field_name']][$key]['nsid'] = array('#type' => 'value', '#value' => variable_get('flickr_default_userid', ''));
861          $form[$field['field_name']][$key]['preview'] = array(
862            '#type' => 'item',
863            '#value' => $item['preview'],
864          );
865          $form[$field['field_name']][$key]['remove'] = array(
866            '#type' => 'checkbox',
867            '#default_value' => $item['remove'],
868          );
869    
870          $form[$field['field_name']][$key]['title'] = array(
871            '#type' => 'textfield',
872            '#description' => t('Link to flickr photo?'),
873            '#size' => 40,
874            '#maxlength' => 255,
875            '#default_value' => $item['title'],
876          );
877        }
878      }
879    
880      $form['new'] = array(
881        '#prefix' => '<div id="flickrup-hide">',
882        '#suffix' => '</div>',
883      );
884      $form['new']['flickrfile'] = array(
885        '#title' => t('Upload new image'),
886        '#type' => 'file',
887        '#size' => 48,
888      );
889      $form['new']['flickrup'] = array(
890        '#type' => 'button',
891        '#value' => t('Upload'),
892        '#name' => 'flickrup',
893        '#id' => 'flickrup-button',
894      );
895    
896      $form['flickrup-url'] = array('#type' => 'hidden', '#value' => url('flickrup/js', NULL, NULL, TRUE), '#attributes' => array('class' => 'upload'));
897    
898      $form['current']['vid'] = array('#type' => 'hidden', '#value' => $node->vid);
899    
900      return $form;
901    }
902    
903    /**
904     * Theme the attachment form.
905     * Note: required to output prefix/suffix.
906     */
907    function theme_flickrup_form_new($form) {
908      $output = drupal_render($form);
909      return $output;
910    }
911    
912    /**
913     * Theme current photos.
914     */
915    function theme_flickrup_form_current($form) {
916      $header = array(t('Preview'), t('Remove'), t('Title'));
917    
918      foreach (element_children($form) as $key) {
919        $row = array();
920        $row[] = drupal_render($form[$key]['preview']);
921        $row[] = drupal_render($form[$key]['remove']);
922        $row[] = drupal_render($form[$key]['title']);
923        $rows[] = $row;
924      }
925      $output = theme('table', $header, $rows);
926      $output .= drupal_render($form);
927      return $output;
928    }
929    
930    /**
931     * Save new uploads and attach them to the node object.
932     * append flickrup_previews to the node object as well.
933     */
934    function _flickrup_prepare(&$node, $field, &$items) {
935      $fieldname = $field['field_name'];
936      // Clear session variable and files that were leftover from previous uploading session
937      if(count($_POST) == 0) {
938        if (is_array($_SESSION['flickrup_previews']) && count($_SESSION['flickrup_previews'])) {
939          foreach ($_SESSION['flickrup_previews'] as $file) {
940            file_delete($file->filepath);
941          }
942          unset($_SESSION['flickrup_previews']);
943        }
944      }
945    
946      if (($file = file_check_upload('flickrfile')) && user_access('upload photos to all nodes')) {
947        $key = 'flickrup_'. count($_SESSION['flickrup_previews']);
948        $file->fid = $key;
949        flickrup_validate_picture($file, 'flickrfile');
950        $_SESSION['flickrup_previews'][$key] = $file;
951      }
952    
953      // Attach previews to node
954      if (is_array($_SESSION['flickrup_previews']) && count($_SESSION['flickrup_previews'])) {
955        foreach ($_SESSION['flickrup_previews'] as $fid => $file) {
956          if ($user->uid != 1) {
957            // Here something.php.pps becomes something.php_.pps
958            $file->filename = upload_munge_filename($file->filename, NULL, 0);
959            $file->description = $file->filename;
960          }
961          $node->{$fieldname}[$fid] = $file;
962          $items[]['file'] = $file;
963        }
964      }
965    }
966    
967    function _flickrup_process(&$node, $field, &$items) {
968      $fieldname = $field['field_name'];
969    
970      foreach ($items as $delta => $item) {
971    
972        // Remove empty items
973        if ($item['remove'] || (!$item['id'] && !is_object($item['file']))) {
974          if (variable_get('flickrup_remove', '')) {
975            $flickr = flickrup_flickrapi();
976            $flickr->photos_delete($item['id']);
977          }
978          unset($items[$delta]);
979        }
980    
981        // Process new files
982        if (is_object($item['file'])) {
983          // Tags
984          $global_tags = explode(' ', variable_get('flickr_global_tag', ''));
985          $field_tags = explode(' ', $field['widget']['tags']);
986          $tags = array_merge($global_tags, $field_tags);
987          $result = flickrup_upload($item['file']->filepath, $item['title'], $tags);
988          $items[$delta]['id'] = $result;
989        }
990        else {
991          if ($item['title'] != $item['flickr_photo']['title']['_content']) {
992            $flickr = flickrup_flickrapi();
993            $flickr->photos_setMeta($item['id'], $item['title'], $item['flickr_photo']['description']['_content']);
994          }
995        }
996      }
997    }
998    
999    function flickrup_js() {
1000      $node = $_POST;
1001      $field = content_fields($node['flickrup-fieldname']);
1002      $fieldname = $field['field_name'];
1003      $node = (object)$node;
1004    
1005      // Load node to get existing photos
1006      $node = node_load(array('vid' => $node->vid));
1007    
1008      $items = $node->$fieldname;
1009      _flickrup_prepare($node, $field, $items);
1010    
1011      $form = _flickrup_form($node, $field, $items);
1012      $form = form_builder('flickrup_js', $form);
1013      $output .= theme('status_messages') . drupal_render($form);
1014      print drupal_to_js(array('status' => TRUE, 'data' => $output));
1015      exit;
1016    }
1017    
1018    function flickrup_flickrapi() {
1019      static $flickr;
1020      require_once drupal_get_path('module', 'flickrup').'/phpFlickr/phpFlickr.php';
1021    
1022      if (!$flickr) {
1023        $flickr = new phpFlickr(trim(variable_get('flickr_api_key', '')), trim(variable_get('flickr_api_secret', '')));
1024        $flickr->setToken(trim(variable_get('flickr_api_token', '')));
1025      }
1026    
1027      return $flickr;
1028  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2