diff --git a/instagram_social_feed.install b/instagram_social_feed.install index 4178447..6191d71 100644 --- a/instagram_social_feed.install +++ b/instagram_social_feed.install @@ -73,6 +73,20 @@ function instagram_social_feed_schema() { 'default' => '', 'description' => 'Low resolution photo.', ), + 'low_resolution_width' => array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Width of low resolution photo.', + ), + 'low_resolution_height' => array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Height of low resolution photo.', + ), 'thumbnail' => array( 'type' => 'varchar', 'length' => 255, @@ -80,6 +94,20 @@ function instagram_social_feed_schema() { 'default' => '', 'description' => 'Photo thumbnail.', ), + 'thumbnail_width' => array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Width of thumbnail photo.', + ), + 'thumbnail_height' => array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Height of thumbnail photo.', + ), 'standard_resolution' => array( 'type' => 'varchar', 'length' => 255, @@ -87,6 +115,20 @@ function instagram_social_feed_schema() { 'default' => '', 'description' => 'Standard photo resolution', ), + 'standard_resolution_width' => array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Width of standard resolution photo.', + ), + 'standard_resolution_height' => array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Height of standard resolution photo.', + ), 'caption' => array( 'type' => 'blob', 'size' => 'normal', @@ -266,3 +308,63 @@ function instagram_social_feed_update_7104() { db_add_field('instagram_social_feeds', 'user_id', array('type' => 'varchar', 'length' => 255, 'default' => '', 'description' => 'User ID')); } } + +/** + * Add width, height columns for photos to instagram_social_feed_photos table. + */ +function instagram_social_feed_update_7105() { + $spec = array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Width of low resolution photo.', + ); + db_add_field( 'instagram_social_feed_photos', 'low_resolution_width', $spec); + + $spec = array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Height of low resolution photo.', + ); + db_add_field( 'instagram_social_feed_photos', 'low_resolution_height', $spec); + + $spec = array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Width of thumbnail photo.', + ); + db_add_field( 'instagram_social_feed_photos', 'thumbnail_width', $spec); + + $spec = array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Height of thumbnail photo.', + ); + db_add_field( 'instagram_social_feed_photos', 'thumbnail_height', $spec); + + $spec = array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Width of standard resolution photo.', + ); + db_add_field( 'instagram_social_feed_photos', 'standard_resolution_width', $spec); + + $spec = array( + 'type' => 'varchar', + 'length' => 25, + 'not null' => TRUE, + 'default' => '', + 'description' => 'Height of standard resolution photo.', + ); + db_add_field( 'instagram_social_feed_photos', 'standard_resolution_height', $spec); + +} diff --git a/instagram_social_feed.module b/instagram_social_feed.module index 1d6ce2b..341d887 100644 --- a/instagram_social_feed.module +++ b/instagram_social_feed.module @@ -196,8 +196,14 @@ function instagram_social_feed_cron() { // Time stored in unix epoch format. 'created_time' => $feed->created_time, 'low_resolution' => $low_resolution, + 'low_resolution_width' => $feed->images->low_resolution->width, + 'low_resolution_height' => $feed->images->low_resolution->height, 'thumbnail' => $thumbnail, + 'thumbnail_width' => $feed->images->thumbnail->width, + 'thumbnail_height' => $feed->images->thumbnail->height, 'standard_resolution' => $standard_resolution, + 'standard_resolution_width' => $feed->images->standard_resolution->width, + 'standard_resolution_height' => $feed->images->standard_resolution->height, 'caption' => filter_xss(utf8_encode($caption)), 'instagram_id' => $feed->id, 'instagram_link' => $feed->link, @@ -467,3 +473,32 @@ function instagram_social_feed_ctools_plugin_directory($owner, $plugin_type) { return 'plugins/export_ui'; } } + +/** + * Implements hook_theme(). + */ +function instagram_social_feed_theme() { + return array( + 'instagram_social_feed_last_pic' => array( + 'variables' => array( + 'instagram_user' => NULL, + 'instagram_account_url' => NULL, + 'cta_text_inline' => NULL, + 'cta_text_stacked' => NULL, + 'instagram_photo_url' => NULL, + 'thumbnail_photo' => NULL, + 'thumbnail_width' => NULL, + 'thumbnail_height' => NULL, + 'low_resolution_photo' => NULL, + 'low_resolution_width' => NULL, + 'low_resolution_height' => NULL, + 'standard_resolution_photo' => NULL, + 'standard_resolution_width' => NULL, + 'standard_resolution_height' => NULL, + 'caption' => NULL, + ), + 'template' => 'instagram-social-feed-last-pic', + 'path' => drupal_get_path('module', 'instagram_social_feed_fields') . '/templates', + ), + ); +} diff --git a/instagram_social_feed_fields/instagram_social_feed_fields.info b/instagram_social_feed_fields/instagram_social_feed_fields.info new file mode 100644 index 0000000..716f1c6 --- /dev/null +++ b/instagram_social_feed_fields/instagram_social_feed_fields.info @@ -0,0 +1,5 @@ +name = Instagram Social Feed Fields +description = Adds fields for the last picture content for Instagram feeds. +core = 7.x +dependencies[] = instagram_social_feed +dependencies[] = field diff --git a/instagram_social_feed_fields/instagram_social_feed_fields.install b/instagram_social_feed_fields/instagram_social_feed_fields.install new file mode 100644 index 0000000..8fc3f1d --- /dev/null +++ b/instagram_social_feed_fields/instagram_social_feed_fields.install @@ -0,0 +1,39 @@ + array( + 'description' => 'The unique identifier for this feed within the Instagram Social Feed module.', + 'type' => 'varchar', + 'length' => 20, + 'not null' => TRUE, + ), + 'empty_message' => array( + 'description' => 'A message to show if there are no photos for this feed.', + 'type' => 'varchar', + 'length' => 140, + 'not null' => TRUE, + ), + ); + $indexes = array( + 'feed_id' => array('feed_id'), + ); + return array( + 'columns' => $columns, + 'indexes' => $indexes, + ); +} + +/** + * Implements hook_uninstall(). + */ +// function instagram_social_feed_fields_uninstall() { +// } diff --git a/instagram_social_feed_fields/instagram_social_feed_fields.module b/instagram_social_feed_fields/instagram_social_feed_fields.module new file mode 100644 index 0000000..ebd2aae --- /dev/null +++ b/instagram_social_feed_fields/instagram_social_feed_fields.module @@ -0,0 +1,180 @@ + array( + 'label' => t('Instagram Social Feed: Last photo'), + 'description' => t('Display the last photo for a specific Instagram feed.'), + 'default_widget' => 'instagram_social_feed_last_pic_widget', + 'default_formatter' => 'instagram_social_feed_last_pic_formatter', + ), + ); +} + +/** + * Implements hook_field_is_empty(). + */ +function instagram_social_feed_fields_field_is_empty($item, $field) { + return empty($item['feed_id']); +} + +/** + * Field widget. + */ + +/** + * Implements hook_field_widget_info(). + */ +function instagram_social_feed_fields_field_widget_info() { + return array( + 'instagram_social_feed_last_pic_widget' => array( + 'label' => t('Instagram feed selector'), + 'field types' => array('instagram_social_feed_last_pic'), + ), + ); +} + +/** + * Implements hook_field_widget_form(). + */ +function instagram_social_feed_fields_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + $main_widget = $element; + $main_widget['#delta'] = $delta; + $empty_widget = array(); + + if ($instance['widget']['type'] == 'instagram_social_feed_last_pic_widget') { + global $user; + $main_widget += array( + '#type' => 'select', + '#options' => instagram_social_feed_fields_get_all_feeds(), + '#default_value' => isset($items[$delta]['feed_id']) ? $items[$delta]['feed_id'] : '', + '#required' => TRUE, + ); + if (!empty($main_widget['#description'])) { + $main_widget['#description'] .= ' '; + } + $main_widget['#description'] .= t('Need to add another feed to the list?', array('@url' => url('admin/config/services/instagram_social_feed'))); + $empty_widget = array( + '#type' => 'textfield', + '#default_value' => isset($items[$delta]['empty_message']) ? $items[$delta]['empty_message'] : '', + '#title' => t('Message to use if no photos found'), + ); + } + + $element['feed_id'] = $main_widget; + $element['empty_message'] = $empty_widget; + + return $element; +} + +/** + * Field formatter. + */ + +/** + * Implements hook_field_formatter_info(). + */ +function instagram_social_feed_fields_field_formatter_info() { + return array( + // Display the picture. + 'instagram_social_feed_last_pic_formatter' => array( + 'label' => t('Standard formatter'), + 'field types' => array('instagram_social_feed_last_pic'), + ), + ); +} + +/** + * Implements hook_field_formatter_view(). + */ +function instagram_social_feed_fields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $element = array(); + + if ($display['type'] == 'instagram_social_feed_last_pic_formatter') { + module_load_include('inc', 'instagram_social_feed'); + + foreach ($items as $delta => $item) { + // Load the most recent info for this feed. + $result = db_query("SELECT * + FROM {instagram_social_feed_photos} + WHERE feed_id = :feed_id + ORDER BY created_time DESC + LIMIT 1", + array(':feed_id' => $item['feed_id'])) + ->fetchObject(); + + if (!empty($result)) { + // Build the field. + $instagram_user = check_plain($result->instagram_user); + $instagram_photo_url = check_url($result->instagram_link); + $photo_url_parsed = parse_url($instagram_photo_url); + $instagram_account_url = $photo_url_parsed['scheme'] . '://' . $photo_url_parsed['host'] . '/' . $instagram_user; + $element[$delta] = array( + '#theme' => 'instagram_social_feed_last_pic', + '#instagram_user' => $instagram_user, + '#instagram_account_url' => $instagram_account_url, + '#cta_text_inline' => 'Follow @' . $instagram_user . ' on Instagram', + '#cta_text_stacked' => 'Follow
@' . $instagram_user . '
on Instagram', + '#instagram_photo_url' => $instagram_photo_url, + '#thumbnail_photo' => check_url($result->thumbnail), + '#thumbnail_width' => check_plain($result->thumbnail_width), + '#thumbnail_height' => check_plain($result->thumbnail_height), + '#low_resolution_photo' => check_url($result->low_resolution), + '#low_resolution_width' => check_plain($result->low_resolution_width), + '#low_resolution_height' => check_plain($result->low_resolution_height), + '#standard_resolution_photo' => check_url($result->standard_resolution), + '#standard_resolution_width' => check_plain($result->standard_resolution_width), + '#standard_resolution_height' => check_plain($result->standard_resolution_height), + '#caption' => filter_xss($result->caption), + ); + } + elseif (!empty($item['empty_message'])) { + $element = array( + '#markup' => $item['empty_message'], + ); + } + else { + watchdog('Instagram Social Feed', 'No photos found for Instagram feed ID: @id', array('@id' => $item['feed_id'])); + } + } + } + + return $element; +} + +/** + * Internal functions. + */ + +/** + * Get a list of all enabled feeds by ID and name. + */ +function instagram_social_feed_fields_get_all_feeds() { + $feeds = array(); + + $results = db_select('instagram_social_feeds', 's') + ->fields('s', array()) + ->condition('enabled', 0,'>') + ->execute(); + + foreach ($results as $row) { + $feeds[$row->id] = check_plain($row->name); + } + + return $feeds; +} diff --git a/instagram_social_feed_fields/templates/instagram-social-feed-last-pic.tpl.php b/instagram_social_feed_fields/templates/instagram-social-feed-last-pic.tpl.php new file mode 100644 index 0000000..692f0a7 --- /dev/null +++ b/instagram_social_feed_fields/templates/instagram-social-feed-last-pic.tpl.php @@ -0,0 +1,53 @@ + + + intval($standard_resolution_height)) { + $crop = 'landscape-crop'; + } + elseif (intval($standard_resolution_width) < intval($standard_resolution_height)) { + $crop = 'portrait-crop'; + } + else { + $crop = ''; + } +?> + +
+
+
+ + <?php print $caption; ?> + +
+
+
+
+
+
+
+