/[drupal]/contributions/modules/inventory/inventory.module
ViewVC logotype

Diff of /contributions/modules/inventory/inventory.module

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

revision 1.1, Wed Feb 28 19:54:38 2007 UTC revision 1.2, Wed Feb 28 20:02:13 2007 UTC
# Line 0  Line 1 
1    <?php
2    
3    /**
4    * Declare information about a field type.
5    */
6    function inventory_field_info() {
7      return array(
8        'inventory_field' => array(
9          'label' => t('Inventory field'),
10        ),
11      );
12    }
13    
14    /**
15    * Handle the parameters for a field.
16    */
17    function inventory_field_settings($op, $field) {
18      switch ($op) {
19        case 'form':
20          $form = array();
21          $form['inventory_items_const'] = array(
22            '#type'          => 'textarea',
23            '#title'         => t('Allowed inventory item list'),
24            '#description'   => t('The possible values this field can contain. Enter one value per line.'),
25            '#default_value' => $field['inventory_items_const'],
26          );
27          $form['advanced_options'] = array(
28            '#type' => 'fieldset',
29            '#title' => t('Php code'),
30            '#collapsible' => TRUE,
31            '#collapsed' => empty($field['inventory_items_php']),
32          );
33          $form['advanced_options']['inventory_items_php'] = array(
34            '#type'          => 'textarea',
35            '#title'         => t('Code'),
36            '#description'   => t('Advanced Usage Only: PHP code that returns a keyed array of allowed inventory items. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the array returned by this code will override the allowed values list above.'),
37            '#default_value' => $field['inventory_items_php'],
38          );
39          $form['inventory_item_min'] = array(
40            '#type'          => 'textfield',
41            '#title'         => t('Minimum number of an item'),
42            '#description'   => t('Please enter a non-negative integer'),
43            '#default_value' => empty($field['inventory_items_min']) ? 0 : $field['inventory_items_min'],
44          );
45          $form['inventory_item_max'] = array(
46            '#type'          => 'textfield',
47            '#title'         => t('Maximum number of an item'),
48            '#description'   => t('Please enter a positive integer that is not lower than the minimum number.'),
49            '#default_value' => empty($field['inventory_item_max']) ? 1 : $field['inventory_item_max'],
50          );
51          return $form;
52        case 'save':
53          return array('inventory_items_const', 'inventory_items_php', 'inventory_item_min', 'inventory_item_max');
54        case 'validate':
55          if (empty($field['inventory_items_const']) && empty($form['inventory_items_php'])) {
56            form_set_error('inventory_items_const', t('You have to provide a list of available values either by typing the items in manually or giving a PHP code that will return them.'));
57          }
58          elseif (empty($field['inventory_items_const'])) {
59            $array = drupal_eval($field['inventory_items_php']);
60            if (!is_array($array) || empty($array)) {
61              form_set_error('inventory_items_const', t('The PHP snippet you have provided is not returning an array or the array itself is empty.'));
62            }
63          }
64          if (!is_numeric($field['inventory_item_max']) || intval($field['inventory_item_max']) != $field['inventory_item_max'] || $field['inventory_item_max'] < 1) {
65            form_set_error('inventory_item_max', t('The max number must be a positive integer value.'));
66          }
67          if (!is_numeric($field['inventory_item_min']) || intval($field['inventory_item_min']) != $field['inventory_item_min'] || $field['inventory_item_min'] < 0) {
68            form_set_error('inventory_item_min', t('The min number must be a non negative integer value.'));
69          }
70          if (is_numeric($field['inventory_item_min']) && is_numeric($field['inventory_item_max']) && intval($field['inventory_item_min']) == $field['inventory_item_min'] && intval($field['inventory_item_max']) == $field['inventory_item_max'] && intval($field['inventory_item_min']) > intval($field['inventory_item_max'])) {
71            form_set_error('inventory_item_max', t('The max number must be at least as much as the minimum.'));
72          }
73          return;
74        case 'callbacks':
75          return array('view' => CONTENT_CALLBACK_CUSTOM);
76          break;
77      }
78    }
79    
80    /**
81    * Handling the behavior of a field type.
82    */
83    function inventory_field($op, &$node, $field, &$node_field, $teaser, $page) {
84      switch ($op) {
85        case 'view':
86          $fieldset = _inventory_get_fields($field);
87          $rs = db_query("SELECT name, count FROM {inventory_items} WHERE vid = %d AND fieldset = '%s' ORDER BY name ASC", $node->vid, $field['field_name']);
88          while ($row = db_fetch_object($rs)) {
89            if (in_array($row->name, $fieldset)) {
90              $node_field[] = array($row->name => $row->count);
91            }
92          }
93          foreach ($node_field as $delta => $item) {
94            $node_field[$delta]['view'] = content_format($field, $item, 'default', $node);
95          }
96          return theme('field', $node, $field, $node_field, $teaser, $page);
97        case 'insert': case 'update':
98          // deleting because if the fields are determined by the PHP snippet and the snippet is
99          // now giving a different fieldset than before, the deprecated fields must get ridden of
100          db_query("DELETE FROM {inventory_items} WHERE vid = %d AND fieldset = '%s'", $node->vid, $field['field_name']);
101          foreach ($node_field as $item => $count) {
102            db_query("INSERT INTO {inventory_items} (nid, vid, fieldset, name, count) VALUES(%d, %d, '%s', '%s', %d)", $node->nid, $node->vid, $field['field_name'], $item, $count);
103          }
104          break;
105        case 'delete':
106          db_query("DELETE FROM {inventory_items} WHERE nid = %d", $node->nid);
107          break;
108      }
109    }
110    
111    /**
112     * Implementation of hook_field_formatter_info().
113     */
114    function inventory_field_formatter_info() {
115      $formatters = array(
116        'default' => array(
117          'label' => t('Default'),
118          'field types' => array('inventory_field'),
119        ),
120      );
121      return $formatters;
122    }
123    
124    /**
125     * Implementation of hook_field_formatter().
126     */
127    function inventory_field_formatter($field, $item, $formatter) {
128      // switch formatter in case we implement multiple formatters.
129      switch ($formatter) {
130        case 'default':  return theme('inventory_formatter_default', $item);
131      }
132    }
133    
134    /**
135     * callback function for the theme
136     */
137    function theme_inventory_formatter_default($item) {
138      $output = '';
139      if (is_array($item)) {
140        $keys = array_keys($item);
141        $output .= $keys[0] .": ". ($item[$keys[0]] == 0 ? t('None') : $item[$keys[0]]);
142      }
143      return $output;
144    }
145    
146    /**
147    * Declare information about a widget.
148    */
149    function inventory_widget_info() {
150      return array(
151        'inventory_manual' => array(
152          'label' => t('Manual Input'),
153          'field types' => array('inventory_field'),
154        ),
155      );
156    }
157    
158    /**
159    * Define the behavior of a widget.
160    */
161    function inventory_widget($op, &$node, $field, &$node_field) {
162      $fieldname = $field['field_name'];
163      switch ($op) {
164        case 'prepare form values':
165          $fieldset = _inventory_get_fields($field);
166          $rs = db_query("SELECT name, count FROM {inventory_items} WHERE vid = %d AND fieldset = '%s' ORDER BY name ASC", $node->vid, $field['field_name']);
167          while ($row = db_fetch_object($rs)) {
168            if (in_array($row->name, $fieldset)) {
169              $node_field[$row->name] = $row->count;
170            }
171          }
172          break;
173        case 'form':
174          $form = array();
175          $form[$fieldname] = array(
176            '#type'        => 'fieldset',
177            '#title'       => $field['widget']['label'],
178            '#weight'      => $field['widget']['weight'],
179            '#collapsible' => TRUE,
180            '#collapsed'   => FALSE,
181            '#tree'        => TRUE,
182          );
183          $options = range($field['inventory_item_min'], $field['inventory_item_max']);
184          if (empty($options[0])) {
185            $options[0] = t('None');
186          }
187    
188          if ($fields = _inventory_get_fields($field)) {
189            foreach ($fields as $item) {
190              $form[$fieldname][$item] = array(
191                '#type'          => 'select',
192                '#title'         => $item,
193                '#options'       => $options,
194                '#default_value' => $node_field[$item],
195              );
196            }
197            return $form;
198          }
199          return array();
200      }
201    }
202    
203    /**
204     * Collects the available fields and returns it in an array
205     */
206    function _inventory_get_fields($field) {
207      static $field_set;
208    
209      if (isset($field_set[$field['field_name']])) {
210        return $field_set[$field['field_name']];
211      }
212    
213      $field_set[$field['field_name']] = array();
214      if (!empty($field['inventory_items_php'])) {
215        $result = eval($field['inventory_items_php']);
216        if (is_array($result)) {
217          $field_set[$field['field_name']] = $result;
218        }
219      }
220      if (empty($field_set[$field['field_name']])) {
221        $list = explode("\n", $field['inventory_items_const']);
222        $list = array_map('trim', $list);
223        $list = array_filter($list, 'strlen');
224        foreach ($list as $item) {
225          $field_set[$field['field_name']][] = $item;
226        }
227      }
228      return $field_set[$field['field_name']];
229    }

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

  ViewVC Help
Powered by ViewVC 1.1.2