Skip to content

Commit

Permalink
Merge branch 'master_copy' of git@github.com:shakty/Patterns.git
Browse files Browse the repository at this point in the history
  • Loading branch information
yanbo committed Aug 18, 2011
2 parents 583c717 + 52c3ba2 commit a4ce0c1
Show file tree
Hide file tree
Showing 17 changed files with 1,524 additions and 1,465 deletions.
165 changes: 154 additions & 11 deletions components/system.inc
@@ -1,41 +1,40 @@
<?php
/*
* TODO: module enabling/disabling is done using a workaround because
* TODO: Module enabling/disabling is done using a workaround because
* the form submitting way does not work for some reason
* TODO: Only set a theme default if 'default' is 1.
* TODO: Handle administration themes separately.
* @file
* Patterns component for system related operations.
*/
function system_patterns () {

$files = array('modules/system/system.admin.inc');
// TODO: Not every action might need this file.

$actions['modules'] = array('descr' => t('Enable/Disable Modules'),
PATTERNS_CREATE => array('system_modules'),
PATTERNS_DELETE => array('system_modules'),
PATTERNS_CREATE => array(),
PATTERNS_DELETE => array(),
'files' => $files,
);

$actions['themes'] = array('descr' => t('Enable (and set default)/Disable Themes'),
$actions['theme'] = array('descr' => t('Enable (and set default)/Disable Themes'),
PATTERNS_CREATE => array('set_active_theme'),
PATTERNS_DELETE => array('set_active_theme'),
'files' => $files,
);

$actions['form'] = array('descr' => t('Submit Custom Forms'),
PATTERNS_CREATE => array('TODO'), // TODO: Cannot specify forms at this time.
'files' => $files,
PATTERNS_CREATE => array(), // TODO: Cannot specify forms at this time.
);

$actions['call_php_func'] = array('descr' => t('Call PHP Functions'),
PATTERNS_CREATE => array('call_php_func_form'),
'files' => $files,
);

$actions['variables'] = array('descr' => t('Set/Modify/Delete System Variables'),
PATTERNS_CREATE => array('variables'),
PATTERNS_MODIFY => array('variables'),
PATTERNS_DELETE => array('variables'),
'files' => $files,
);

$actions['variable'] = $actions['variables']; // Alias
Expand All @@ -62,6 +61,33 @@ function system_patterns_prepare($action, $tag, &$data = NULL) {
$data['theme_default'] = $data['value'];
}
}
elseif ($tag == 'modules') {
// Make a <modules>modulename</modules> style tag work
if (is_string($data) || (isset($data['value']) && !isset($data[0]))) {
$data = array($data);
}

// Ensure proper data format for cases when <modules> tag contains
// only single <module> tag.
if (!empty($data['module']) && is_string($data['module'])) {
$data[0]['value'] = $data['module'];
unset($data['module']);
}

foreach ($data as &$item) {
// Ensure that modules with tags like <module>modulename</module>
// are represented as an array instead of a string
if (is_string($item)) {
$item = array('value' => $item);
}
}

// proccess alias for delete
if (isset($data['disable']) && !isset($data['delete'])) {
$data['delete'] = $data['disable'];
}
unset($data['disable']);
}
// TODO: other tags

return patterns_results();
Expand All @@ -80,20 +106,131 @@ function system_patterns_validate ($action, $tag, &$data = NULL) {
$msg = t('%theme is not a valid theme.', array('%theme' => $data['theme_default']));
}
}
elseif ($tag == 'modules') {
if (!isset($modules_info) || !is_array($modules_info)) {
$modules_info = system_rebuild_module_data(); // list of available modules
}

$modules = module_list(); // list of enabled modules
$delete = $action === PATTERNS_DELETE;
for ($i = 0; isset($data[$i]) && ($item = $data[$i]); $i++) {
$module = $item['value'];

// Ensure a module can be disabled safely
if ($delete) { // TODO: Move this out of the loop?
if (array_key_exists($module, $modules_info)) { // Module is available
if (array_key_exists($module, $modules)) { // Module is enabled
/*
* TODO: no more dependents field in Drupal7.x
foreach ((array)$modules_info[$module]->info['dependents'] as $dependent) { // Dependency check
if (array_key_exists($dependent, $modules)) {
$remove[] = $i;
drupal_set_message(t('Warning: Could not disable %module because other modules depend on it.', array('%module' => $module)), "warning");
break;
}
}
*/
}
else {
$remove[] = $i;
$status = PATTERNS_WARN;
$msg .= t('Warning: Did not disable %module because it is already disabled.', array('%module' => $module)) . '<br/>';
}
}
else {
$remove[] = $i;
$status = PATTERNS_WARN;
$msg .= t('Warning: Could not disable %module because it is missing.', array('%module' => $module)) . '<br/>';
}
}
// Ensure a module and all of its dependencies exist
else {
if (!array_key_exists($module, $modules)) { // Module not yet enabled
if (!array_key_exists($module, $modules_info)) { // Module does not exist
$required[] = $module;
}
else {
foreach ((array)$modules_info[$module]->info['dependencies'] as $dependency) {
if (!array_key_exists($dependency, $modules) && !array_key_exists($dependency, $modules_info)) {
$required[] = $dependency;
}
}
}

if (!empty($required)) {
$status = PATTERNS_ERR;
$msg .= t('%module can not be installed because the module or its dependencies are missing. Please download them and try again.', array('%module' => $module)) .
t('!title%dependencies', array('!title' => '<br/><b>' . t('Missing module(s): ') . '</b>', '%dependencies' => implode(', ', $required)));
}
}
else {
$status = PATTERNS_WARN;
$msg .= t('Warning: Did not enable %module because it is already enabled.', array('%module' => $module)) . '<br/>';
}

}
}
if (!empty($remove)) {
$result = array();
foreach ($data as $key => $item) {
if (!in_array($key, $remove)) $result[$key] = $item;
}
$data = $result;
}
}
// TODO: other tags

return patterns_results($status, $msg);
}

// Return which callback functions to actually use.
function system_patterns_callbacks ($action, $tag, &$data = NULL) {
if ($tag == 'modules') { // Custom functions will do the work
$result = array('modules_execute');
}
else { // Just regular forms
$desc = system_patterns();
$result = $desc[$tag][$action];
}
return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
}

// Prepare for valid processing of this type of component
function system_patterns_build ($action, $tag, &$data = NULL, $form_id) {
$status = PATTERNS_SUCCESS;
$msg = '';
$result = NULL;

if ($form_id == 'set_active_theme') {
$data['op'] = t('Save configuration');
//module_load_include('inc', 'system', 'system.admin');
// return $data;
}
elseif ($form_id == 'system_modules') {

$enabled = $disabled = array();
$delete = $action === PATTERNS_DELETE;
for ($i = 0; $module = $data[$i]; $i++) {
if ($delete) { // TODO: Move this out of loop?
module_disable(array($module['value']), TRUE);
$disabled[] = $module['value'];
}
else {
if (!module_enable(array($module['value']), TRUE)) {
// TODO: use proper logging?
drupal_set_message(t('Warning: Could not enable %module because a dependency is missing.', array('%module' => $module)), "warning");
}
else {
$enabled[] = $module['value'];
}
}
}

$msg = ((count($enabled) > 0) ? t('Module(s) %vars enabled.', array('%vars' => implode(', ', $enabled))) : t('No modules have been enabled.')) . ' ' .
((count($disabled) > 0) ? t('Module(s) %vars disabled.', array('%vars' => implode(', ', $disabled))) : t('No modules have been disabled.')) ;
}
// TODO: other forms
return patterns_results();
return patterns_results($status, $msg, $result);
}

// Build a patterns actions and parameters
Expand All @@ -108,10 +245,16 @@ function system_patterns_params($action, $form_id, &$data = NULL) {
// Cleanup any global settings after the action runs
function system_patterns_cleanup($action, $tag, &$data = NULL) {
if ($tag == 'modules') {
menu_rebuild();
menu_rebuild(); // TODO: only do this if there has been a module enabled/disabled
}
}

function modules_execute($action, &$data) {
dvm($data);
}

// ------------------------------- old

function system_patterns_old($op, $id = NULL, &$data = NULL) {
static $modules_info;

Expand Down
39 changes: 25 additions & 14 deletions components/taxonomy.inc
Expand Up @@ -6,8 +6,6 @@
// Return an error string only in case of error, otherwise modify $data

function taxonomy_patterns () {
// old way
//return array('vocabulary', 'term', 'terms');

$actions['vocabulary'] = array('descr' => t('Create/Modify/Delete Taxonomy Vocabularies'),
PATTERNS_CREATE => array('taxonomy_form_vocabulary'),
Expand Down Expand Up @@ -105,6 +103,15 @@ function taxonomy_patterns_validate ($action, $tag, &$data = NULL) {
return patterns_results($status, $msg);
}

// Return which callback functions to actually use.
function taxonomy_patterns_callbacks ($action, $tag, &$data = NULL) {
// Just return the form ids specified above. No custom functions used here.
$desc = system_patterns();
$result = $desc[$tag][$action];

return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
}

// Prepare for valid processing of this type of component
function taxonomy_patterns_build ($action, $tag, &$data = NULL, $form_id) {
module_load_include('inc', 'taxonomy', 'taxonomy.admin'); // TODO: Isn't this already loaded?
Expand All @@ -116,18 +123,18 @@ function taxonomy_patterns_params($action, $form_id, &$data = NULL) {

if ($form_id == 'taxonomy_form_vocabulary') {
if ($data['vid']) {
$result = (array)taxonomy_vocabulary_load($data['vid']);
$result = taxonomy_vocabulary_load($data['vid']);
}
}
else if ( $form_id == 'taxonomy_vocabulary_confirm_delete') {
if ($data['vid']) {
$result = array($data['vid']);
$result = $data['vid'];
}
else if (!isset($data['machine_name'])) {
$taxo = taxonomy_vocabulary_machine_name_load($data['machine_name']);
$vid = $taxo->vid;
if (!empty($vid)) {
(array)taxonomy_vocabulary_load($data['vid']);
taxonomy_vocabulary_load($data['vid']);
}
}
}
Expand All @@ -136,7 +143,7 @@ function taxonomy_patterns_params($action, $form_id, &$data = NULL) {
$result = array($data, $vocab);
}
else if ( $form_id == 'taxonomy_term_confirm_delete') {
$result = array($data['tid']);
$result = $data['tid'];
}

return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
Expand Down Expand Up @@ -243,9 +250,11 @@ function _taxonomy_patterns_prepare_vocabulary ($action, $data) {
// We are changing other stuff
else if (isset($data['machine_name'])){
$taxo = taxonomy_vocabulary_machine_name_load($data['machine_name']);
$vid = $taxo->vid;
// The old_machine_name must be set equal to machine_name
$data['old_machine_name'] = $data['machine_name'];
if (!empty($taxo)) {
$vid = $taxo->vid;
// The old_machine_name must be set equal to machine_name
$data['old_machine_name'] = $data['machine_name'];
}
}

if(!empty($vid)) {
Expand Down Expand Up @@ -279,12 +288,14 @@ function _taxonomy_patterns_prepare_vocabulary_delete ($action, $data) {
$taxo = taxonomy_vocabulary_machine_name_load($data['machine_name']);
}

$data['vid'] = $taxo->vid;
$data['name'] = $taxo->machine_name;
if (!empty($taxo)){
$data['vid'] = $taxo->vid;
$data['name'] = $taxo->machine_name;

$data['type'] = 'vocabulary';
$data['confirm'] = 1;
$data['op'] = 'Delete';
$data['type'] = 'vocabulary';
$data['confirm'] = 1;
$data['op'] = 'Delete';
}

return $data;
}
Expand Down
5 changes: 3 additions & 2 deletions doc/patterns.html
Expand Up @@ -505,7 +505,8 @@ <h2 id="_installation">2. Installation</h2>
</p>
</li>
</ul></div>
<div class="paragraph"><p>Both libraries should be installed under <strong><tt>sites/all/libraries/</tt></strong>.</p></div>
<div class="paragraph"><p>Both libraries should be installed under <strong><tt>sites/all/libraries/</tt></strong>
under the names of <strong><tt>spyc</tt></strong> and <strong><tt>codemirror2</tt></strong>.</p></div>
</div>
<h2 id="pattern-example">3. What is a Pattern?</h2>
<div class="sectionbody">
Expand Down Expand Up @@ -746,7 +747,7 @@ <h2 id="_list_of_all_currently_supported_tags">7. List of all currently supporte
<div id="footer">
<div id="footer-text">
Version 0.1<br />
Last updated 2011-08-16 10:11:40 CEST
Last updated 2011-08-17 15:59:26 CEST
</div>
</div>
</body>
Expand Down
17 changes: 11 additions & 6 deletions doc/patterns.txt
Expand Up @@ -42,7 +42,8 @@ uses of external libraries which must be installed separately:
is _not_ necessary, for the correct functioning of Patterns, but
recommended.

Both libraries should be installed under *+sites/all/libraries/+*.
Both libraries should be installed under *+sites/all/libraries/+*
under the names of *+spyc+* and *+codemirror2+*.



Expand All @@ -67,24 +68,28 @@ info: # mandatory name
description: Adds a new vocabulary to the website
author: QScience
category: Examples
version: 1.0
core: 7.x
author_email: sbalietti@ethz.ch
author_website: http://qlectives.eu

actions: # any name is good for this category

create:
tag: vocabulary
tag: vocabulary
name: Another Vocabulary
machine_name: anothervoc
description: Another interesting vocabulary
hierarchy: 0

modify:
tag: vocabulary
tag: vocabulary
machine_name: anothervoc
description: It was not that interesting after all
# vid: 2
description: It was not that interesting after all
# vid: 2

delete:
tag: vocabulary
tag: vocabulary
machine_name: anothervoc
# vid: 2

Expand Down

0 comments on commit a4ce0c1

Please sign in to comment.