| 1 |
<?php
|
| 2 |
// $Id:$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @param string $node - node or valid node type string for new nodes.
|
| 6 |
* @param Array $form_values
|
| 7 |
*
|
| 8 |
* Usage:
|
| 9 |
* $form_values = array(
|
| 10 |
* 'title' => 'Example title',
|
| 11 |
* 'body' => 'Example body.',
|
| 12 |
* 'field_image' => array('upload' => URL),
|
| 13 |
* );
|
| 14 |
*/
|
| 15 |
function mtk_node_save($node, $form_values = array()) {
|
| 16 |
if (is_string($node)) {
|
| 17 |
$node = array('type' => $node);
|
| 18 |
}
|
| 19 |
else {
|
| 20 |
$node = (array) $node;
|
| 21 |
}
|
| 22 |
$form_id = $node['type'] .'_node_form';
|
| 23 |
|
| 24 |
// If uid is set load user and fill in name.
|
| 25 |
// To circumvent uid reset in node_submit().
|
| 26 |
// @todo: roll patch for D7.
|
| 27 |
if ($form_values['uid']) {
|
| 28 |
$account = user_load(array('uid' => $form_values['uid']));
|
| 29 |
$form_values['name'] = $account->name;
|
| 30 |
}
|
| 31 |
// Store changed date - we will set it manually at end of function.
|
| 32 |
if ($form_values['changed']) {
|
| 33 |
$changed = $form_values['changed'];
|
| 34 |
}
|
| 35 |
else if ($form_values['created']) {
|
| 36 |
$changed = $form_values['created'];
|
| 37 |
}
|
| 38 |
// Store created date to be set as well
|
| 39 |
if($form_values['created']) {
|
| 40 |
$node['created'] = $form_values['created'];
|
| 41 |
}
|
| 42 |
|
| 43 |
// Validate og group settings.
|
| 44 |
if (!mtk_node_validate_og($form_values['og_groups'], $form_values['uid'])) {
|
| 45 |
drupal_set_message('OG settings not valid, couldn\'t save node. UID '. $form_values['uid'] .' '. dprint_r($form_values['og_groups'], true), 'error');
|
| 46 |
return FALSE;
|
| 47 |
}
|
| 48 |
|
| 49 |
// Convert file uploads (http://drupal.org/node/201594)
|
| 50 |
// $form_values[field_name][upload] = array(http://example.com/img.jpg)
|
| 51 |
// @todo: multiple file uploads not tested.
|
| 52 |
foreach ($form_values as $field_name => $value) {
|
| 53 |
if (is_array($value) && is_array($value['upload'])) {
|
| 54 |
foreach ($value['upload'] as $upload) {
|
| 55 |
if (!$file_data = file_get_contents($upload)) {
|
| 56 |
drupal_set_message(t('Couldn\'t read file !url', array('!url' => $upload)), 'error');
|
| 57 |
return FALSE;
|
| 58 |
}
|
| 59 |
if (!$file_temp = file_save_data($file_data, file_directory_path().'/'.basename($upload), FILE_EXISTS_RENAME)) {
|
| 60 |
drupal_set_message(t('Couldn\'t save file.'), 'error');
|
| 61 |
return FALSE;
|
| 62 |
}
|
| 63 |
$i = 0;
|
| 64 |
$file = array(
|
| 65 |
'fid' => 'upload_'. $i,
|
| 66 |
'title' => basename($file_temp),
|
| 67 |
'filename' => basename($file_temp),
|
| 68 |
'filepath' => $file_temp,
|
| 69 |
'filesize' => filesize($file_temp),
|
| 70 |
'list' => 1, // always list
|
| 71 |
'filemime' => mimedetect_mime($file_temp),
|
| 72 |
);
|
| 73 |
// File upload likes objects, others like arrays.
|
| 74 |
$file = ($field_name == 'files') ? (object) $file : $file;
|
| 75 |
$node[$field_name]['upload_'. $i] = $file;
|
| 76 |
$i++;
|
| 77 |
}
|
| 78 |
unset($form_values[$field_name]);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
// This allows us to force validation on each iteration of drupal_execute
|
| 83 |
// For more information, see includes/form.inc ~line 387
|
| 84 |
global $validated_forms;
|
| 85 |
unset($validated_forms[$form_id]);
|
| 86 |
|
| 87 |
$path = explode('/', drupal_execute($form_id, $form_values, $node));
|
| 88 |
if (is_numeric($path[1])) {
|
| 89 |
$node = node_load($path[1]);
|
| 90 |
if ($changed) {
|
| 91 |
db_query('UPDATE {node} SET changed = %d WHERE nid = %d', $changed, $node->nid);
|
| 92 |
}
|
| 93 |
return $node;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Validation helper for og settings.
|
| 99 |
* This is for avoiding errors that result in
|
| 100 |
* 'An illegal choice has been detected. Please contact the site administrator.'
|
| 101 |
*/
|
| 102 |
function mtk_node_validate_og($groups, $uid) {
|
| 103 |
if (is_array($groups)) {
|
| 104 |
// First, check whether node owner is member of group.
|
| 105 |
$account = user_load(array('uid' => $uid));
|
| 106 |
if (!$account->uid) {
|
| 107 |
return FALSE;
|
| 108 |
}
|
| 109 |
foreach ($groups as $group_nid) {
|
| 110 |
if (!array_key_exists($group_nid, $account->og_groups)) {
|
| 111 |
return FALSE;
|
| 112 |
}
|
| 113 |
}
|
| 114 |
// Second, check whether current user is member of group.
|
| 115 |
// This is necessary, because OG won't allow posts to groups that the current user is not subscribed to.
|
| 116 |
global $user;
|
| 117 |
foreach ($groups as $group_nid) {
|
| 118 |
if (!array_key_exists($group_nid, $user->og_groups)) {
|
| 119 |
return FALSE;
|
| 120 |
}
|
| 121 |
}
|
| 122 |
}
|
| 123 |
return TRUE;
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Test upload.
|
| 128 |
*/
|
| 129 |
function mtk_test_upload() {
|
| 130 |
$node['title'] = 'Test node';
|
| 131 |
$node['files']['upload'] = array(url(drupal_get_path('module', 'mtk') .'/crud/test.txt', NULL, NULL, TRUE));
|
| 132 |
drupal_set_message(dprint_r($node, true));
|
| 133 |
$node = mtk_node_save('book', $node);
|
| 134 |
drupal_set_message(dprint_r($node, true));
|
| 135 |
}
|