| 1 |
<?php
|
| 2 |
// $Id: node_factory.module,v 1.2 2008/08/14 08:58:25 clemenstolboom Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides utility functions for creating nodes on the fly.
|
| 7 |
*
|
| 8 |
* When wanting to create nodes on the fly a lot of code is needed.
|
| 9 |
* This module tries to ease the pain by providing some helper functions.
|
| 10 |
*
|
| 11 |
* Just call create_node then set_value and finally save_node
|
| 12 |
*
|
| 13 |
* $edit = node_factory_create_node( 'example');
|
| 14 |
*
|
| 15 |
* node_factory_set_value( $edit, 'oneliner', 'hello world');
|
| 16 |
*
|
| 17 |
* node_factory_save_node( $edit);
|
| 18 |
*
|
| 19 |
* Thats all
|
| 20 |
*
|
| 21 |
* Some helper functions are
|
| 22 |
*
|
| 23 |
* @see node_factory_set_cck_value
|
| 24 |
* @see node_factory_set_cck_noderef
|
| 25 |
*
|
| 26 |
* THIS CODE runs ok
|
| 27 |
*
|
| 28 |
* $edit = node_factory_create_node('cck_noderef');
|
| 29 |
* node_factory_set_cck_value( $edit, 'title', 'test-' . date('y-m-d G:i:s'));
|
| 30 |
* node_factory_set_cck_value( $edit, 'field_node_ref_auto', array( array( 'nid' => 47)));
|
| 31 |
* node_factory_set_cck_value( $edit, 'field_node_ref_select', array( 'nids' => 47));
|
| 32 |
* node_factory_save_node( $edit);
|
| 33 |
*
|
| 34 |
* ====================================
|
| 35 |
* $edit = node_factory_create_node('cck_radios');
|
| 36 |
* node_factory_set_cck_value( $edit, 'title', 'test-' . date('y-m-d G:i:s'));
|
| 37 |
* node_factory_set_cck_value( $edit, 'field_radio', array( 'key' => 'A'));
|
| 38 |
* node_factory_save_node( $edit);
|
| 39 |
*
|
| 40 |
* -- OR --
|
| 41 |
*
|
| 42 |
* $edit = node_factory_create_node('cck_options');
|
| 43 |
* node_factory_set_value( $edit, 'title', 'test-' . date('y-m-d G:i:s'));
|
| 44 |
* node_factory_set_value( $edit, 'field_options', 'A');
|
| 45 |
* node_factory_save_node( $edit);
|
| 46 |
*
|
| 47 |
* ====================================
|
| 48 |
*
|
| 49 |
* When having MULTIPLE values we have to escape to set_cck_value:
|
| 50 |
*
|
| 51 |
* $edit = node_factory_create_node('cck_radios');
|
| 52 |
* node_factory_set_value( $edit, 'title', 'test-' . date('y-m-d G:i:s'));
|
| 53 |
* node_factory_set_cck_value( $edit, 'field_radio', array( 'keys' => array( 'A' => 'A', 'B' => 'B')));
|
| 54 |
* node_factory_save_node( $edit);
|
| 55 |
*/
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Create a partly initialized (node-like) array
|
| 59 |
*
|
| 60 |
* @param $nodetype
|
| 61 |
* A string containing a valid node type
|
| 62 |
* @return
|
| 63 |
* An array containing parts of the given nodetype
|
| 64 |
*/
|
| 65 |
function node_factory_create_node( $nodetype){
|
| 66 |
// The current user is used as the creator
|
| 67 |
global $user;
|
| 68 |
|
| 69 |
$edit= array();
|
| 70 |
|
| 71 |
// store the type so functions can get called with the right type
|
| 72 |
$edit[ 'type']= $nodetype;
|
| 73 |
|
| 74 |
// get the node type defaults
|
| 75 |
$node_type_default = variable_get('node_options_'. $edit['type'], array('status', 'published'));
|
| 76 |
|
| 77 |
// default settings
|
| 78 |
$edit['uid'] = $user->uid;
|
| 79 |
$edit['name'] = $user->name;
|
| 80 |
$edit['promote'] = in_array('promote', $node_type_default);
|
| 81 |
$edit['comment'] = variable_get('comment_'. $edit['type'], 2);
|
| 82 |
$edit['revision'] = in_array('revision', $node_type_default);
|
| 83 |
$edit['format'] = FILTER_FORMAT_DEFAULT;
|
| 84 |
$edit['status'] = in_array('status', $node_type_default);
|
| 85 |
|
| 86 |
return $edit;
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Sets values without knowing what an array is
|
| 91 |
*
|
| 92 |
* @param $edit
|
| 93 |
* Array containing a node-like structure. Is by reference
|
| 94 |
* @param $name
|
| 95 |
* name of the field to set
|
| 96 |
* @param $value
|
| 97 |
* value to set
|
| 98 |
* @return
|
| 99 |
* nothing
|
| 100 |
*/
|
| 101 |
function node_factory_set_value( &$edit, $name, $value){
|
| 102 |
if ( module_exists( 'content')) {
|
| 103 |
$field = content_fields( $name, $edit['type']);
|
| 104 |
|
| 105 |
if( isset( $field)){
|
| 106 |
//if( function_exists('dsm')) dsm( $field);
|
| 107 |
|
| 108 |
// Thanks to sime: see http://drupal.org/node/232121
|
| 109 |
switch ($field['type']) {
|
| 110 |
/*
|
| 111 |
case 'userreference':
|
| 112 |
$key = 'uids';
|
| 113 |
*/
|
| 114 |
case 'nodereference':
|
| 115 |
if ( !isset($key)) {
|
| 116 |
$key = 'nid';
|
| 117 |
}
|
| 118 |
|
| 119 |
if ( $field['multiple'] == 1) {
|
| 120 |
// make plural
|
| 121 |
$key.= 's';
|
| 122 |
$edit[ $name][$key] = array($value);
|
| 123 |
}
|
| 124 |
else {
|
| 125 |
if( $field['widget']['type']== 'nodereference_select' || $field['widget']['type']== 'userreference_select') {
|
| 126 |
$edit[ $name][$key] = $value;
|
| 127 |
}
|
| 128 |
else {
|
| 129 |
// autocomplete ????
|
| 130 |
$edit[ $name] = array(array($key => $value));
|
| 131 |
}
|
| 132 |
}
|
| 133 |
return;
|
| 134 |
|
| 135 |
break;
|
| 136 |
case 'text':
|
| 137 |
case 'number_integer':
|
| 138 |
if ( $field['multiple'] == 1) {
|
| 139 |
$edit[ $name]['keys'][] = $value;
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
$edit[ $name]['key'] = $value;
|
| 143 |
}
|
| 144 |
return;
|
| 145 |
|
| 146 |
default:
|
| 147 |
drupal_set_message( t('node_factory: Not yet implemented for cck field "%field". use node_factory_set_cck_value instead', array( '%field'=> $name)), 'error');
|
| 148 |
return;
|
| 149 |
|
| 150 |
break;
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
if( $name == 'og_groups') {
|
| 156 |
if ( is_array( $value)) {
|
| 157 |
// make sure keys == values
|
| 158 |
$value = array_combine( array_values( $value) , array_values( $value));
|
| 159 |
}
|
| 160 |
else {
|
| 161 |
$value = array( $value => $value);
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
// default behaviour
|
| 166 |
$edit[ $name] = $value;
|
| 167 |
}
|
| 168 |
|
| 169 |
function node_factory_add_value( $edit, $name, $value) {
|
| 170 |
drupal_set_message( 'ERROR: Not yet implemented. This is for cck multiple fields', 'error');
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* helper function for cck 'simple' fields.
|
| 175 |
*
|
| 176 |
* Function is changed to focus on a real implementaiton of node_factory_set_value
|
| 177 |
*
|
| 178 |
* This function must now be used explicit with an array
|
| 179 |
*
|
| 180 |
* @see node_factory_set_value for a better approach
|
| 181 |
*
|
| 182 |
* @param array $edit array of node field values
|
| 183 |
* @param string $name field name to use
|
| 184 |
* @param array $value ckk specific array
|
| 185 |
*/
|
| 186 |
function node_factory_set_cck_value( &$edit, $name, $value){
|
| 187 |
if( strpos($name, 'field_') != 0) {
|
| 188 |
drupal_set_message( t('node_factory API change!!! Prepend %name with "field_"', array( '%name' => $name), 'error'));
|
| 189 |
}
|
| 190 |
|
| 191 |
if( !is_array($value)) {
|
| 192 |
drupal_set_message( t('node_factory API change node_factory_set_cck_value !!!'."\n".'Param $value must be an array! Is %type', array( '%type'=> gettype($value))), 'error');
|
| 193 |
}
|
| 194 |
|
| 195 |
$edit[ $name] = $value;
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* helper function for cck node ref field
|
| 200 |
*
|
| 201 |
* When using a CCK Node reference field be aware of setting
|
| 202 |
* the field type to 'Auto Complete' otherwise the new value
|
| 203 |
* is not stored yet.
|
| 204 |
*/
|
| 205 |
function node_factory_set_noderef_value( &$edit, $name, $value){
|
| 206 |
drupal_set_message( t('deprecated. node_factory API change: node_factory_set_noderef_value!!! Use node_factory_set_cck_value instead'));
|
| 207 |
$edit[ 'field_'. $name] = array( array( 'nid' => $value));
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* prepared the node system and saves the node
|
| 212 |
*
|
| 213 |
* A message is displayed upon success or failure
|
| 214 |
*
|
| 215 |
* @return
|
| 216 |
* nid in case of success
|
| 217 |
* FALSE in case of failure
|
| 218 |
*/
|
| 219 |
function node_factory_save_node( $edit, $succes_message= TRUE){
|
| 220 |
// save proces
|
| 221 |
if( function_exists('dsm')) dsm( $edit);
|
| 222 |
//TODO: use node_prepare?!?
|
| 223 |
$new_node = node_submit( $edit);
|
| 224 |
node_save($new_node);
|
| 225 |
if ($new_node->nid) {
|
| 226 |
if( $succes_message){
|
| 227 |
drupal_set_message( t('Automatic produced %node_type with title %title', array( '%node_type' => $new_node->type, '%title' => $new_node->title)));
|
| 228 |
}
|
| 229 |
return $new_node->nid;
|
| 230 |
} else {
|
| 231 |
drupal_set_message( t('Cannot create %new_node', array( '%new_node'=> $new_node->type)), 'error');
|
| 232 |
return FALSE;
|
| 233 |
}
|
| 234 |
}
|
| 235 |
|
| 236 |
/*
|
| 237 |
function node_factory_nodeapi( &$node, $op, $a3 = NULL, $a4 = NULL){
|
| 238 |
if( $op== 'submit'){
|
| 239 |
dsm( $_POST);
|
| 240 |
}
|
| 241 |
}
|
| 242 |
*/
|