Index: help/elementtypes.html
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/linodef/modules/linodef_buttonsapi/help/Attic/elementtypes.html,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 elementtypes.html
--- help/elementtypes.html	30 Nov 2008 13:00:31 -0000	1.1.2.2
+++ help/elementtypes.html	11 May 2009 12:08:14 -0000
@@ -1,6 +1,8 @@
 ﻿<!-- $Id: elementtypes.html,v 1.1.2.2 2008/11/30 13:00:31 roidanton Exp $ -->
 The <a href="&topic:linodef_buttonsapi/getrawdata&">data retrieve function</a> of <em>Linodef Buttons API</em> filters the parameter for the element type you entered as you created the editors button. The following list shows the supported element types as well as the elements that the buttons API returns:
 <dl>
+<dt>viewname</dt>
+<dt>Terms from a term view, nodes from a node view and fields from a node view with one CCK field. You may also set a certain view display and arguments.</dt>
 <dt>fieldname</dt>
 <dd>The values of all fields with that name as well as the title of the corresponding node in brackets, i.e. Fieldname (Node title). See <a href="&topic:linodef/fieldtypes&">supported field types</a>.</dd>
 <dt>content type</dt>
Index: includes/linodef_buttonsapi.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/linodef/modules/linodef_buttonsapi/includes/Attic/linodef_buttonsapi.inc,v
retrieving revision 1.1.2.5
diff -u -r1.1.2.5 linodef_buttonsapi.inc
--- includes/linodef_buttonsapi.inc	21 Jan 2009 17:19:40 -0000	1.1.2.5
+++ includes/linodef_buttonsapi.inc	10 May 2009 23:52:49 -0000
@@ -3,7 +3,7 @@
 
 /**
  * @file
- * Helper functions to retrieve the data for the buttons.
+ * Functions to retrieve the data for the buttons.
  *
  * The user data is filtered with check_plain and newlines are removed for compatibility with javascript of editors.
  *
@@ -12,6 +12,96 @@
  */
 
 /**
+ * Get data for button: Show elements by view name.
+ *
+ * @param string $viewname
+ *      The name of the view.
+ * @param string $options
+ *      Linodef tag options separated by comma.
+ * @return
+ *      array
+ *          - data: the node/field/vocab data
+ *              - tag: the tag to be inserted
+ *              - desc: the description for the editor button
+ *          - name: the label/name of the node/field/vocab
+ *          - element type
+ *          - element
+ *      string
+ *          if view not found a message is returned which should appear in editor text field (depends on Linodef submodule for editor)
+ *
+ * @see linodef_buttonsapi_rawdata()
+ */
+function _linodef_buttonsapi_getelementssbyviewname($viewname, $display_id = NULL, $args = NULL, $option_string = '') {
+  $view_result = _linodef_buttonsapi_get_view_result($viewname, $display_id, $args);
+  if (is_array($view_result)) {
+  
+    // Nodes/Fields.
+    if (property_exists($view_result[0], nid)) {
+      // Check for CCK fields: find the first field (lowest weight).
+      $view_resultfields = get_object_vars($view_result[0]);
+      while (current($view_resultfields) !== False && !$fieldvalue_key) {
+        $view_resultkey = key($view_resultfields);
+        if (substr($view_resultkey, 0, 15) == 'node_data_field') {
+          // Create field name.
+          $fieldname = strstr($view_resultkey, 'field_');
+          $fieldname = substr($fieldname, 0, strrpos($fieldname, 'field_', 6)-1);
+          // Key for the field value.
+          if (substr($view_resultkey, -5) == 'value') {
+            $fieldvalue_key = $view_resultkey;
+          }
+        }
+        next($view_resultfields);
+      }
+      // Field syntax.
+      if ($fieldvalue_key) {
+        foreach ($view_result as $i => $item) {
+          $item_vars = get_object_vars($item);
+          if ($item_vars[$fieldvalue_key]) {
+            $comment = _linodef_buttonsapi_setdefaultcomment($item_vars[$fieldvalue_key]);
+            $output['data'][$i] = array('tag' => '[#'. $item->nid .':'. $fieldname . $option_string . $comment .']', 'desc' => $item_vars[$fieldvalue_key]);
+          }
+        }
+        $output['element type']['output'] = t('fields');
+      }
+      // Default syntax.
+      else {
+        foreach ($view_result as $i => $item) {
+          $node = node_load($item->nid);
+          $comment = _linodef_buttonsapi_setdefaultcomment($node->title);
+          $output['data'][$i] = array('tag' => '[#'. $item->nid . $option_string . $comment .']', 'desc' => $node->title);
+        }
+        $output['element type']['output'] = t('nodes');
+      }
+    }
+
+    // Terms.
+    elseif (property_exists($view_result[0], tid)) {
+      $option_string = ',tid'. $option_string;
+      $output['element type']['output'] = t('terms');
+      $output['options']['tid'] = 'tid';
+      foreach ($view_result as $i => $item) {
+        if (!$item->term_data_name) {
+          $term = taxonomy_get_term($item->tid);
+          $term_name = $term->name;
+        }
+        else {
+           $term_name = $item->term_data_name; 
+        }
+        $comment = _linodef_buttonsapi_setdefaultcomment($term_name);
+        $output['data'][$i] = array('tag' => '[#'. $item->tid . $option_string . $comment .']', 'desc' => $term_name);
+      }
+    }
+    $output['name'] = $viewname;
+    $output['element type']['input'] = t('view');
+    $output['element'] = $viewname;
+    return $output;
+  }
+  else {
+    return $view_result;
+  }
+}
+
+/**
  * Get data for button: Show fields by field name.
  *
  * @param string $fieldname
@@ -29,7 +119,7 @@
  *      string
  *          if node/field (value) not found a message is returned which should appear in editor text field (depends on Linodef submodule for editor)
  *
- * @see linodef_editorbuttons_switch()
+ * @see linodef_buttonsapi_rawdata()
  */
 function _linodef_buttonsapi_getfieldsbyfieldname($fieldname, $option_string) {
     // Heed content permissions settings if module is enabled.
@@ -56,8 +146,8 @@
                         // Security and compatibility step.
                         $ergebnis->value = check_plain($ergebnis->value);
                         $node->title = check_plain($node->title);
-                        // Set default comment.
-                        $comment = variable_get('linodef_buttonsapi_comments', 0) ? ' '. str_replace(']', '', $node->title) : '';
+                        // Set default comment. Use node titles since field content could contain new lines.
+                        $comment = _linodef_buttonsapi_setdefaultcomment($node->title);
                         // Make compatible with editor's Javascripts (remove newline). If not done with drupal_to_js() in editorbuttons submodules.
                         $ergebnis->value = str_replace(array("\r\n", "\n", "\r"), ' ', $ergebnis->value);
                         // Multiple values: only insert valuenumber if fieldvalue is not first value.
@@ -122,7 +212,7 @@
  *      string
  *          if content type not found a message is returned which should appear in editor text field (depends on Linodef submodule for editor)
  *
- * @see linodef_editorbuttons_switch()
+ * @see linodef_buttonsapi_rawdata()
  */
 function _linodef_buttonsapi_getnodesbycontenttype($contenttype, $option_string) {
     if ($ctype_obj = node_get_types('type', $contenttype)) {
@@ -134,8 +224,7 @@
             if ($ergebnis->nid && $node = node_load($ergebnis->nid)) {
                 if (node_access('view', $node)) {
                     $node->title = check_plain($node->title);
-                    // Set default comment.
-                    $comment = variable_get('linodef_buttonsapi_comments', 0) ? ' '. str_replace(']', '', $node->title) : '';
+                    $comment = _linodef_buttonsapi_setdefaultcomment($node->title);
                     $output['data'][$i] = array('tag' => '[#'. $node->nid . $option_string . $comment .']', 'desc' => $node->title);
                 }
                 else {
@@ -178,15 +267,14 @@
  *      string
  *          if vid not found a message is returned which should appear in editor text field (depends on Linodef submodule for editor)
  *
- * @see linodef_editorbuttons_switch()
+ * @see linodef_buttonsapi_rawdata()
  */
 function _linodef_buttonsapi_gettermsbyvid($vid, $option_string) {
   if (module_exists('taxonomy')) {
     if ($terms = taxonomy_get_tree($vid)) {
         $output = array();
         for ($i = 0; $i < count($terms); $i++) {
-            // Set default comment.
-            $comment = variable_get('linodef_buttonsapi_comments', 0) ? ' '. str_replace(']', '', $terms[$i]->name) : '';
+            $comment = _linodef_buttonsapi_setdefaultcomment($terms[$i]->name);
             $output['data'][$i] = array('tag' => '[#'. $terms[$i]->tid .',tid'. $option_string . $comment .']', 'desc' => $terms[$i]->name);
         }
         if (!is_array($output['data'])) {
@@ -207,4 +295,67 @@
   else {
     return linodef_message(array('warning' => array('activate module' => 'Taxonomy'), 'explanation' => array('option to use in tag id' => array('%option' => 'tid', '%id' => $tid))), TRUE);
   }
+}
+
+/**
+ * Helper function for _linodef_buttonsapi_getelementssbyviewname().
+ * Investigate the result of a view.
+ *
+ * @param string $viewname
+ *      The name of the view to retrieve the data from.
+ * @param string $display_id
+ *      The display id. On the edit page for the view in question, you'll find
+ *      a list of displays at the left side of the control area. "Defaults"
+ *      will be at the top of that list. Hover your cursor over the name of the
+ *      display you want to use. An URL will appear in the status bar of your
+ *      browser. This is usually at the bottom of the window, in the chrome.
+ *      Everything after #views-tab- is the display ID, e.g. page_1.
+ * @param array $args
+ *      Array of arguments.
+ * @return
+ *      array
+ *          An array containing an object for each view item. For details see http://drupal.org/node/342132 and http://views.doc.logrus.com/classview.html.
+ *      string
+ *          If the view is not found a message is returned which should appear in editor text field (depends on Linodef submodule for editor)
+ *
+ * @see linodef_buttonsapi_rawdata()
+ */
+function _linodef_buttonsapi_get_view_result($viewname, $display_id = NULL, $args = NULL) {
+  if (module_exists('views')) {
+    $view = views_get_view($viewname);
+    if (is_object($view)) {
+      if (is_array($args)) {
+        $view->set_arguments($args);
+      }
+      if (is_string($display_id)) {
+        $view->set_display($display_id);
+      }
+      else {
+        // Heed pager setting.
+        $view->init_display();
+      }
+      $view->pre_execute();
+      $view->execute();
+      return $view->result;
+    }
+    else {
+      return linodef_message(array('error' => array('view' => $viewname, 'type' => 'not found')), TRUE);
+    }
+  }
+  else {
+    return linodef_message(array('warning' => array('activate module' => 'Views'), 'explanation' => array('option to use in tag id' => array('%option' => 'viewid', '%id' => $viewname))), TRUE);
+  }
+}
+
+/**
+ * Insert default comment.
+ *
+ * @param string $comment
+ *      The content of the comment, mostly short names.
+ * @return
+ *      string
+ *          If comment admin option is set true than the comment with a prepended space. Otherwise an empty string.
+ */
+function _linodef_buttonsapi_setdefaultcomment($comment) {
+  return variable_get('linodef_buttonsapi_comments', 0) ? ' '. str_replace(']', '', $comment) : '';
 }
\ No newline at end of file
Index: linodef_buttonsapi.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/linodef/modules/linodef_buttonsapi/Attic/linodef_buttonsapi.module,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 linodef_buttonsapi.module
--- linodef_buttonsapi.module	30 Nov 2008 13:00:31 -0000	1.1.2.2
+++ linodef_buttonsapi.module	11 May 2009 15:52:47 -0000
@@ -9,7 +9,7 @@
  * 2) Buttons
  *  a. Provide a popup window.
  *  b. Provide autocomplete button.
- * 4) Admin settings.
+ * 3) Admin settings.
  *
  * @package Linodef
  * @author Roi Danton
@@ -86,20 +86,31 @@
     $options = linodef_buttonsapi_validate_tagoptions($option_string_path);
     $option_string = $options ? ','. implode(',', $options) : '';
 
-    if (substr($drupal_element, 0, 6) == "field_") {
+    // Retrieve element list using views.
+    if (substr($drupal_element, 0, 5) == "view_") {
+        $drupal_element = substr($drupal_element, 5);
+        $output = _linodef_buttonsapi_getelementssbyviewname($drupal_element, NULL, NULL, $option_string);
+    }
+    // Backwards compatibility for Linodef versions <RC3.
+    // Fields by name.
+    elseif (substr($drupal_element, 0, 6) == "field_") {
         $output = _linodef_buttonsapi_getfieldsbyfieldname($drupal_element, $option_string);
     }
+    // Taxonomy terms by vid.
     elseif (preg_match("@([0-9]+)@", $drupal_element)) {
+
         // Remove tag options that are not needed for terms: translation.
         if ($options['translation']) { unset($options['translation']); }
-        $option_string = preg_replace("@,translation=([a-zA-Z]{2})@", "", $option_string);
+        $option_string = preg_replace("@,translation=([a-zA-Z]+)@", "", $option_string);
+        
         $output = _linodef_buttonsapi_gettermsbyvid($drupal_element, $option_string);
     }
+    //Nodes by content type.
     else {
         $output = _linodef_buttonsapi_getnodesbycontenttype($drupal_element, $option_string);
     }
     if (is_array($output)) {
-        // If set by terms ID.
+        // If set by the functions above.
         if (is_array($output['options'])) {
             $output['options'] += $options ? $options : array();
         }
@@ -124,10 +135,13 @@
  */
 function linodef_buttonsapi_validate_tagoptions($option_string) {
     if (is_string($option_string) && !empty($option_string)) {
+        // Replace , inside "" before exploding. Replace it back afterwards.
+        $option_string = linodef_prepare_options($option_string);
         if (strpos($option_string, ',') !== FALSE) {
             $options_path = explode(',', $option_string);
         }
         // If modified by linodef_buttonsapi_tagoptions_to_path().
+        // DOESNT WORK WITH NEW TAGOPTIONS LAYOUT, NEEDS TO BE MODIFIED!
         elseif (strpos($option_string, '_') !== FALSE) {
             $options_path = explode('_', $option_string);
         }
@@ -165,8 +179,9 @@
  */
 function linodef_buttonsapi_tagoptions_to_path($options) {
     $output = implode('_', $options);
-    // Make option layout compatible with path: translation.
+    // Make options with values compatible with paths.
     $output = str_replace('=', '-', $output);
+    // todo: remove/modify strings between ""
     return $output;
 }
 
@@ -331,7 +346,7 @@
 }
 
 /**
- * 4) Admin settings.
+ * 3) Admin settings.
  *
  * Implementation of hook_form_[form_id]_alter().
  *

