/[drupal]/contributions/modules/views/includes/handlers.inc
ViewVC logotype

Diff of /contributions/modules/views/includes/handlers.inc

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

revision 1.1 by merlinofchaos, Sun Aug 19 23:43:43 2007 UTC revision 1.2 by merlinofchaos, Mon Aug 27 19:16:49 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: handlers.inc,v 1.1 2007/08/19 23:43:43 merlinofchaos Exp $
3  /**  /**
4   * @file handlers.inc   * @file handlers.inc
5   * Defines the various handler objects to help build and display views.   * Defines the various handler objects to help build and display views.
# Line 17  Line 17 
17   *   // PHP 4 doesn't call constructors of the base class automatically from a   *   // PHP 4 doesn't call constructors of the base class automatically from a
18   *   // constructor of a derived class. It is your responsibility to propagate   *   // constructor of a derived class. It is your responsibility to propagate
19   *   // the call to constructors upstream where appropriate.   *   // the call to constructors upstream where appropriate.
20   *   function views_complex_join($left_table, $left_field, $field, $extra = array(), $type = 'LEFT') {   *   function construct($left_table, $left_field, $field, $extra = array(), $type = 'LEFT') {
21   *     parent::views_join($left_table, $left_field, $field, $extra, $type);   *     parent::construct($left_table, $left_field, $field, $extra, $type);
22   *   }   *   }
23   *   *
24   *   function join($table, &$query) {   *   function join($table, &$query) {
# Line 42  class views_join { Line 42  class views_join {
42    /**    /**
43     * Construct the views_join object.     * Construct the views_join object.
44     */     */
45    function views_join($table, $left_table, $left_field, $field, $extra = array(), $type = 'LEFT') {    function construct($table, $left_table, $left_field, $field, $extra = array(), $type = 'LEFT') {
46      $this->table = $table;      $this->table = $table;
47      $this->left_table = $left_table;      $this->left_table = $left_table;
48      $this->left_field = $left_field;      $this->left_field = $left_field;
# Line 89  class views_join { Line 89  class views_join {
89   * This class would be abstract in PHP5, but PHP4 doesn't understand that.   * This class would be abstract in PHP5, but PHP4 doesn't understand that.
90   *   *
91   */   */
92  class views_handler {  class views_handler extends views_object {
93    /**    /**
94     * Seed the handler with necessary data.     * Seed the handler with necessary data.
95     * @param $view     * @param $view
# Line 112  class views_handler { Line 112  class views_handler {
112      }      }
113    
114      // This exist on most handlers, but not all. So they are still optional.      // This exist on most handlers, but not all. So they are still optional.
115      if (isset($data->table)) {      if (isset($data->tablename)) {
116        $this->table = $data->table;        $this->table = $data->tablename;
117      }      }
118    
119      if (isset($data->field)) {      if (isset($data->field)) {
120        $this->field = $data->field;        $this->field = $data->field;
121          if (!isset($this->real_field)) {
122            $this->real_field = $data->field;
123          }
124      }      }
125    
126      if (isset($data->relationship)) {      if (isset($data->relationship)) {
# Line 151  class views_handler { Line 154  class views_handler {
154     * If we were using PHP5, this would be abstract.     * If we were using PHP5, this would be abstract.
155     */     */
156    function query() { }    function query() { }
157    
158      /**
159       * Ensure the main table for this handler is in the query. This is used
160       * a lot.
161       */
162      function ensure_my_table() {
163        if (!isset($this->alias)) {
164          $this->table_alias = $this->query->ensure_table($this->table, $this->relationship);
165        }
166        return $this->table_alias;
167      }
168  }  }
169    
170  /**  /**
# Line 169  class views_handler_relationship extends Line 183  class views_handler_relationship extends
183     */     */
184    function query() {    function query() {
185      $alias = $this->table . '_' . $this->field . '_' . $this->relationship;      $alias = $this->table . '_' . $this->field . '_' . $this->relationship;
186      return $this->query->add_relationship($alias, new views_join($this->view->primary_table, $this->table, $this->field, $this->primary_field), $this->relationship);      return $this->query->add_relationship($alias, new views_join($this->view->primary_table, $this->table, $this->real_field, $this->primary_field), $this->relationship);
187    }    }
188  }  }
189    
# Line 181  class views_handler_relationship extends Line 195  class views_handler_relationship extends
195   * @defgroup views_field_handlers Views' field handlers   * @defgroup views_field_handlers Views' field handlers
196   * @{   * @{
197   * Handlers to tell Views how to build and display fields.   * Handlers to tell Views how to build and display fields.
198     *
199   */   */
200    
201  /**  /**
# Line 191  class views_handler_field extends views_ Line 206  class views_handler_field extends views_
206    /**    /**
207     * Construct a new field handler.     * Construct a new field handler.
208     */     */
209    function views_handler_field($click_sortable = FALSE, $additional_fields = array()) {    function construct($click_sortable = FALSE, $additional_fields = array()) {
210      $this->click_sortable = $click_sortable;      $this->click_sortable = $click_sortable;
211      $this->additional_fields = $additional_fields;      $this->additional_fields = $additional_fields;
212    }    }
# Line 200  class views_handler_field extends views_ Line 215  class views_handler_field extends views_
215     * Called to add the field to a query.     * Called to add the field to a query.
216     */     */
217    function query() {    function query() {
218      // Ensure the requested table is part of the query, and get the proper alias fori t.      $this->ensure_my_table();
     $alias = $this->query->ensure_table($this->table, $this->relationship);  
219      // Add the field.      // Add the field.
220      $this->field_alias = $this->query->add_field($alias, $this->field);      $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field);
221        dpr("alias: $this->field_alias");
222      // Add any additional fields we are given.      // Add any additional fields we are given.
223      if (!empty($this->additional_fields) && is_array($this->additional_fields)) {      if (!empty($this->additional_fields) && is_array($this->additional_fields)) {
224        foreach ($this->additional_fields as $this->field) {        foreach ($this->additional_fields as $field) {
225          $this->query->add_field($alias, $this->field);          $this->aliases[$field] = $this->query->add_field($this->table_alias, $field);
226        }        }
227      }      }
228    }    }
# Line 216  class views_handler_field extends views_ Line 231  class views_handler_field extends views_
231     * Called to determine what to tell the clicksorter.     * Called to determine what to tell the clicksorter.
232     */     */
233    function click_sort() {    function click_sort() {
234      // Ensure the requested table is part of the query, and get the proper alias for it.      return "$this->field_alias";
     $alias = $this->query->ensure_table($this->table, $this->relationship);  
     return "$alias.$this->field";  
235    }    }
236    
237    /**    /**
# Line 240  class views_handler_field_date extends v Line 253  class views_handler_field_date extends v
253    /**    /**
254     * Constructor; calls to base object constructor.     * Constructor; calls to base object constructor.
255     */     */
256    function views_handler_field_date($click_sortable = FALSE, $additional_fields = array()) {    function construct($click_sortable = FALSE, $additional_fields = array()) {
257      parent::views_handler_field($click_sortable, $additional_fields);      parent::construct($click_sortable, $additional_fields);
258    }    }
259    
260    function options_form(&$form) {    function options_form(&$form) {
# Line 267  class views_handler_field_date extends v Line 280  class views_handler_field_date extends v
280    
281    function render($values) {    function render($values) {
282      $value = $values->{$this->field_alias};      $value = $values->{$this->field_alias};
283      $format = $this->options['date_format'];      $format = !empty($this->options['date_format']) ? $this->options['date_format'] : 'medium';
284      $custom_format = $this->options['custom_date_format'];      if ($format == 'custom') {
285          $custom_format = $this->options['custom_date_format'];
286        }
287    
288      switch ($format) {      switch ($format) {
289        case 'time ago':        case 'time ago':
# Line 298  class views_handler_sort extends views_h Line 313  class views_handler_sort extends views_h
313     * Called to add the sort to a query.     * Called to add the sort to a query.
314     */     */
315    function query() {    function query() {
316      // Ensure the requested table is part of the query, and get the proper alias for it.      $this->ensure_my_table();
     $alias = $this->query->ensure_table($this->table, $this->relationship);  
317      // Add the field.      // Add the field.
318      $this->query->add_orderby($alias, $this->field, $this->data->order);      $this->query->add_orderby($this->table_alias, $this->real_field, $this->data->order);
319    }    }
320  }  }
321    
# Line 316  class views_handler_sort_formula extends Line 330  class views_handler_sort_formula extends
330     *   The formula used to sort. If an array, may be keyed by database type. If     *   The formula used to sort. If an array, may be keyed by database type. If
331     *   used, 'default' MUST be defined.     *   used, 'default' MUST be defined.
332     */     */
333    function views_handler_sort_formula($formula) {    function construct($formula) {
334      $this->formula = $formula;      $this->formula = $formula;
335      if (is_array($formula) && !isset($formula['default'])) {      if (is_array($formula) && !isset($formula['default'])) {
336        $this->error = t('views_handler_sort_formula missing default: @formula', array('@formula' => var_export($formula, TRUE)));        $this->error = t('views_handler_sort_formula missing default: @formula', array('@formula' => var_export($formula, TRUE)));
# Line 338  class views_handler_sort_formula extends Line 352  class views_handler_sort_formula extends
352      else {      else {
353        $formula = $this->formula;        $formula = $this->formula;
354      }      }
355      // Ensure the requested table is part of the query, and get the proper alias for it.      $this->ensure_my_table();
     $alias = $this->query->ensure_table($this->table, $this->relationship);  
356      // Add the field.      // Add the field.
357      $this->add_orderby(NULL, $this->formula, $this->data->order, $alias . '_' . $this->field);      $this->add_orderby(NULL, $this->formula, $this->data->order, $this->table_alias . '_' . $this->field);
358    }    }
359  }  }
360    
# Line 395  class views_handler_filter extends views Line 408  class views_handler_filter extends views
408     * Add this filter to the query.     * Add this filter to the query.
409     */     */
410    function query() {    function query() {
411      $alias = $this->query->ensure_table($this->table, $this->relationship);      $this->ensure_my_table();
412      $this->query->add_where($this->data->group, "$alias.$this->field " . $this->data->operator . " '%s'", $this->data->value);      $this->query->add_where($this->data->group, "$this->table_alias.$this->real_field " . $this->data->operator . " '%s'", $this->data->value);
413    }    }
414  }  }
415    
# Line 417  class views_handler_argument extends vie Line 430  class views_handler_argument extends vie
430    /**    /**
431     * Constructor     * Constructor
432     */     */
433    function views_handler_argument($name_field = NULL) {    function construct($name_field = NULL) {
434      $this->name_field = $name_field;      $this->name_field = $name_field;
435    }    }
436    
# Line 434  class views_handler_argument extends vie Line 447  class views_handler_argument extends vie
447     *   The alias used to get the number of records (count) for this entry.     *   The alias used to get the number of records (count) for this entry.
448     */     */
449    function summary_query() {    function summary_query() {
450      $alias = $this->query->ensure_table($this->table, $this->relationship);      $this->ensure_my_table();
451      // Add the field.      // Add the field.
452      $this->base_alias = $this->query->add_field($alias, $this->field);      $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
453    
454      // Add the 'name' field. For example, if this is a uid argument, the      // Add the 'name' field. For example, if this is a uid argument, the
455      // name field would be 'name' (i.e, the username).      // name field would be 'name' (i.e, the username).
456      if (isset($this->name_field)) {      if (isset($this->name_field)) {
457        $this->name_alias = $this->query->add_field($alias, $this->name_field);        $this->name_alias = $this->query->add_field($this->table_alias, $this->name_field);
458      }      }
459      else {      else {
460        $this->name_alias = $this->base_alias;        $this->name_alias = $this->base_alias;
461      }      }
462    
463        return $this->summary_basics();
464      }
465    
466      /**
467       * Some basic summary behavior that doesn't need to be repeated as much as
468       * code that goes into summary_query()
469       */
470      function summary_basics($count_field = TRUE) {
471      // Add the number of nodes counter      // Add the number of nodes counter
472      $count_alias = $this->query->add_field(NULL, 'COUNT(' . $this->query->primary_field . ')', 'num_records');      $count_alias = $this->query->add_field(NULL, 'COUNT(' . $this->query->primary_field . ')', 'num_records');
473      $this->query->add_groupby($this->base_alias);      $this->query->add_groupby($this->base_alias);
474    
475      $this->query->set_count_field($alias, $this->field);      if ($count_field) {
476          $this->query->set_count_field($this->table_alias, $this->real_field);
477        }
478    
479      return $count_alias;      return $count_alias;
480    }    }
# Line 477  class views_handler_argument extends vie Line 500  class views_handler_argument extends vie
500     *   The base URL to use.     *   The base URL to use.
501     */     */
502    function summary_link($data, $url) {    function summary_link($data, $url) {
503      return l($data->{$this->name_alias}, "$url/$this->base_field");      $value = $data->{$this->base_alias};
504        return l($data->{$this->name_alias}, "$url/$value");
505    }    }
506    
507    /**    /**
# Line 545  class views_handler_argument extends vie Line 569  class views_handler_argument extends vie
569          // Summaries have their own sorting and fields, so tell the View not          // Summaries have their own sorting and fields, so tell the View not
570          // to build these.          // to build these.
571          $this->view->build_sort = $this->view->build_fields = FALSE;          $this->view->build_sort = $this->view->build_fields = FALSE;
572            return TRUE;
573      }      }
574    }    }
575    
# Line 554  class views_handler_argument extends vie Line 579  class views_handler_argument extends vie
579     * The argument sent may be found at $this->argument.     * The argument sent may be found at $this->argument.
580     */     */
581    function query() {    function query() {
582      // Ensure the requested table is part of the query, and get the proper alias fori t.      $this->ensure_my_table();
583      $alias = $this->query->ensure_table($this->table, $this->relationship);      $this->query->add_where(0, "$this->table_alias.$this->real_field = '%s'", $this->argument);
     // Add the field.  
     $field = $this->query->add_field($alias, $this->field);  
     $this->query->add_where(0, "$field = '%s'", $this->argument);  
584    }    }
585    
586    /**    /**
# Line 566  class views_handler_argument extends vie Line 588  class views_handler_argument extends vie
588     *     *
589     * This usually needs to be overridden to provide a proper title.     * This usually needs to be overridden to provide a proper title.
590     */     */
591    function title($argument) {    function title() {
592      return check_plain($argument);      return check_plain($this->argument);
593    }    }
594  }  }
595    
596  /**  /**
597   * Argument handler for simple formulae.   * Abstract argument handler for simple formulae.
598     *
599     * Child classes of this object should implement summary_link, at least.
600   */   */
601  class views_handler_argument_date extends views_handler_argument {  class views_handler_argument_formula extends views_handler_argument {
602      /**
603       * Constructor
604       */
605      function construct($formula) {
606        $this->formula = $formula;
607      }
608    
609      /**
610       * Build the summary query based on a formula
611       */
612      function summary_query() {
613        $this->ensure_my_table();
614        $field_alias = $alias . '_' . $this->field;
615        // Add the field.
616        $this->base_alias = $this->query->add_field(NULL, $this->formula, $field_alias);
617        $this->query->set_count_field(NULL, $this->formula, $field_alias);
618    
619        return $this->summary_basics(FALSE);
620      }
621    
622      /**
623       * Build the query based upon the formula
624       */
625      function query() {
626        $this->ensure_my_table();
627        $field_alias = $alias . '_' . $this->field;
628        // Add the field.
629        $this->name_alias = $this->query->add_field(NULL, $this->formula, $field_alias);
630        $this->query->add_where(0, "$field = '%s'", $this->argument);
631      }
632    }
633    
634    /**
635     * Argument handler for a year (CCYY)
636     */
637    class views_handler_argument_date_year extends views_handler_argument_formula {
638      /**
639       * Constructor implementation
640       */
641      function construct() {
642        $timezone = views_get_timezone();
643        $this->formula = "YEAR(FROM_UNIXTIME(node.created+$timezone))";
644      }
645    
646      /**
647       * Provide a link to the next level of the view
648       */
649      function summary_link($data, $url) {
650        $value = $data->{$this->base_alias};
651        return l($value, "$url/$value");
652      }
653    }
654    
655    /**
656     * Argument handler for a year plus month (CCYYMM)
657     */
658    class views_handler_argument_date_year_month extends views_handler_argument_formula {
659      /**
660       * Constructor implementation
661       */
662      function construct() {
663        $timezone = views_get_timezone();
664        $this->formula =  "DATE_FORMAT(FROM_UNIXTIME(node.created+$timezone), '%Y%m')";
665        $this->format = 'F, Y';
666      }
667    
668      /**
669       * Provide a link to the next level of the view
670       */
671      function summary_link($data, $url) {
672        $value = $data->{$this->base_alias};
673        $created = $data->{$this->name_alias};
674        return l(format_date($created, 'custom', $this->format), "$url/$value");
675      }
676    
677      /**
678       * Provide a link to the next level of the view
679       */
680      function title($data, $url) {
681        return format_date(strtotime($this->argument . "15"), 'custom', $this->format, 0);
682      }
683    }
684    
685    /**
686     * Argument handler for a month (MM)
687     */
688    class views_handler_argument_date_month extends views_handler_argument_formula {
689      /**
690       * Constructor implementation
691       */
692      function construct() {
693        $timezone = views_get_timezone();
694        $this->formula =  "MONTH(FROM_UNIXTIME(node.created+$timezone))";
695        $this->format = 'F';
696      }
697    
698      /**
699       * Provide a link to the next level of the view
700       */
701      function summary_link($data, $url) {
702        $value = $data->{$this->base_alias};
703        $created = $data->{$this->name_alias};
704        return l(format_date($created, 'custom', $this->format), "$url/$value");
705      }
706    
707      /**
708       * Provide a link to the next level of the view
709       */
710      function title($data, $url) {
711        return format_date(strtotime("2005" . $this->argument . "15"), 'custom', $this->format, 0);
712      }
713  }  }
714    
715  /**  /**

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

  ViewVC Help
Powered by ViewVC 1.1.3