jqplug.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // $Id$
  3. //
  4. // jqplug.module
  5. //
  6. //
  7. //
  8. // Created by Bach on 2008-04.
  9. // Modified by bach on 2009-09-09
  10. // Copyright 2009 gui. All rights reserved.
  11. //
  12. /**
  13. * @file
  14. * Loads plugins for jQuery
  15. *
  16. *
  17. *
  18. */
  19. define('JQPLUG_PERM_ADMIN', 'administer jqplug');
  20. /**
  21. *
  22. * hook_permission()
  23. *
  24. */
  25. function jqplug_permission(){
  26. return array('administer jqplug' => array(
  27. 'title' => 'administer jqplug',
  28. 'description' => 'administer what jquery plugin will be loaded',
  29. ),
  30. );
  31. }
  32. /**
  33. * hook_menu()
  34. *
  35. */
  36. function jqplug_menu() {
  37. $items['admin/config/user-interface/jgplug'] = array(
  38. 'title' => 'jq plug',
  39. 'description' => 'Control which plugin are loaded by theme using jqplug.',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('jqplug_admin_settings'),
  42. 'access arguments' => array(JQPLUG_PERM_ADMIN),
  43. 'type' => MENU_NORMAL_ITEM
  44. );
  45. return $items;
  46. }
  47. function jqplug_admin_settings() {
  48. $form = array();
  49. $form['jqplug'] = array(
  50. '#type' => 'fieldset',
  51. '#title' => t('jqplug settings'),
  52. '#description' => t('Control which plugin are loaded by theme using jqplug.')
  53. );
  54. $default = array();
  55. $options = array();
  56. $list = _get_plugList();
  57. ## DEFINE OPTIONS
  58. foreach($list as $plug){
  59. $options[$plug['file']] = t($plug['name']);
  60. }
  61. # config by theme, does'nt work yet 'cause of hook_init which does'nt have acces to global $theme
  62. /*
  63. $themes = list_themes();
  64. foreach($themes as $theme){
  65. if( $theme->status == 1){
  66. $form['jqplug']['jqplug_theme_' . $theme->name] = array(
  67. '#type' => 'checkboxes',
  68. '#title' => t($theme->name),
  69. '#default_value' => variable_get('jqplug_theme_' . $theme->name, $default),
  70. '#options' => $options,
  71. '#description' =>t('<b>' . $theme->name . ' :</b> selecte which plugins will be load for this theme.'),
  72. );
  73. }
  74. }
  75. */
  76. #same config for alla themes
  77. $form['jqplug']['jqplug'] = array(
  78. '#type' => 'checkboxes',
  79. '#title' => t('jQuery Plugins'),
  80. '#default_value' => variable_get('jqplug', $default),
  81. '#options' => $options,
  82. '#description' =>t('selecte which plugins will be load for all theme.'),
  83. );
  84. return system_settings_form($form);
  85. }
  86. // function jqplug_add($adds = array()){
  87. //
  88. // $activated = variable_get('jqplug', array());
  89. // $list = _get_plugList();
  90. //
  91. // $match = true;
  92. // foreach ($adds as $add) {
  93. // foreach($list as $plug){
  94. // if($plug['name'] == $add){
  95. // variable_set();
  96. // }
  97. // }
  98. // }
  99. //
  100. //
  101. //
  102. // return $match;
  103. //
  104. // }
  105. function _get_plugList(){
  106. $list = array();
  107. $mod_path = drupal_get_path('module', 'jqplug');
  108. $pluginsFolderPath = $mod_path.'/js/*';
  109. $strToRemove = array("jquery.", ".pack", "jQuery", ".js", ".compressed", ".minified", ".min");
  110. $strToSpace = array(".", "-");
  111. foreach(glob($pluginsFolderPath) as $file){
  112. $pathParts = explode("/", $file);
  113. $file = $pathParts[count($pathParts)-1];
  114. $fileParts = explode(".", $file);
  115. $extension = $fileParts[count($fileParts)-1];
  116. if($extension == "js"){
  117. $PlugName = str_replace($strToRemove, '', $file);
  118. $PlugName = str_replace($strToSpace, ' ', $PlugName);
  119. array_push($list, array("file"=>$file,"name"=>$PlugName));
  120. }
  121. }
  122. return $list;
  123. }
  124. /**
  125. * Implementation of hook_init().
  126. */
  127. function jqplug_init() {
  128. # Add the JS for this module.
  129. $mod_path = drupal_get_path('module', 'jqplug');
  130. // dsm($mod_path);
  131. # config by theme, does'nt work yet 'cause of hook_init which does'nt have acces to global $theme
  132. // global $theme;
  133. // $theme_plugs = variable_get('jqplug_theme_'.$theme, array());
  134. // foreach($theme_plugs as $plug){
  135. // if( $plug != 0 ){
  136. // drupal_add_js($mod_path.'/js/'.$plug, 'module', 'header', FALSE, TRUE);
  137. // }
  138. // }
  139. #same config for alla themes
  140. $plugs = variable_get('jqplug', array());
  141. // dsm($plugs);
  142. foreach($plugs as $plug){
  143. if( $plug != "0" ){
  144. // dsm($mod_path.'/js/'.$plug);
  145. if(file_exists($mod_path.'/js/'.$plug))
  146. drupal_add_js($mod_path.'/js/'.$plug, array('scope'=>'header', 'group'=>JS_LIBRARY));
  147. $cssFile = preg_replace("/\.js$/", '.css', $plug);
  148. $cssFile = str_replace(".min", '', $cssFile);
  149. if(file_exists($mod_path.'/js/'.$cssFile))
  150. drupal_add_css($mod_path.'/js/'.$cssFile, array('media'=>'screen', 'group'=>CSS_DEFAULT));
  151. }
  152. }
  153. }