/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Folder.php

https://github.com/ewandor/horde · PHP · 247 lines · 160 code · 12 blank · 75 comment · 9 complexity · 900f4bb59c42dd8b3cacbb608c989eaa MD5 · raw file

  1. <?php
  2. /**
  3. * The Horde_Kolab_Cli_Module_Folder:: class handles single folders.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Horde
  8. * @package Kolab_Cli
  9. * @author Gunnar Wrobel <wrobel@pardus.de>
  10. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11. * @link http://pear.horde.org/index.php?package=Kolab_Cli
  12. */
  13. /**
  14. * The Horde_Kolab_Cli_Module_Folder:: class handles single folders.
  15. *
  16. * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
  17. *
  18. * See the enclosed file COPYING for license information (LGPL). If you
  19. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  20. *
  21. * @category Horde
  22. * @package Kolab_Cli
  23. * @author Gunnar Wrobel <wrobel@pardus.de>
  24. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  25. * @link http://pear.horde.org/index.php?package=Kolab_Cli
  26. */
  27. class Horde_Kolab_Cli_Module_Folder
  28. implements Horde_Kolab_Cli_Module
  29. {
  30. /**
  31. * Get the usage description for this module.
  32. *
  33. * @return string The description.
  34. */
  35. public function getUsage()
  36. {
  37. return Horde_Kolab_Cli_Translation::t(" folder - Handle a single folder (the default action is \"show\")
  38. - show PATH : Display information about the folder at PATH.
  39. - create PATH [TYPE] : Create the folder PATH (with the optional type TYPE).
  40. - delete PATH : Delete the folder PATH.
  41. - rename OLD NEW : Rename the folder from OLD to NEW.
  42. - getacl PATH : Get all ACL on the specified folder.
  43. - getmyacl PATH : Get your ACL on the specified folder.
  44. - setacl PATH USER ACL: Set the ACL for the specified user on the folder.
  45. - deleteacl PATH USER ACL: Delete the ACL for the specified user on the folder.
  46. - getdesc PATH : Return the share description of the specified folder.
  47. - setdesc PATH DESC : Set the share description of the specified folder to DESC.
  48. - getshare PATH : Return the share parameters of the specified folder.
  49. ");
  50. }
  51. /**
  52. * Get a set of base options that this module adds to the CLI argument
  53. * parser.
  54. *
  55. * @return array The options.
  56. */
  57. public function getBaseOptions()
  58. {
  59. return array();
  60. }
  61. /**
  62. * Indicate if the module provides an option group.
  63. *
  64. * @return boolean True if an option group should be added.
  65. */
  66. public function hasOptionGroup()
  67. {
  68. return false;
  69. }
  70. /**
  71. * Return the title for the option group representing this module.
  72. *
  73. * @return string The group title.
  74. */
  75. public function getOptionGroupTitle()
  76. {
  77. return '';
  78. }
  79. /**
  80. * Return the description for the option group representing this module.
  81. *
  82. * @return string The group description.
  83. */
  84. public function getOptionGroupDescription()
  85. {
  86. return '';
  87. }
  88. /**
  89. * Return the options for this module.
  90. *
  91. * @return array The group options.
  92. */
  93. public function getOptionGroupOptions()
  94. {
  95. return array();
  96. }
  97. /**
  98. * Handle the options and arguments.
  99. *
  100. * @param mixed &$options An array of options.
  101. * @param mixed &$arguments An array of arguments.
  102. * @param array &$world A list of initialized dependencies.
  103. *
  104. * @return NULL
  105. */
  106. public function handleArguments(&$options, &$arguments, &$world)
  107. {
  108. }
  109. /**
  110. * Run the module.
  111. *
  112. * @param Horde_Cli $cli The CLI handler.
  113. * @param mixed $options An array of options.
  114. * @param mixed $arguments An array of arguments.
  115. * @param array &$world A list of initialized dependencies.
  116. *
  117. * @return NULL
  118. */
  119. public function run($cli, $options, $arguments, &$world)
  120. {
  121. if (!isset($arguments[1])) {
  122. $action = 'show';
  123. } else {
  124. $action = $arguments[1];
  125. }
  126. if (!isset($arguments[2])) {
  127. $folder_name = 'INBOX';
  128. } else {
  129. $folder_name = $arguments[2];
  130. }
  131. switch ($action) {
  132. case 'create':
  133. if (!isset($arguments[3])) {
  134. $folder = $world['storage']->getList()
  135. ->createFolder($folder_name);
  136. } else {
  137. $folder = $world['storage']->getList()
  138. ->createFolder($folder_name, $arguments[3]);
  139. }
  140. $this->_showFolder($folder_name, $world, $cli);
  141. break;
  142. case 'rename':
  143. $folder = $world['storage']->getList()
  144. ->renameFolder($folder_name, $arguments[3]);
  145. $this->_showFolder($arguments[3], $world, $cli);
  146. break;
  147. case 'delete':
  148. $folder = $world['storage']->getList()
  149. ->deleteFolder($folder_name);
  150. break;
  151. case 'getacl':
  152. $acl = $world['storage']->getList()
  153. ->getQuery(Horde_Kolab_Storage_List::QUERY_ACL)
  154. ->getAcl($folder_name);
  155. $cli->writeln($folder_name);
  156. $cli->writeln(str_repeat('=', strlen($folder_name)));
  157. $pad = max(array_map('strlen', array_keys($acl))) + 2;
  158. foreach ($acl as $user => $rights) {
  159. $cli->writeln(Horde_String::pad($user . ':', $pad) . $rights);
  160. }
  161. break;
  162. case 'getmyacl':
  163. $acl = $world['storage']->getList()
  164. ->getQuery(Horde_Kolab_Storage_List::QUERY_ACL)
  165. ->getMyAcl($folder_name);
  166. $cli->writeln('Your rights on ' . $folder_name . ': ' . $acl);
  167. break;
  168. case 'setacl':
  169. $acl = $world['storage']->getList()
  170. ->getQuery(Horde_Kolab_Storage_List::QUERY_ACL)
  171. ->setAcl($folder_name, $arguments[3], $arguments[4]);
  172. break;
  173. case 'deleteacl':
  174. $acl = $world['storage']->getList()
  175. ->getQuery(Horde_Kolab_Storage_List::QUERY_ACL)
  176. ->deleteAcl($folder_name, $arguments[3]);
  177. break;
  178. case 'getdesc':
  179. $list = $world['storage']->getList();
  180. $world['storage']->addListQuery(
  181. $list,
  182. Horde_Kolab_Storage_List::QUERY_SHARE
  183. );
  184. $cli->writeln(
  185. $list->getQuery(Horde_Kolab_Storage_List::QUERY_SHARE)
  186. ->getDescription($folder_name)
  187. );
  188. break;
  189. case 'setdesc':
  190. $list = $world['storage']->getList();
  191. $world['storage']->addListQuery(
  192. $list,
  193. Horde_Kolab_Storage_List::QUERY_SHARE
  194. );
  195. $list->getQuery(Horde_Kolab_Storage_List::QUERY_SHARE)
  196. ->setDescription($folder_name, $arguments[3]);
  197. break;
  198. case 'getshare':
  199. $list = $world['storage']->getList();
  200. $world['storage']->addListQuery(
  201. $list,
  202. Horde_Kolab_Storage_List::QUERY_SHARE
  203. );
  204. $parameters = $list->getQuery(Horde_Kolab_Storage_List::QUERY_SHARE)
  205. ->getParameters($folder_name);
  206. $pad = max(array_map('strlen', array_keys($parameters))) + 2;
  207. foreach ($parameters as $key => $value) {
  208. $cli->writeln(Horde_String::pad($key . ':', $pad) . $value);
  209. }
  210. break;
  211. case 'show':
  212. $this->_showFolder($folder_name, $world, $cli);
  213. break;
  214. default:
  215. $cli->message(
  216. sprintf(
  217. Horde_Kolab_Cli_Translation::t('Action %s not supported!'),
  218. $action
  219. ),
  220. 'cli.error'
  221. );
  222. break;
  223. }
  224. }
  225. private function _showFolder($folder_name, $world, $cli)
  226. {
  227. $folder = $world['storage']->getList()->getFolder($folder_name);
  228. $cli->writeln('Path: ' . $folder->getPath());
  229. $cli->writeln('Title: ' . $folder->getTitle());
  230. $cli->writeln('Owner: ' . $folder->getOwner());
  231. $cli->writeln('Type: ' . $folder->getType());
  232. $cli->writeln('Parent: ' . $folder->getParent());
  233. $cli->writeln('Namespace: ' . $folder->getNamespace());
  234. }
  235. }