/libraries/piwi/Widget/Bin/Bin.php

https://github.com/jaws-project/jaws · PHP · 419 lines · 213 code · 38 blank · 168 comment · 36 complexity · 9e2bcfe4402008b72200b5a8f3ccf2f4 MD5 · raw file

  1. <?php
  2. /**
  3. * Bin.php - Bin Class for all bin widgets
  4. *
  5. * @version $Id $
  6. * @author Pablo Fischer <pablo@pablo.com.mx>
  7. *
  8. * <c> Pablo Fischer 2004
  9. * <c> Piwi
  10. */
  11. require_once PIWI_PATH . '/Widget/Widget.php';
  12. require_once PIWI_PATH . '/JS/JSEnums.php';
  13. class Bin extends Widget
  14. {
  15. /**
  16. *
  17. * Comment the widget (usefull if you want to use it in a form)
  18. *
  19. * @var string $_comment
  20. * @access private
  21. * @see setComment(), getComment()
  22. */
  23. var $_comment;
  24. /**
  25. *
  26. * AccessKey for the widget
  27. *
  28. * @var string $_accessKey
  29. * @access private
  30. * @see setAccessKey(), getAccessKey()
  31. */
  32. var $_accessKey;
  33. /**
  34. *
  35. * Data attribute for the widget
  36. *
  37. * @var array $_data
  38. * @access private
  39. * @see setData()
  40. */
  41. var $_data = array();
  42. /**
  43. * JS events
  44. *
  45. * @var array $_rvents;
  46. * @access private
  47. * @see addEvent()
  48. */
  49. var $_events;
  50. /**
  51. * Tells if the bin widget is enabled or not
  52. *
  53. * @var boolean $_isEnabled
  54. * @access private
  55. * @see setEnabled()
  56. */
  57. var $_isEnabled;
  58. /**
  59. * Is a 'list' that will have the avaiable gadgets
  60. * of a specific widget
  61. *
  62. * @var array $_availableEvents;
  63. * @access private
  64. * @see addEvent()
  65. */
  66. var $_availableEvents;
  67. /**
  68. * Add two colons to the end of the title.
  69. *
  70. * For example: Your name: <Name Entry>
  71. *
  72. * @var boolean $_twoColons
  73. * @access private
  74. * @see enableTwoColons(), requiresTwoColons()
  75. */
  76. var $_twoColons;
  77. /**
  78. * Is a list where all validators should go
  79. *
  80. * @var array $_validators
  81. * @access private
  82. * @see addValidator(), getValidators()
  83. */
  84. var $_validators;
  85. /**
  86. * Initializes the binary
  87. *
  88. * @access private
  89. */
  90. function init()
  91. {
  92. $this->_familyWidget = 'bin';
  93. $this->_events = array();
  94. $this->_validators = array();
  95. $this->setEnabled(true);
  96. $this->enableTwoColons(true);
  97. parent::init();
  98. if (!empty($this->_title)) {
  99. $this->setTitle($this->_title);
  100. } else {
  101. $this->setTitle('');
  102. }
  103. $this->setComment($this->_comment);
  104. }
  105. /**
  106. * Enable or disable the use of two-colons
  107. *
  108. * @access public
  109. * @param boolean $status True if two-colons should be added or false if not
  110. */
  111. function enableTwoColons($status = true)
  112. {
  113. $this->_twoColons = $status;
  114. }
  115. /**
  116. * Return the status of two-colons. To know if two colons should be added or not
  117. *
  118. * @access public
  119. * @return boolean True if two-colons should be added or false if not
  120. */
  121. function requiresTwoColons()
  122. {
  123. return $this->_twoColons;
  124. }
  125. /**
  126. * Get the comment of the widget
  127. *
  128. * @access public
  129. */
  130. function getComment()
  131. {
  132. return $this->_comment;
  133. }
  134. /**
  135. * Set the comment
  136. *
  137. * @access public
  138. * @param string Comment to use
  139. */
  140. function setComment($comment)
  141. {
  142. $this->_comment = $comment;
  143. }
  144. /**
  145. * Get the accesskey of the widget
  146. *
  147. * @access public
  148. */
  149. function getAccessKey()
  150. {
  151. return $this->_accessKey;
  152. }
  153. /**
  154. * Set the comment
  155. *
  156. * @access public
  157. * @param string Key to Use
  158. */
  159. function setAccessKey($accesskey)
  160. {
  161. $this->_accessKey = $accesskey;
  162. }
  163. /**
  164. * Set data attributes
  165. *
  166. * @access public
  167. * @param string name
  168. * @param string value
  169. */
  170. function setData($name, $value)
  171. {
  172. $this->_data[$name] = $value;
  173. }
  174. /**
  175. * Get the direction of the box
  176. * @return string Direction of the box
  177. */
  178. function getDirection()
  179. {
  180. return $this->_direction;
  181. }
  182. /**
  183. * Build the basic piwiXML data, adding bin params
  184. *
  185. * @access private
  186. */
  187. function buildBasicPiwiXML()
  188. {
  189. parent::buildBasicPiwiXML();
  190. if (!empty($this->_title)) {
  191. $this->_PiwiXML->addAttribute('title', $this->_Title);
  192. }
  193. }
  194. /**
  195. * Build the basix XHTML data - Adding bin params
  196. *
  197. * @access private
  198. * @return string XHTML data
  199. */
  200. function buildBasicXHTML()
  201. {
  202. $xhtml = '';
  203. if (!empty($this->_accessKey)) {
  204. $xhtml.= " accesskey=\"".$this->_accessKey."\"";
  205. }
  206. $xhtml.= parent::buildBasicXHTML();
  207. foreach ($this->_data as $name => $value) {
  208. $xhtml.= " data-{$name}=\"{$value}\"";
  209. }
  210. return $xhtml;
  211. }
  212. /**
  213. * Build the Events in a piwiXML style
  214. *
  215. * @access private
  216. * @return string Events in piwiXML style
  217. */
  218. function buildXMLEvents()
  219. {
  220. if (is_array($this->_events) && count($this->_events) > 0) {
  221. $this->_PiwiXML->openElement('events');
  222. foreach ($this->_events as $event) {
  223. $code = $event->getCode();
  224. if (substr($code, -1) != ';') {
  225. $code = $code . ';';
  226. }
  227. $this->_PiwiXML->openElement('event');
  228. $this->_PiwiXML->addAttribute('listener', $event->getID());
  229. if ($event->needsFile()) {
  230. $this->_PiwiXML->addAttribute('src', $event->getSrc());
  231. }
  232. $this->_PiwiXML->addText($code, true);
  233. $this->_PiwiXML->closeElement('event');
  234. }
  235. $this->_PiwiXML->closeElement('events');
  236. }
  237. }
  238. /**
  239. * Build the JS events XHTML syntax.
  240. *
  241. * @access private
  242. * @return string the XHTML syntax of the JS Events
  243. */
  244. function buildJSEvents()
  245. {
  246. $events = array();
  247. $xhtml = '';
  248. if (is_array($this->_events)) {
  249. foreach ($this->_events as $event) {
  250. $code = $event->getCode();
  251. if (substr($code, -1) != ';') {
  252. $code = $code . ';';
  253. }
  254. $id = $event->getID();
  255. if (in_array($id, array_keys($events))) {
  256. $code = str_replace('javascript:', '', $code);
  257. $events[$id].= ' '.$code;
  258. } else {
  259. if (isset($events[$id])) {
  260. $events[$id].= $code;
  261. } else {
  262. $events[$id] = $code;
  263. }
  264. }
  265. if ($event->needsFile()) {
  266. $this->addFile($event->getSrc());
  267. }
  268. }
  269. //Let's make the XHTML..
  270. foreach ($events as $jsid => $code) {
  271. $xhtml.= " ".$jsid."=\"".$code."\"";
  272. }
  273. }
  274. return $xhtml;
  275. }
  276. /**
  277. * Rebuild the JS of the widget
  278. *
  279. * @access public
  280. */
  281. function rebuildJS()
  282. {
  283. $this->buildJSEvents();
  284. }
  285. /**
  286. * Set the enabled status
  287. *
  288. * @param boolean the status value
  289. * @access public
  290. */
  291. function setEnabled($status = true)
  292. {
  293. $this->_isEnabled = $status;
  294. }
  295. /**
  296. * Add a new Event
  297. *
  298. * @param object The event to add
  299. * @access public
  300. */
  301. function addEvent($event)
  302. {
  303. if (is_string($event) && func_num_args() == 2) {
  304. $action = func_get_arg(1);
  305. if (is_array($this->_availableEvents) && count($this->_availableEvents) > 0) {
  306. if (in_array($event, $this->_availableEvents)) {
  307. $this->_events[] = new JSEvent($event, $action);
  308. } else {
  309. die("[PIWI] - Sorry but you are not permitted to use ".$event." in this widget");
  310. }
  311. } else {
  312. $this->_events[] = new JSEvent($event, $action);
  313. }
  314. } elseif (is_object($event) && strtolower(get_class($event)) == 'jsevent') {
  315. if (is_array($this->_availableEvents) && count($this->_availableEvents) > 0) {
  316. $id = $event->getID();
  317. if (in_array($id, $this->_availableEvents)) {
  318. $this->_events[] =& $event;
  319. } else {
  320. die("[PIWI] - Sorry but you are not permitted to use ".$id." in this widget");
  321. }
  322. } else {
  323. $this->_events[] =& $event;
  324. }
  325. } else {
  326. die("[PIWI] - Events should be objects");
  327. }
  328. }
  329. /**
  330. * Add a new validator
  331. *
  332. * @param JSValidator $validator Validator object
  333. * @access public
  334. */
  335. function addValidator($validator)
  336. {
  337. if (is_string($validator)) {
  338. $file = PIWI_PATH . '/JS/' . $validator . '.php';
  339. if (file_exists($file)) {
  340. require_once $file;
  341. } else {
  342. die("[PIWI] - Piwi validator doesn't exists");
  343. }
  344. $parameter = "";
  345. $numargs = func_num_args();
  346. if ($numargs > 1) {
  347. $arg_list = func_get_args();
  348. $arg_count = func_num_args();
  349. for ($i = 1; $i < $arg_count; $i++) {
  350. if (defined($arg_list[$i])) {
  351. $arg_list[$i] = constant($arg_list[$i]);
  352. }
  353. $parameter .= "\$arg_list[$i]";
  354. if ($i != ($arg_count-1)) {
  355. $parameter .= ',';
  356. }
  357. }
  358. }
  359. $validatorObj = null;
  360. eval("\$validatorObj = new \$validator($parameter);");
  361. $this->_validators[] = $validatorObj;
  362. } elseif (is_object($validator) && strtolower(get_parent_class($validator)) == 'jsvalidator') {
  363. $this->_validators[] = $validator;
  364. } else {
  365. die("[PIWI] - Validators should be objects and must inherit from JSValidator class");
  366. }
  367. }
  368. /**
  369. * Returns the list of validators
  370. *
  371. * @return array List of validators
  372. * @access public
  373. */
  374. function getValidators()
  375. {
  376. return $this->_validators;
  377. }
  378. }
  379. ?>