| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: image_gallery.views.inc,v 1.3.2.1 2010/08/03 17:43:00 sun Exp $ 3 4 /** 5 * @file 6 * Image Gallery views integration. 7 * 8 * A gallery is made up of two views: 9 * - a view of nodes (the images) 10 * - a view of terms (the child galleries) 11 * The view of image nodes embeds the view of child gallery terms with a special 12 * image gallery display plugin. 13 * We provide the default views to make this, the display plugin, and a variety 14 * of fields and relationships for the child gallery view. 15 * The default views are: 16 * - image_gallery 17 * - image_gallery_terms 18 * 19 * There are two ways of getting the image gallery cover image: 20 * - a plain field. This allows complex code like recursion (check the gallery itself, 21 * if empty, check the child galleries, etc) 22 * Plain field also allows us to grab the sort order set on the gallery images 23 * and get the first image from that order as the front. 24 * (combining the two options will be MONSTROUS!) 25 * - a relationship 26 * This gets us onto the node table, so any fields can be used for the cover. 27 * Depth is possible too -- eg, 28 * "the newest image out of the gammery or its immediate children" 29 * However recursion is not possible. 30 * 31 * So we have two families of handlers: 32 * - field handlers 33 * - relationship handlers 34 * Between these many ways of getting a cover image are possible. The handlers 35 * are fairly generic, and further possibilities can be obtained just by 36 * defining more fields with those handlers. 37 */ 38 39 /** 40 * Implementation of hook_views_data_alter(). 41 * Add fields for image gallery (ie vocabulary terms) to the term_data table. 42 */ 43 function image_gallery_views_data_alter(&$data) { 44 // ---------------------------------------------------------------------- 45 // Relationships for cover node. 46 // 47 // These allow any node field to be used to create the gallery cover node. 48 // The limitation, however, is that either consider the immediate gallery, 49 // or a flat pool of the gallery and descendants. 50 // Note that a bug in Views -- http://drupal.org/node/380560 -- will cause 51 // an error message when adding node fields on these relationships. 52 $data['term_data']['image_gallery_cover_latest'] = array( 53 'group' => t('Image gallery'), 54 'relationship' => array( 55 'title' => t('Latest image'), 56 'label' => t('Cover image, latest'), 57 'help' => t('Relate an image gallery to its most recently updated node (does not consider child galleries).'), 58 'handler' => 'image_gallery_handler_relationship_gallery_cover', 59 'base' => 'node', 60 'field' => 'nid', 61 'correlated field' => 'tid', 62 'subquery order' => ' gallery_cover_node.created DESC ', 63 ), 64 ); 65 $data['term_data']['image_gallery_cover_oldest'] = array( 66 'group' => t('Image gallery'), 67 'relationship' => array( 68 'title' => t('Oldest image'), 69 'label' => t('Cover image, oldest'), 70 'help' => t('Relate an image gallery to its oldest node (does not consider child galleries).'), 71 'handler' => 'image_gallery_handler_relationship_gallery_cover', 72 'base' => 'node', 73 'field' => 'nid', 74 'correlated field' => 'tid', 75 'subquery order' => ' gallery_cover_node.created ASC ', 76 ), 77 ); 78 $data['term_data']['image_gallery_cover_node_title'] = array( 79 'group' => t('Image gallery'), 80 'relationship' => array( 81 'title' => t('First image by title'), 82 'label' => t('Cover image, first by title'), 83 'help' => t('Relate an image gallery to its first node when sorted by title (does not consider child galleries).'), 84 'handler' => 'image_gallery_handler_relationship_gallery_cover', 85 'base' => 'node', 86 'field' => 'nid', 87 'correlated field' => 'tid', 88 'subquery order' => ' gallery_cover_node.title ASC ', 89 ), 90 ); 91 92 // ---------------------------------------------------------------------- 93 // Simple fields. 94 95 // Gallery count. 96 $data['term_data']['image_gallery_count'] = array( 97 'group' => t('Image gallery'), 98 'field' => array( 99 'title' => t('Count'), 100 'help' => t('Count of items in a gallery.'), 101 'handler' => 'image_gallery_handler_field_gallery_count', 102 ), 103 ); 104 105 // ---------------------------------------------------------------------- 106 // Fields for cover image. 107 // 108 // These use a combination of code and separate queries to get a cover node. 109 // This makes them more powerful that using the relationship cover node, 110 // as we can consider child galleries recursively rather than just 111 // flattening all descendant galleries. 112 // We can also do complex things such as grab the top-most node from the 113 // gallery according to how the view for that gallery sorts them. 114 // The downside however is that without a relationship, the fields here are 115 // all you've got. 116 // To add more fields, define them on term_data and optionally add handlers. 117 // See image_gallery_handler_field_gallery_cover for more information. 118 $data['term_data']['image_gallery_latest_thumbnail'] = array( 119 'group' => t('Image gallery'), 120 'field' => array( 121 'title' => t('Latest image'), 122 'help' => t('The most recently posted image in the gallery or its child galleries.'), 123 'handler' => 'image_gallery_handler_field_gallery_cover_thumbnail', 124 'order clause' => 'n.sticky DESC, n.created DESC', 125 ), 126 ); 127 $data['term_data']['image_gallery_latest_time'] = array( 128 'group' => t('Image gallery'), 129 'field' => array( 130 'title' => t('Last updated time'), 131 'help' => t('The time of the most recently posted image in the gallery or its child galleries.'), 132 'handler' => 'image_gallery_handler_field_gallery_cover_latest_time', 133 'order clause' => 'n.sticky DESC, n.created DESC', 134 ), 135 ); 136 $data['term_data']['image_gallery_first_title'] = array( 137 'group' => t('Image gallery'), 138 'field' => array( 139 'title' => t('First image by title'), 140 'help' => t('The first posted image in the gallery or its child galleries.'), 141 'handler' => 'image_gallery_handler_field_gallery_cover_thumbnail', 142 'order clause' => 'n.sticky DESC, n.title ASC', 143 ), 144 ); 145 $data['term_data']['image_gallery_oldest_thumbnail'] = array( 146 'group' => t('Image gallery'), 147 'field' => array( 148 'title' => t('Oldest image'), 149 'help' => t('The first posted image in the gallery or its child galleries.'), 150 'handler' => 'image_gallery_handler_field_gallery_cover_thumbnail', 151 'order clause' => 'n.sticky DESC, n.created ASC', 152 ), 153 ); 154 } 155 156 /** 157 * Implementation of hook_views_handlers(). 158 */ 159 function image_gallery_views_handlers() { 160 return array( 161 'info' => array( 162 'path' => drupal_get_path('module', 'image_gallery') . '/views', 163 ), 164 'handlers' => array( 165 'image_gallery_handler_field_gallery_count' => array( 166 'parent' => 'views_handler_field_taxonomy', 167 ), 168 'image_gallery_handler_field_gallery_cover' => array( 169 'parent' => 'views_handler_field_taxonomy', 170 ), 171 'image_gallery_handler_field_gallery_cover_thumbnail' => array( 172 'parent' => 'image_gallery_handler_field_gallery_cover', 173 ), 174 'image_gallery_handler_field_gallery_cover_latest_time' => array( 175 'parent' => 'image_gallery_handler_field_gallery_cover', 176 ), 177 'image_gallery_handler_relationship_gallery_cover' => array( 178 'parent' => 'views_handler_relationship', 179 ), 180 'image_gallery_plugin_display_image_gallery' => array( 181 'parent' => 'views_plugin_display_page', 182 ), 183 ), 184 ); 185 } 186 187 /** 188 * Implementation of hook_views_plugins(). 189 */ 190 function image_gallery_views_plugins() { 191 return array( 192 'display' => array( 193 /** 194 * The image gallery display: like a page, but with a subgallery 195 * list embedded above it. 196 * Uses a handler rather than a tpl file. 197 */ 198 'image_gallery' => array( 199 'title' => t('Gallery page'), 200 'help' => t('Display the view as a gallery of images, with a URL and menu links.'), 201 'parent' => 'page', 202 'handler' => 'image_gallery_plugin_display_image_gallery', 203 'theme' => 'views_view', 204 'uses hook menu' => TRUE, 205 'use ajax' => TRUE, 206 'use pager' => TRUE, 207 'accept attachments' => TRUE, 208 'admin' => t('Page'), 209 // @todo ? 210 'help topic' => 'display-page', 211 'path' => drupal_get_path('module', 'image_gallery') . '/views', 212 ), 213 ), 214 'style' => array( 215 /** 216 * A list style for term lists. 217 */ 218 'image_gallery_terms' => array( 219 'title' => 'Subgallery list', 220 'help' => t('Displays subgalleries in a formatted list.'), 221 'parent' => 'list', 222 'handler' => 'views_plugin_style_list', 223 'theme path' => drupal_get_path('module', 'image_gallery') . '/views/theme', 224 'theme file' => 'theme.inc', 225 'theme' => 'image_gallery_view_image_gallery_terms', 226 'uses row plugin' => TRUE, 227 'uses options' => TRUE, 228 'type' => 'normal', 229 'help topic' => 'style-list', 230 ), 231 ), 232 ); 233 } 234 235 /** 236 * Join handler for relationships that join with a subquery as the left field. 237 * To use this join, set 'left query' in the definition as a subquery to go on 238 * the left side of the JOIN condition, ie: 239 * LEFT JOIN node node_term_data ON ([YOUR SUBQUERY HERE]) = node_term_data.nid 240 * This should ultimately be replaced with the stuff for general groupwise 241 * max relationships in views: @see <http://drupal.org/node/470258>. 242 */ 243 class image_gallery_join_subquery extends views_join { 244 // PHP 4 doesn't call constructors of the base class automatically from a 245 // constructor of a derived class. It is your responsibility to propagate 246 // the call to constructors upstream where appropriate. 247 function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT') { 248 parent::construct($table, $left_table, $left_field, $field, $extra, $type); 249 250 $this->left_query = $this->definition['left query']; 251 } 252 253 /** 254 * Build the SQL for the join this object represents. 255 */ 256 function join($table, &$query) { 257 $left = $query->get_table_info($this->left_table); 258 $output = " $this->type JOIN {" . $this->table . "} $table[alias] ON ($this->left_query) = $table[alias].$this->field"; 259 260 // Tack on the extra. 261 if (isset($this->extra)) { 262 if (is_array($this->extra)) { 263 $extras = array(); 264 foreach ($this->extra as $info) { 265 $extra = ''; 266 // Figure out the table name. Remember, only use aliases provided 267 // if at all possible. 268 $join_table = ''; 269 if (!array_key_exists('table', $info)) { 270 $join_table = $table['alias'] . '.'; 271 } 272 elseif (isset($info['table'])) { 273 $join_table = $info['table'] . '.'; 274 } 275 276 // And now deal with the value and the operator. Set $q to 277 // a single-quote for non-numeric values and the 278 // empty-string for numeric values, then wrap all values in $q. 279 $raw_value = $this->db_safe($info['value']); 280 $q = (empty($info['numeric']) ? "'" : ''); 281 282 if (is_array($raw_value)) { 283 $operator = !empty($info['operator']) ? $info['operator'] : 'IN'; 284 // Transform from IN() notation to = notation if just one value. 285 if (count($raw_value) == 1) { 286 $value = $q . array_shift($raw_value) . $q; 287 $operator = $operator == 'NOT IN' ? '!=' : '='; 288 } 289 else { 290 $value = "($q" . implode("$q, $q", $raw_value) . "$q)"; 291 } 292 } 293 else { 294 $operator = !empty($info['operator']) ? $info['operator'] : '='; 295 $value = "$q$raw_value$q"; 296 } 297 $extras[] = "$join_table$info[field] $operator $value"; 298 } 299 300 if ($extras) { 301 if (count($extras) == 1) { 302 $output .= ' AND ' . array_shift($extras); 303 } 304 else { 305 $output .= ' AND (' . implode(' ' . $this->extra_type . ' ', $extras) . ')'; 306 } 307 } 308 } 309 else if ($this->extra && is_string($this->extra)) { 310 $output .= " AND ($this->extra)"; 311 } 312 } 313 return $output; 314 } 315 } 316
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |