PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/dynamic-widgets/classes/dwmodule_class.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 330 lines | 189 code | 49 blank | 92 comment | 45 complexity | 1dcca313f081e1e48d457dc4b5b20dbf MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * DWModule class
  4. *
  5. * @version $Id$
  6. * @copyright 2011 Jacco Drabbe
  7. */
  8. abstract class DWModule {
  9. protected static $classname;
  10. protected static $info = FALSE;
  11. protected static $name;
  12. public static $option;
  13. protected static $opt;
  14. protected static $overrule = FALSE;
  15. public static $plugin = FALSE;
  16. protected static $type = 'simple';
  17. protected static $wpml = FALSE;
  18. public function __construct() {
  19. self::$classname = get_class($this);
  20. }
  21. /**
  22. * DWModule::admin() Basic admin init
  23. *
  24. */
  25. public static function admin() {
  26. $DW = &$GLOBALS['DW'];
  27. // $classname = self::getClassName();
  28. $vars = self::getVars(self::$classname);
  29. self::setName(self::$classname);
  30. // Would be so much easier if we could require PHP > 5.3: $name::
  31. self::checkOverrule();
  32. if ( $vars['plugin'] !== FALSE ) {
  33. self::registerPlugin($vars['plugin']);
  34. }
  35. if ( $vars['type'] == 'simple' ) {
  36. self::mkGUI($vars['type'], $vars['option'][self::$name], $vars['question'], $vars['info']);
  37. }
  38. }
  39. /**
  40. * DWModule::checkOverrule() Registers an overrule module to $DW
  41. *
  42. */
  43. public static function checkOverrule($classname = NULL) {
  44. $DW = &$GLOBALS['DW'];
  45. if (! is_null($classname) ) {
  46. self::$classname = $classname;
  47. }
  48. // $classname = self::getClassName();
  49. $vars = self::getVars(self::$classname);
  50. self::setName(self::$classname);
  51. if ( isset($vars['overrule']) && $vars['overrule'] && ! in_array(self::$name, $DW->overrule_maintype) ) {
  52. $DW->overrule_maintype[ ] = self::$name;
  53. }
  54. }
  55. /**
  56. * DWModule::getClassName() Gets the called class
  57. *
  58. * @return string
  59. */
  60. protected static function getClassName() {
  61. // $classname = get_called_class();
  62. $classname = get_class($this);
  63. return $classname;
  64. }
  65. /**
  66. * DWModule::getVars() Gets the properties from the class
  67. *
  68. * @param string $classname
  69. * @return array
  70. */
  71. protected static function getVars($classname) {
  72. $vars = get_class_vars($classname);
  73. return $vars;
  74. }
  75. /**
  76. * DWModule::GUIComplex() GUI output of the complex list
  77. *
  78. * @param string $except Except string
  79. * @param array $list List of options
  80. * @param string $extra Extra option for the checkboxes
  81. * @param string $name Name of the DWOpt type
  82. */
  83. public static function GUIComplex($except, $list, $extra = NULL, $name = NULL) {
  84. if (! is_null($name) ) {
  85. self::$name = $name;
  86. }
  87. if ( count($list) > DW_LIST_LIMIT ) {
  88. $select_style = DW_LIST_STYLE;
  89. }
  90. if ( count($list) > 0 ) {
  91. echo '<br />' . "\n";
  92. _e($except, DW_L10N_DOMAIN);
  93. echo '<br />';
  94. echo '<div id="' . self::$name . '-select" class="condition-select" ' . ( (isset($select_style)) ? $select_style : '' ) . ' />';
  95. foreach ( $list as $key => $value ) {
  96. echo '<input type="checkbox" id="' . self::$name . '_act_' . $key . '" name="' . self::$name . '_act[]" value="' . $key . '" ' . ( (self::$opt->count > 0 && in_array($key, self::$opt->act)) ? 'checked="checked"' : '' ) . $extra . ' /> <label for="' . self::$name . '_act_' . $key . '">' . $value . '</label><br />' . "\n";
  97. }
  98. echo '</div>' . "\n";
  99. }
  100. }
  101. /**
  102. * DWModule::GUIFooter() GUI output of the footer module div
  103. *
  104. */
  105. public static function GUIFooter() {
  106. echo '</div><!-- end dynwid_conf -->' . "\n";
  107. }
  108. /**
  109. * DWModule::GUIHeader() GUI output of the header module div
  110. *
  111. * @param string $title Title of the module
  112. * @param string $question Main question
  113. * @param string $info Extra info
  114. * @param string $post_title Extra title info
  115. * @param object $opt DWOpt object
  116. */
  117. public static function GUIHeader($title, $question, $info, $post_title = NULL, $opt = NULL, $name = NULL) {
  118. $DW = &$GLOBALS['DW'];
  119. // $classname = self::getClassName();
  120. $vars = self::getVars(self::$classname);
  121. $wpml = FALSE;
  122. if ( $vars['wpml'] !== FALSE ) {
  123. $wpml = TRUE;
  124. }
  125. if (! is_null($post_title) ) {
  126. $title = __($title, DW_L10N_DOMAIN);
  127. $title .= ' ' . $post_title;
  128. }
  129. if (! is_null($opt) ) {
  130. self::$opt = $opt;
  131. }
  132. if (! is_null($name) ) {
  133. self::$name = $name;
  134. }
  135. echo '<!-- ' . $title . ' //-->' . "\n";
  136. echo '<h3 id="' . self::$name . '" title=" Click to toggle " class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all"><b>' . __($title, DW_L10N_DOMAIN) . '</b>' . ( (self::$opt->count > 0) ? ' <img src="' . $DW->plugin_url . 'img/checkmark.gif" alt="Checkmark" />' : '' ) . ' ' . ( ($DW->wpml && $wpml) ? DW_WPML::$icon : '' ) . '</h4>' . "\n";
  137. echo '<div id="' . self::$name . '_conf" class="dynwid_conf ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom">' . "\n";
  138. _e($question, DW_L10N_DOMAIN);
  139. if ( $info !== FALSE ) {
  140. echo ' <img src="' . $DW->plugin_url . 'img/info.gif" alt="info" title="' . __('Click to toggle info', DW_L10N_DOMAIN) . '" onclick="divToggle(\'' . self::$name . '_info\')" /><br />' . "\n";
  141. echo '<div><div id="' . self::$name . '_info" class="infotext">' . "\n";
  142. _e($info, DW_L10N_DOMAIN);
  143. echo '</div></div>' . "\n";
  144. } else {
  145. echo '<br />' . "\n";
  146. }
  147. }
  148. /**
  149. * DWModule::GUIOption() GUI output of the radio buttons
  150. *
  151. * @param string $name Name of the module
  152. * @param object $opt DWOpt object
  153. */
  154. public static function GUIOption($name = NULL, $opt = NULL) {
  155. $DW = &$GLOBALS['DW'];
  156. if (! is_null($name) ) {
  157. self::$name = $name;
  158. }
  159. if (! is_null($opt) ) {
  160. self::$opt = $opt;
  161. }
  162. $DW->dumpOpt(self::$opt);
  163. echo '<input type="radio" name="' . self::$name . '" value="yes" id="' . self::$name . '-yes" ' . ( (self::$opt->selectYes()) ? self::$opt->checked : '' ) . ' /> <label for="' . self::$name . '-yes">' . __('Yes') . '</label>' . "\n";
  164. echo '<input type="radio" name="' . self::$name . '" value="no" id="' . self::$name . '-no" ' . ( (self::$opt->selectNo()) ? self::$opt->checked : '' ) . ' /> <label for="' . self::$name . '-no">' . __('No') . '</label>' . "\n";
  165. }
  166. /**
  167. * DWModule::mkGUI() Full GUI output
  168. *
  169. * @param string $type Type of output
  170. * @param string $title Title of module
  171. * @param string $question Main question
  172. * @param string $info Extra info
  173. * @param string $except Except string
  174. * @param array $list List of options
  175. * @param string $name Name of module
  176. */
  177. public static function mkGUI($type, $title, $question, $info, $except = FALSE, $list = FALSE, $name = NULL) {
  178. $DW = &$GLOBALS['DW'];
  179. $widget_id = $GLOBALS['widget_id'];
  180. if (! is_null($name) ) {
  181. self::$name = $name;
  182. }
  183. self::$opt = $DW->getDWOpt($widget_id, self::$name);
  184. self::GUIHeader($title, $question, $info);
  185. self::GUIOption();
  186. if ( $type == 'complex' ) {
  187. self::GUIComplex($except, $list);
  188. }
  189. self::GUIFooter();
  190. }
  191. /**
  192. * DWModule::registerOption() Register module to $DW
  193. *
  194. * @param array $dwoption Name and title of module
  195. */
  196. public static function registerOption($dwoption) {
  197. $DW = &$GLOBALS['DW'];
  198. // For some reason when a widget is just added to the sidebar $dwoption is not an array
  199. if ( is_array($dwoption) ) {
  200. foreach ( $dwoption as $key => $value ) {
  201. $DW->dwoptions[$key] = __($value, DW_L10N_DOMAIN);
  202. }
  203. }
  204. }
  205. /**
  206. * DWModule::registerPlugin() Regsiter plugin to $DW
  207. *
  208. * @param array $plugin Name and default value statuc of plugin
  209. */
  210. public static function registerPlugin($plugin) {
  211. $DW = &$GLOBALS['DW'];
  212. foreach ( $plugin as $key => $value ) {
  213. if (! isset($DW->$key) ) {
  214. $DW->$key = $value;
  215. }
  216. }
  217. }
  218. /**
  219. * DWModule::save() Basic save of Module options to the database via $DW
  220. *
  221. * @param string $name Name of module
  222. * @param string $type Type of module
  223. */
  224. public static function save($name, $type = 'simple') {
  225. $DW = &$GLOBALS['DW'];
  226. $widget_id = $GLOBALS['widget_id'];
  227. switch ( $type ) {
  228. case 'complex':
  229. $act = $name . '_act';
  230. if ( isset($_POST[$act]) && count($_POST[$act]) > 0 ) {
  231. $DW->addMultiOption($widget_id, $name, $_POST[$name], $_POST[$act]);
  232. } else if ( isset($_POST[$name]) && $_POST[$name] == 'no' ) {
  233. $DW->addSingleOption($widget_id, $name);
  234. }
  235. break;
  236. // simple
  237. default:
  238. if ( isset($_POST[$name]) && $_POST[$name] == 'no' ) {
  239. $DW->addSingleOption($widget_id, $name);
  240. }
  241. } // switch
  242. }
  243. public static function childSave($name) {
  244. $DW = &$GLOBALS['DW'];
  245. $widget_id = $GLOBALS['widget_id'];
  246. $act = $name . '_act';
  247. $child_act = $name . '_childs_act';
  248. $dwtype = $name . '-childs';
  249. // Workaround for lazy taxonomy tree
  250. if ( isset($_POST[$act]) && isset($_POST[$child_act]) ) {
  251. if (! is_array($_POST[$act]) ) {
  252. if ( substr($_POST[$act], 0, 1) == ',' ) {
  253. $_POST[$act] = substr($_POST[$act], 1);
  254. }
  255. $t = explode(',', $_POST[$act]);
  256. $t = array_unique($t);
  257. if ( substr($_POST[$child_act], 0, 1) == ',' ) {
  258. $_POST[$child_act] = substr($_POST[$child_act], 1);
  259. }
  260. $t_childs = explode(',', $_POST[$child_act]);
  261. $t_childs = array_unique($t_childs);
  262. } else {
  263. $t = $_POST[$act];
  264. $t_childs = $_POST[$child_act];
  265. }
  266. if ( count($t) > 0 && count($t_childs) > 0 ) {
  267. $DW->addChilds($widget_id, $dwtype, $_POST[$name], $t, $t_childs);
  268. }
  269. }
  270. }
  271. /**
  272. * DWModule::setName() Auto registering name to DWModule class
  273. *
  274. * @param string $classname Full classname
  275. */
  276. protected static function setName($classname) {
  277. self::$name = strtolower(substr($classname, 3)); // Chop off the "DW_"
  278. self::$name = str_replace('_', '-', self::$name);
  279. }
  280. }
  281. ?>