/[drupal]/contributions/modules/category/contrib/category_views/category_views.module
ViewVC logotype

Diff of /contributions/modules/category/contrib/category_views/category_views.module

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

revision 1.8, Mon Jul 16 03:40:21 2007 UTC revision 1.9, Wed Aug 5 04:52:54 2009 UTC
# Line 9  Line 9 
9  /**  /**
10   * Implementation of hook_help().   * Implementation of hook_help().
11   */   */
12  function category_views_help($section) {  function category_views_help($path, $arg) {
13    switch ($section) {    switch ($path) {
14      case 'admin/help#category_views':      case 'admin/help#category_views':
15        return t('<p>The category_views module renders node listings on category pages using a view. It requires the category module and the views module to be installed.</p>');        return t('<p>The category_views module renders node listings on category pages using a view. It requires the category module and the views module to be installed.</p>');
16    }    }
# Line 22  function category_views_help($section) { Line 22  function category_views_help($section) {
22  function category_views_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {  function category_views_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
23    switch ($op) {    switch ($op) {
24      case 'load':      case 'load':
25        if (category_is_cat_or_cont($node->nid)) {        $behavior = variable_get('category_behavior_'. $node->type, 0);
26          $type = category_node_get_type($node);        if (!empty($behavior)) {
27          $is_cat = $type == 'category_cat';          // This is a container or category. Load the view settings from database.
28          if (!$is_cat) {          // No category information is available in the $node object yet.
29            return (array)category_views_cont_load($node->nid);          $cache_key = 'cview_node_load';
30          }          $output = category_cache_op('get', $node->nid, $cache_key);
31          else {          if (!isset($output)) {
32            return (array)category_views_cont_load($node->cnid);            if ($behavior === 'container') {
33                $result = db_query('SELECT * FROM {category_views} WHERE cid = %d', $node->nid);
34              }
35              else {
36                $result = db_query('SELECT v.* FROM {category_views} v INNER JOIN {category} c ON v.cid = c.cnid WHERE c.cid = %d', $node->nid);
37              }
38              $settings = db_fetch_array($result);
39              $output = array(
40                'view_for_cats' => $settings['view_name'],
41                'view_display' => array('cont' => $settings['display_cont'], 'cat' => $settings['display_cat']),
42              );
43              category_cache_op('set', $node->nid, $cache_key, $output);
44          }          }
45            return $output;
46        }        }
47        break;        break;
48    
49      case 'insert':      case 'insert':
50      case 'update':      case 'update':
51        $op = $_POST['op'];        $behavior = variable_get('category_behavior_'. $node->type, 0);
52        $submit_values = array(        if ($behavior === 'container') {
53          'insert' => t('Add to category outline'),          // This is a container. Insert or update view settings in database.
54          'update' => t('Update category outline'),          $old_entry = db_fetch_array(db_query('SELECT * FROM {category_views} WHERE cid = %d', $node->nid));
55          'delete' => t('Remove from category outline'),          if (!empty($old_entry)) {
56        );            db_query("UPDATE {category_views} SET view_name = '%s', display_cont = %d, display_cat = %d WHERE cid = %d", $node->view_for_cats, $node->view_display['cont'], $node->view_display['cat'], $node->nid);
       if (in_array($op, $submit_values)) {  
         switch ($op) {  
           case $submit_values['insert']:  
           case $submit_values['update']:  
             category_views_cont_update($node);  
             break;  
           case $submit_values['delete']:  
             category_views_cont_delete($node->nid);  
             break;  
57          }          }
58        }          else {
59        elseif (category_is_cat_or_cont($node->nid, TRUE)) {            db_query("INSERT INTO {category_views} (cid, view_name, display_cont, display_cat) VALUES (%d, '%s', %d, %d)", $node->nid, $node->view_for_cats, $node->view_display['cont'], $node->view_display['cat']);
         $type = category_node_get_type($node);  
         $is_cat = $type == 'category_cat';  
         if (!$is_cat) {  
           category_views_cont_update($node);  
60          }          }
61        }        }
62        break;        break;
63    
64      case 'delete':      case 'delete':
65        $type = category_node_get_type($node);        $behavior = variable_get('category_behavior_'. $node->type, 0);
66        $is_cat = $type == 'category_cat';        if ($behavior === 'container') {
67        if (!$is_cat) {          // This is a container being deleted. Delete view settings from database.
68          category_views_cont_delete($node->nid);          db_query('DELETE FROM {category_views} WHERE cid = %d', $node->nid);
69        }        }
70        break;        break;
71    }    }
72  }  }
73    
74  /**  /**
75   * Implementation of hook_category().   * Implementation of hook_form_alter().
76   */   */
77  function category_views_category($op, $node = NULL) {  function category_views_form_alter(&$form, $form_state, $form_id) {
78    switch ($op) {    if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
79      case 'form':      $node = $form['#node'];
80        // Views settings      if (isset($node->category) && $node->category['behavior'] === 'container') {
81        $form['category_views'] = array(  
82          // Add views settings to container node form.
83          $form['category']['view'] = array(
84          '#type' => 'fieldset',          '#type' => 'fieldset',
85          '#title' => t('Category view settings'),          '#title' => t('Category view settings'),
86          '#collapsible' => TRUE,          '#collapsible' => TRUE,
87          '#collapsed' => TRUE,          '#collapsed' => TRUE,
88            '#weight' => 8,
89        );        );
90          $form['#submit'][] = 'category_views_node_form_submit';
91        $view_list = array(0 => '<'. t('none') .'>') + category_views_get_views();        $view_list = array(0 => '<'. t('none') .'>') + category_views_get_views();
92        $options = array(        $options = array(
93          'cont' => t('This container\'s page'),          'cont' => t('This container\'s page'),
94          'cat' => t('Pages for categories in this container'),          'cat' => t('Pages for categories in this container'),
95        );        );
96        $form['category_views']['view_for_cats'] = array(        $form['category']['view']['view_for_cats'] = array(
97          '#type' => 'select',          '#type' => 'select',
98          '#title' => t('View for this container'),          '#title' => t('View for this container'),
99          '#default_value' => isset($node->view_for_cats) ? $node->view_for_cats : 0,          '#default_value' => isset($node->view_for_cats) ? $node->view_for_cats : 0,
100          '#options' => $view_list,          '#options' => $view_list,
101          '#multiple' => FALSE,          '#multiple' => FALSE,
102          '#description' => t('The view to use for rendering listings of assigned nodes, in this container and its categories. If you do not check either of the \'show view on\' boxes below, then it makes no difference whether or not you select a view here. Conversely, if you leave this setting at its default value (&lt;none&gt;), then it makes no difference whether or not you check either of the boxes below. If there are no views available, or if the currently available views are not adequate, you can !create-view (<em>warning:</em> clicking this link may cause you to leave this page, and to lose whatever information you have entered so far).', array('!create-view' => l(t('create a new view'), 'admin/views/add'))),          '#description' => t('The view to use for rendering listings of assigned nodes, in this container and its categories. If you do not check either of the \'show view on\' boxes below, then it makes no difference whether or not you select a view here. Conversely, if you leave this setting at its default value (&lt;none&gt;), then it makes no difference whether or not you check either of the boxes below. If there are no views available, or if the currently available views are not adequate, you can !create-view (<em>warning:</em> clicking this link may cause you to leave this page, and to lose whatever information you have entered so far).', array('!create-view' => l(t('create a new view'), 'admin/build/views/add'))),
103        );        );
104        $form['category_views']['view_display'] = array(        $form['category']['view']['view_display'] = array(
105          '#type' => 'checkboxes',          '#type' => 'checkboxes',
106          '#title' => t('Show view on'),          '#title' => t('Show view on'),
107          '#default_value' => isset($node->view_display) ? $node->view_display : array(),          '#default_value' => array('cat' => empty($node->view_display['cat']) ? 0 : 'cat', 'cont' => empty($node->view_display['cont']) ? 0 : 'cont'),
108          '#options' => $options,          '#options' => $options,
109          '#description' => t('Whether to use this view for rendering assigned node listings on this container\'s page, and/or on pages for categories in this container. The node listing will be passed to the view renderer, instead of to the default category module renderer.'),          '#description' => t('Whether to use this view for rendering assigned node listings on this container\'s page, and/or on pages for categories in this container. The node listing will be passed to the view renderer, instead of to the default category module renderer.'),
110        );        );
111    
112        return $form;      }
113        break;    }
114    }
115    
116    /**
117     * Additional form submit handler for node forms.
118     *
119     * Converts the $form_state['values']['category']['view'] array
120     * so that it will be merged back into node object.
121     */
122    function category_views_node_form_submit($form, &$form_state) {
123      if (isset($form_state['values']['category']['view']['view_for_cats'])) {
124        $form_state['values']['view_for_cats'] = $form_state['values']['category']['view']['view_for_cats'];
125      }
126      if (isset($form_state['values']['category']['view']['view_display'])) {
127        $form_state['values']['view_display']['cont'] = (int) !empty($form_state['values']['category']['view']['view_display']['cont']);
128        $form_state['values']['view_display']['cat'] = (int) !empty($form_state['values']['category']['view']['view_display']['cat']);
129    }    }
130      unset($form_state['values']['category']['view']);
131  }  }
132    
133  /**  /**
# Line 133  function category_views_category_legacy( Line 154  function category_views_category_legacy(
154  }  }
155    
156  /**  /**
157   * Responds to the load of an existing container, by loading the container's   * Fetches a list of all views usable for 'default' display.
  * views settings from the database.  
  *  
  * @param $nid  
  *   The node ID of the container being loaded.  
158   *   *
159   * @return   * @return
160   *   The views settings for the specified node.   *   An array of views, where each key is the view's name, and each value is its
  */  
 function category_views_cont_load($nid) {  
   $container = new stdClass();  
   $sql = db_query("SELECT * FROM {category_views} WHERE cid = %d", $nid);  
   if ($result = db_fetch_object($sql)) {  
     $container->view_for_cats = $result->view_id;  
     if ($result->display_cont) $container->view_display['cont'] = 'cont';  
     if ($result->display_cat) $container->view_display['cat'] = 'cat';  
   }  
   
   return $container;  
 }  
   
 /**  
  * Responds to the insert or update of an existing container, by updating the  
  * container's export settings in the database.  
  *  
  * @param $node  
  *   The container node being updated.  
  */  
 function category_views_cont_update($node) {  
   $node->cid = $node->nid;  
   $fields = array(  
     'cid',  
     'view_id',  
     'display_cont',  
     'display_cat',  
   );  
   $display_cont = $node->view_display['cont'] ? 1 : 0;  
   $display_cat = $node->view_display['cat'] ? 1 : 0;  
   
   $data_exists = (db_num_rows(db_query('SELECT * FROM {category_views} WHERE cid = %d', $node->nid)) > 0);  
   if ($data_exists) {  
     db_query('UPDATE {category_views} SET view_id = %d, display_cont = %d, display_cat = %d WHERE cid = %d', $node->view_for_cats, $display_cont, $display_cat, $node->nid);  
   }  
   else {  
     db_query('INSERT INTO {category_views} '. _category_container_insert($node, 1, $fields) .' VALUES (%d, %d, %d, %d)', $node->cid, $node->view_for_cats, $display_cont, $display_cat);  
   }  
 }  
   
 /**  
  * Responds to the delete of an existing container, by deleting the container's  
  * export settings in the database.  
  *  
  * @param $nid  
  *   The node ID of the container being deleted.  
  */  
 function category_views_cont_delete($nid) {  
   db_query('DELETE FROM {category_views} WHERE cid = %d', $nid);  
 }  
   
 /**  
  * Fetches a list of all views from the database.  
  *  
  * @return  
  *   An array of views, where each key is the view's ID, and each value is its  
161   *   description (not its title).   *   description (not its title).
162   */   */
163  function category_views_get_views() {  function category_views_get_views() {
164    $views = array();    $views = array();
165    
166    $result = db_query("SELECT vid, description FROM {view_view} ORDER BY description");    // Get a list of all displays for all enabled views through the API, by searching
167    while ($view = db_fetch_object($result)) {    // for 'handler' setting, which is always there. We can't get the list directly
168      $views[$view->vid] = $view->description;    // from database, because some views may be stored in hook_views_default_views()
169      // implementations instead. (Views documentation suggests this practice for site
170      // specific modules too.)
171      $all_views = views_get_applicable_views('handler');
172      // Get only 'default' display for each view, and build form options including
173      // descriptions and tags.
174      foreach ($all_views as $result) {
175        list($view, $id) = $result;
176        if ($id == 'default') {
177          $views[$view->name] = $view->description . ($view->tag ? ' ('. $view->tag .')' : '');
178        }
179    }    }
180    
181    return $views;    return $views;
# Line 212  function category_views_get_views() { Line 183  function category_views_get_views() {
183    
184  /**  /**
185   * Determine whether or not a view should be used for rendering the listing   * Determine whether or not a view should be used for rendering the listing
186   * of the specified node.   * of the specified node. This is called directly from category.module.
187   *   *
188   * @param $node   * @param $node
189   *   The node being displayed.   *   The node being displayed.
# Line 223  function category_views_get_views() { Line 194  function category_views_get_views() {
194   *   Boolean TRUE if a view should be used, FALSE otherwise.   *   Boolean TRUE if a view should be used, FALSE otherwise.
195   */   */
196  function category_views_is_visible($node, $type = NULL) {  function category_views_is_visible($node, $type = NULL) {
   if (!isset($type)) {  
     $type = category_node_get_type($node);  
   }  
   
197    if ($node->view_for_cats) {    if ($node->view_for_cats) {
198      if ($type == 'category_cont' && $node->view_display['cont']) {      if ($node->category['behavior'] === 'container' && $node->view_display['cont']) {
199        return TRUE;        return TRUE;
200      }      }
201      elseif ($type == 'category_cat' && $node->view_display['cat']) {      elseif ($node->category['behavior'] === 'category' && $node->view_display['cat']) {
202        return TRUE;        return TRUE;
203      }      }
204    }    }
# Line 240  function category_views_is_visible($node Line 207  function category_views_is_visible($node
207    
208  /**  /**
209   * Retrieves a themed view of the specified category node.   * Retrieves a themed view of the specified category node.
210     * This is called directly from category.module.
211   *   *
212   * @param $node   * @param $node
213   *   The category or container for which to generate the view output.   *   The category or container for which to generate the view output.
# Line 249  function category_views_is_visible($node Line 217  function category_views_is_visible($node
217   *   another page (such as a category page).   *   another page (such as a category page).
218   */   */
219  function category_views_render_nodes($node) {  function category_views_render_nodes($node) {
220      // We cannot use the simple views_embed_view() function, because the Exposed
221      // filters submit button won't point back to the category node in that case.
222      // We do the same manually here, adding the necessary path override.
223    $view = views_get_view($node->view_for_cats);    $view = views_get_view($node->view_for_cats);
224    $view->url = 'node/$arg';    if ($view) {
225        $view->override_path = 'node/%';
226    return views_build_view('embed', $view, array($node->nid), $view->use_pager, $view->nodes_per_page);      return $view->preview('default', array($node->nid));
227      }
228  }  }

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

  ViewVC Help
Powered by ViewVC 1.1.2