View source
<?php
function node_patterns($data) {
$files_ct = array(
'modules/node/content_types.inc',
);
$files_n = array(
'modules/node/node.pages.inc',
);
$forms = array();
if ($data) {
if ($data['tag'] == 'node' && isset($data['type'])) {
$forms[] = $data['type'] . '_node_form';
}
}
$actions['node'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Nodes'),
PATTERNS_CREATE => $forms,
PATTERNS_MODIFY => $forms,
PATTERNS_DELETE => array(
'node_delete_confirm',
),
PATTERNS_FILES => $files_n,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'node_patterns_export_all_nodes',
),
);
$actions['content_type'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Content Types'),
PATTERNS_CREATE => array(
'node_type_form',
),
PATTERNS_MODIFY => array(
'node_type_form',
),
PATTERNS_DELETE => array(
'node_type_delete_confirm',
),
PATTERNS_FILES => $files_ct,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'node_patterns_export_all_content_types',
),
);
$actions['content_type_extract_all'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Content Types'),
PATTERNS_MODIFY => array(
'node_type_form',
),
PATTERNS_FILES => $files_ct,
);
return $actions;
}
function node_patterns_export_all_content_types($args = NULL, &$result = NULL) {
$types = _node_types_build()->types;
$result = array();
foreach ($types as $type) {
$type = (array) $type;
$data = array(
'tag' => 'content_type',
);
$data = array_merge($data, $type);
$action = array(
PATTERNS_CREATE => $data,
);
array_push($result, $action);
}
return $result;
}
function node_patterns_export_all_nodes($args = NULL, &$result = NULL) {
$nodes = node_load_multiple(FALSE);
$result = array();
foreach ($nodes as $node) {
$node = (array) $node;
$data = array(
'tag' => 'node',
);
$data = array_merge($data, $node);
$action = array(
PATTERNS_CREATE => $data,
);
array_push($result, $action);
}
return $result;
}
function node_patterns_prepare($action, $tag, &$data) {
if ($tag == 'content_type') {
if ($action === PATTERNS_CREATE or $action === PATTERNS_MODIFY) {
if (isset($data['node_options']) and $data['node_options']) {
$data['node_options'] = array_values($data['node_options']);
}
else {
$data['node_options'] = array(
'status',
'promote',
);
}
}
}
elseif ($tag == 'node') {
global $user;
if (empty($data['nid']) && !empty($data['id'])) {
$data['nid'] = $data['id'];
}
unset($data['id']);
if (empty($data['name']) && !empty($data['author'])) {
$data['name'] = $data['author'];
}
unset($data['author']);
if (empty($data['uid'])) {
$data['uid'] = $user->uid;
}
if (empty($data['name'])) {
$data['name'] = $user->name;
}
if ($action == PATTERNS_CREATE) {
unset($data['nid']);
unset($data['vid']);
$data['node'] = (object) $data;
$data['update'] = FALSE;
}
elseif ($action == PATTERNS_MODIFY) {
$data['update'] = TRUE;
}
elseif ($action == PATTERNS_DELETE) {
$data['update'] = FALSE;
}
}
return patterns_results();
}
function node_patterns_validate($action, $tag, &$data) {
$status = PATTERNS_SUCCESS;
$msg = '';
$result = array();
switch ($tag) {
case 'node':
switch ($action) {
case PATTERNS_CREATE:
if (empty($data['type'])) {
$status = PATTERNS_ERR;
$msg = t('"type" field is required.');
return patterns_results($status, $msg, $result);
}
if (empty($data['title'])) {
$status = PATTERNS_ERR;
$msg = t('"title" field is required.');
return patterns_results($status, $msg, $result);
}
$node_item = db_select('node_type', 'nt')
->fields('nt', array(
'name',
))
->condition('type', $data['type'])
->execute()
->fetchField();
if (!$node_item) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The content type: "%type" does not exist in the system.', array(
'%type' => $data['type'],
)),
);
}
break;
case PATTERNS_DELETE:
case PATTERNS_MODIFY:
if (empty($data['nid']) && empty($data['title'])) {
$status = PATTERNS_ERR;
$msg = t('"id or title" field is required.');
return patterns_results($status, $msg, $result);
}
elseif (!empty($data['nid'])) {
$data['node'] = node_load($data['nid']);
if ($data['node'] === FALSE) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The node: "%oldname" does not exist in the system.', array(
'%oldname' => $data['nid'],
)),
);
}
else {
$data['node'] = (object) $data['node'];
if ($action == PATTERNS_MODIFY) {
if ($data['type'] != $data['node']->type) {
$status = PATTERNS_ERR;
$msg = t("You can't change content type for already existing node.");
return patterns_results($status, $msg, $result);
}
}
}
}
else {
$num_nodeswithSameTitle = db_select('node')
->condition('title', $data['title'])
->countQuery()
->execute()
->fetchField();
if ($num_nodeswithSameTitle > 1) {
$result[] = array(
PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('The node titled: "%title" is not unique in the system.', array(
'%title' => $data['title'],
)),
);
}
elseif ($num_nodeswithSameTitle == 1) {
$node_item = db_select('node', 'nd')
->fields('nd', array(
'nid',
))
->condition('title', $data['title'])
->execute()
->fetchField();
$data['node'] = node_load($node_item);
$data['nid'] = $node_item;
if ($data['node'] === FALSE) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The node: "%oldname" does not exist in the system.', array(
'%oldname' => $data['nid'],
)),
);
}
else {
if ($action == PATTERNS_MODIFY) {
$data['node'] = (object) $data['node'];
if ($data['type'] != $data['node']->type) {
$status = PATTERNS_ERR;
$msg = t("You can't change content type for already existing node.");
return patterns_results($status, $msg, $result);
}
}
}
}
else {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The node: "%oldname" does not exist in the system.', array(
'%oldname' => $data['title'],
)),
);
}
}
break;
}
break;
case 'content_type':
if (empty($data['type'])) {
$status = PATTERNS_ERR;
$msg = t('"type" field is required.');
return patterns_results($status, $msg, $result);
}
switch ($action) {
case PATTERNS_CREATE:
if (empty($data['name'])) {
$status = PATTERNS_ERR;
$msg = t('"name" field is required.');
return patterns_results($status, $msg, $result);
}
$set = db_select('node_type', 'nt')
->fields('nt', array(
'type',
))
->condition('name', $data['name'])
->execute()
->fetchField();
if ($set) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The content_type named "%name" already exists in the system', array(
'%name' => $data['name'],
)),
);
}
$set = db_select('node_type', 'nt')
->fields('nt', array(
'name',
))
->condition('type', $data['type'])
->execute()
->fetchField();
if ($set) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The type: %type already exists in the system', array(
'%type' => $data['type'],
)),
);
}
break;
case PATTERNS_MODIFY:
$set = db_select('node_type', 'nt')
->fields('nt', array(
'type',
))
->condition('type', $data['type'])
->execute()
->fetchField();
if (!$set) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The content type: "%oldname" does not exist in the system.', array(
'%oldname' => $data['name'],
)),
);
}
if (isset($data['name'])) {
$set = db_select('node_type', 'nt')
->fields('nt', array(
'type',
))
->condition('name', $data['name'])
->execute()
->fetchField();
if ($set) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The content_type named "%name" already exists in the system', array(
'%name' => $data['name'],
)),
);
}
}
break;
case PATTERNS_DELETE:
$set = db_select('node_type', 'nt')
->fields('nt', array(
'type',
))
->condition('type', $data['type'])
->execute()
->fetchField();
if (!$set) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The content type: "%oldname" does not exist in the system.', array(
'%oldname' => $data['name'],
)),
);
}
break;
}
break;
}
return patterns_results($status, $msg, $result);
}
function node_patterns_callbacks($action, $tag, &$data) {
if ($tag == 'node') {
if ($action === PATTERNS_CREATE or $action === PATTERNS_MODIFY) {
$result = array(
$data['type'] . '_node_form',
);
}
else {
$result = array(
'node_delete_confirm',
);
}
}
elseif ($tag == 'content_type') {
if ($action === PATTERNS_CREATE or $action === PATTERNS_MODIFY) {
$result = array(
'node_type_form',
);
}
elseif ($action === PATTERNS_DELETE) {
$result = array(
'node_type_delete_confirm',
);
}
}
return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
}
function node_patterns_build($action, $form_id, &$data = NULL, &$a) {
$status = PATTERNS_SUCCESS;
$msg = '';
$result = NULL;
module_load_include('inc', 'content', 'includes/content.admin');
module_load_include('inc', 'node', 'content_types');
if ($form_id == 'node_delete_confirm') {
$data['confirm'] = 1;
}
elseif ($form_id == 'node_type_form') {
module_load_include('inc', 'node', 'content_types');
$type = $data['type'];
$name = $data['name'];
if (isset($data['has_body']) && !$data['has_body']) {
$data['body_label'] = '';
}
}
elseif ($form_id == 'node_type_delete_confirm') {
$data['op'] = t('Delete');
$data['confirm'] = 1;
}
else {
$data['changed'] = time();
$data['op'] = t('Save');
$node = clone $data['node'];
$type = $data['type'];
$form_id = $type . '_node_form';
$form_state = array(
'build_info' => array(
'args' => array(
$node,
),
'files' => array(
'menu' => 'modules/node/node.pages.inc',
),
),
'rebuild' => FALSE,
'rebuild_info' => array(),
'redirect' => NULL,
'temporary' => array(),
'submitted' => FALSE,
'executed' => FALSE,
'programmed' => FALSE,
'cache' => FALSE,
'method' => 'post',
'groups' => array(),
'buttons' => array(),
'input' => array(),
);
require_once 'modules/node/node.pages.inc';
$form = drupal_retrieve_form($form_id, $form_state);
drupal_prepare_form($form_id, $form, $form_state);
}
$result = $data;
return patterns_results($status, $msg, $result);
}
function node_patterns_params($action, $form_id, &$data = NULL, &$a) {
$status = PATTERNS_SUCCESS;
$msg = '';
$result = NULL;
if ($form_id == 'node_delete_confirm') {
$result = array(
(object) $data,
);
}
elseif ($action === PATTERNS_MODIFY and $form_id == 'node_type_form' or $form_id == 'node_type_delete_confirm') {
$types = (object) _node_types_build();
$type = $types->types[$data['type']];
$result = array(
$type,
);
}
else {
$data['node']->language = 'und';
$result = clone $data['node'];
}
return patterns_results($status, $msg, $result);
}
function node_patterns_cleanup($action, $tag, &$data) {
_field_info_collate_types(true);
return patterns_results();
}