View source
<?php
function cmis_content_menu() {
$items = array();
$items['cmis/create'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => t('Create CMIS Content'),
'page callback' => 'cmis_content_create',
'access callback' => 'user_access',
'access arguments' => array(
'access cmis',
),
);
$items['cmis/get'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'cmis_content_get',
'access callback' => 'user_access',
'access arguments' => array(
'access cmis',
),
);
$items['cmis/block_settings_more_items_js'] = array(
'page callback' => 'cmis_block_settings_more_items_js',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function cmis_content_theme() {
return array(
'cmis_content_create_form' => array(
'arguments' => array(
'form' => null,
),
),
'cmis_content_block_settings' => array(
'arguments' => array(
'form' => NULL,
),
),
);
}
function theme_cmis_content_create_form($form) {
$rows = array(
array(
drupal_render($form['create']['path']),
),
array(
drupal_render($form['create']['name']),
),
array(
drupal_render($form['create']['content']),
),
array(
drupal_render($form['create']['submit']),
),
);
$header = array(
'',
);
$output = theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
function theme_cmis_content_block_settings($form) {
return drupal_render($form);
}
function cmis_content_create() {
drupal_add_css(drupal_get_path('module', 'cmis_content') . '/cmis_content.css');
$contents = '';
$contents .= drupal_get_form('cmis_content_create_form');
return $contents;
}
function cmis_content_create_form($form_state) {
$parts = explode('/', $_GET['q']);
$path = implode('/', array_slice($parts, 2));
$form['#theme'] = 'cmis_content_create_form';
$form['create']['path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => '/' . $path,
'#autocomplete_path' => 'cmis/autocomplete',
'#size' => 70,
);
$form['create']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 70,
);
$form['create']['content'] = array(
'#type' => 'textarea',
'#title' => t('Content'),
);
$form['create']['submit'] = array(
'#type' => 'submit',
'#name' => 'browse',
'#default_value' => 'Create',
);
return $form;
}
function cmis_content_create_form_submit($form, &$form_state) {
module_load_include('api.inc', 'cmis');
$path = $form_state['values']['path'];
$name = $form_state['values']['name'];
$content = $form_state['values']['content'];
$repository = cmisapi_getRepositoryInfo();
if (!$path) {
$folderId_parts = explode('/', $repository->rootFolderId);
$path = '/' . end($folderId_parts);
}
$cmis_object = cmisapi_getProperties($repository->repositoryId, drupal_urlencode($path));
if (false === $cmis_object) {
drupal_set_message(t('Failed to locate the target space with path ' . $path));
$form_state['redirect'] = 'cmis/create';
}
else {
if ($cmis_object->type == 'folder') {
$objectId = cmisapi_createDocument($repository->repositoryId, 'document', array(
'content-type' => 'text/html',
'title' => $name,
), $cmis_object->id, $content);
if ($objectId) {
drupal_set_message('CMIS content ' . $name . ' has been created.');
$form_state['redirect'] = 'cmis/browser' . $path;
}
else {
drupal_set_message('Unable to create ' . $name . ' content.', 'error');
$form_state['redirect'] = 'cmis/create';
return false;
}
}
else {
drupal_set_message(t('Error when locating the target space ' . $path, 'error'));
$form_state['redirect'] = 'cmis/create';
}
}
}
function cmis_content_get() {
module_load_include('api.inc', 'cmis');
$objectId = urldecode($_GET['id']);
$repository = cmisapi_getRepositoryInfo();
$object = cmisapi_getProperties($repository->repositoryId, $objectId);
$content = cmisapi_getContentStream($repository->repositoryId, $objectId);
if (ob_get_level()) {
ob_end_clean();
}
drupal_set_header('Cache-Control: no-cache, must-revalidate');
drupal_set_header('Content-type: ' . $object->contentMimeType);
drupal_set_header('Content-Disposition: attachment; filename="' . $object->title . '"');
print $content;
exit;
}
function cmis_content_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('CMIS Repository Document View'),
);
return $blocks;
case 'configure':
$form = array(
'#cache' => TRUE,
);
if ($delta == 0) {
$form['cmis_target_document_items'] = array(
'#tree' => TRUE,
'#prefix' => '<div class="clear-block" id="cmis-content-items-wrapper">',
'#suffix' => '</div>',
);
$cmis_block_items = variable_get('cmis_target_document_items', array_fill(0, 2, '/'));
$cmis_block_items += count($cmis_block_items) == 0 ? array(
'/',
) : array();
foreach ($cmis_block_items as $key => $item) {
$form['cmis_target_document_items'][] = _cmis_content_block_settings_path_form($key, $item);
}
$form['cmis_target_document_items_more'] = array(
'#type' => 'submit',
'#value' => t('Add more'),
'#description' => t("If the amount of boxes above isn't enough, click here to add more."),
'#weight' => 1,
'#ahah' => array(
'path' => 'cmis/block_settings_more_items_js',
'wrapper' => 'cmis-content-items-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
}
return $form;
case 'save':
if ($delta == 0) {
$cmis_block_items = $edit['cmis_target_document_items'];
foreach ($cmis_block_items as $key => $cmis_block_item) {
if (empty($cmis_block_item) || $cmis_block_item == '/') {
unset($cmis_block_items[$key]);
}
}
variable_set('cmis_target_document_items', array_values($cmis_block_items));
}
return;
case 'view':
default:
switch ($delta) {
case 0:
$block['subject'] = t('Documents');
$block['content'] = cmis_content_contents(1);
break;
}
return $block;
}
}
function _cmis_content_block_settings_path_form($delta, $value = '/') {
return array(
'#type' => 'textfield',
'#title' => t('Target document path @n', array(
'@n' => $delta + 1,
)),
'#default_value' => $value,
'#description' => t('The document path relative to the root of the repository.'),
'#autocomplete_path' => 'cmis/autocomplete',
'#size' => 60,
'#parents' => array(
'cmis_target_document_items',
$delta,
),
);
}
function cmis_block_settings_more_items_js() {
$delta = count($_POST['cmis_target_document_items']);
$form_element = _cmis_content_block_settings_path_form($delta);
drupal_alter('form', $form_element, array(), 'cmis_block_settings_more_items_js');
$form_state = array(
'submitted' => FALSE,
);
$form_build_id = $_POST['form_build_id'];
if (!($form = form_get_cache($form_build_id, $form_state))) {
exit;
}
$form['block_settings']['cmis_target_document_items'][] = $form_element;
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
$form = form_builder('block-admin-configure', $form, $form_state);
$items_form = $form['block_settings']['cmis_target_document_items'];
unset($items_form['#prefix'], $items_form['#suffix']);
$output = theme('status_messages') . drupal_render($items_form);
drupal_json(array(
'status' => TRUE,
'data' => $output,
));
}
function cmis_content_generate_doc_view($target_path) {
module_load_include('api.inc', 'cmis');
$folder_img = theme('image', drupal_get_path('module', 'cmis_browser') . '/images/space.gif');
$file_img = theme('image', drupal_get_path('module', 'cmis_browser') . '/images/file.png');
$repository = cmisapi_getRepositoryInfo();
$cmis_object = cmisapi_getProperties($repository->repositoryId, drupal_urlencode($target_path));
$updatedStr = date_format($cmis_object->updated, 'n/j/Y g:i A');
$contents = '';
if ($cmis_object->type == 'folder') {
$folderlink = l($cmis_object->title, 'cmis/browser', array(
'query' => array(
'id' => $cmis_object->id,
),
));
$contents .= '<div>' . $folder_img . $folderlink . '</div>';
$contents .= '<div>' . $cmis_object->summary . '</div>';
$contents .= '<div> Modified:' . $updatedStr . '</div>';
$contents .= '<div> Modifier:' . $cmis_object->author . '</div>';
}
else {
$documentLink = l($cmis_object->title, 'cmis/get', array(
'query' => array(
'id' => $cmis_object->id,
),
));
$contents .= '<div>' . $file_img . $documentLink . '</div>';
$contents .= '<div>' . $cmis_object->summary . '</div>';
$contents .= '<div> Size:' . number_format($cmis_object->size / 1000, 2, '.', ',') . ' K</div>';
$contents .= '<div> Modified:' . $updatedStr . '</div>';
$contents .= '<div> Modifier:' . $cmis_object->author . '</div>';
}
return $contents;
}
function cmis_content_contents($which_block) {
switch ($which_block) {
case 1:
$content = array();
foreach (variable_get('cmis_target_document_items', array()) as $item) {
if ($item != '/') {
$content[] = cmis_content_generate_doc_view($item);
}
}
return implode('<br />', $content);
case 2:
return;
}
}