You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

520 line
16 KiB

  1. <?php
  2. /**
  3. * Render html tag beginning
  4. *
  5. * @param string $name tag name
  6. * @param array $attrs tag attributes
  7. */
  8. function render_begin_tag($name, array $attrs = array()) {
  9. $tag = "<{$name}";
  10. foreach ($attrs as $key => $value) {
  11. $tag .= " {$key}=\"{$value}\"";
  12. }
  13. $tag .= ">";
  14. echo $tag;
  15. }
  16. /**
  17. * Render select element
  18. *
  19. * @param string $name select name
  20. * @param array $options data options
  21. */
  22. function render_select($name, array $options, $selectedIndex, array $attrs = array()) {
  23. $attrs["name"] = $name;
  24. render_begin_tag("select", $attrs);
  25. $select = "";
  26. foreach ($options as $key => $value) {
  27. $select .= "<option value=\"{$key}\"";
  28. if ($key == $selectedIndex) {
  29. $select .= " selected=\"selected\"";
  30. }
  31. $select .= ">" . $value . "</option>";
  32. }
  33. $select .= "</select>";
  34. echo $select;
  35. }
  36. /**
  37. * Construct a url from action and it's parameters
  38. *
  39. * @param string $action action name
  40. * @param array $params parameters
  41. * @return string
  42. */
  43. function url($action, array $params = array()) {
  44. unset($params["action"]);
  45. if (!strstr($action, ".")) {
  46. $action = __CONTROLLER__ . "." . $action;
  47. }
  48. $url = $_SERVER["PHP_SELF"] . "?action=" . $action;
  49. if (!empty($params)) {
  50. $url .= "&" . http_build_query($params);
  51. }
  52. return $url;
  53. }
  54. /**
  55. * Render navigation
  56. *
  57. * @param string $db database name
  58. * @param string|null $collection collection name
  59. * @param boolean $extend if extend the parameters
  60. */
  61. function render_navigation($db, $collection = null, $extend = true) {
  62. $dbpath = url("db.index", array("db" => $db));
  63. $navigation = '<a href="' . url("server.databases") . '"><img src="' . rock_theme_path() . '/images/world.png" width="14" align="absmiddle"/> ' . rock_lang("databases") . '</a> &raquo; <a href="' .$dbpath . '"><img src="' . rock_theme_path() . '/images/database.png" width="14" align="absmiddle"/> ' . $db . "</a>";
  64. if(!is_null($collection)) {
  65. $navigation .= " &raquo; <a href=\"" . url("collection.index", $extend ? xn() : array( "db" => $db, "collection" => $collection )) . "\">";
  66. $navigation .= '<img src="' . rock_theme_path() . '/images/' . r_get_collection_icon($collection) . '.png" width="14" align="absmiddle"/> ';
  67. $navigation .= $collection . "</a>";
  68. }
  69. echo $navigation;
  70. }
  71. /**
  72. * Render quick links on top-bar
  73. *
  74. */
  75. function render_manual_items() {
  76. $items = array(
  77. '<a href="http://docs.mongodb.org/manual/reference/operators/" target="_blank">' . rock_lang("querying") . '</a>',
  78. '<a href="http://docs.mongodb.org/manual/applications/update/" target="_blank">' . rock_lang("updating") . '</a>',
  79. '<a href="http://docs.mongodb.org/manual/reference/command/" target="_blank">' . rock_lang("commands") . '</a>',
  80. '<a href="http://api.mongodb.org/js/" target="_blank">' . rock_lang("jsapi") . '</a>',
  81. '<a href="http://www.php.net/manual/en/book.mongo.php" target="_blank">' . rock_lang("phpmongo") . '</a>'
  82. );
  83. //plugins
  84. if (class_exists("RFilter")) {
  85. RFilter::apply("MANUAL_MENU_FILTER", $items);
  86. }
  87. foreach ($items as $item) {
  88. echo $item . "<br/>";
  89. }
  90. }
  91. /**
  92. * Render server operations
  93. *
  94. * @param string|null $currentAction current operation action
  95. * @since 1.1.0
  96. */
  97. function render_server_menu($currentAction = null) {
  98. $menuItems = array(
  99. array( "action" => "server.index", "name" => rock_lang("server")),
  100. array( "action" => "server.status", "name" => rock_lang("status")),
  101. array( "action" => "server.databases", "name" => rock_lang("databases")),
  102. array( "action" => "server.processlist", "name" => rock_lang("processlist")),
  103. array( "action" => "server.command", "params" => array("db"=>xn("db")), "name" => rock_lang("command")),
  104. array( "action" => "server.execute", "params" => array("db"=>xn("db")), "name" => rock_lang("execute")),
  105. array( "action" => "server.replication", "name" => rock_lang("master_slave")),
  106. );
  107. //plugin
  108. if (class_exists("RFilter")) {
  109. RFilter::apply("SERVER_MENU_FILTER", $menuItems);
  110. }
  111. $string = "";
  112. $count = count($menuItems);
  113. foreach ($menuItems as $index => $op) {
  114. $string .= '<a href="' . url($op["action"], isset($op["params"]) ? $op["params"] : array()) . '"';
  115. if (__CONTROLLER__ . "." . __ACTION__ == $op["action"] || $currentAction == $op["action"]) {
  116. $string .= ' class="current"';
  117. }
  118. foreach ($op as $attrName => $attrValue) {
  119. if (preg_match("/^attr\\.(\\w+)/", $attrName, $match)) {
  120. $string .= " " . $match[1] . "=\"" . $attrValue . "\"";
  121. }
  122. }
  123. $string .= ">" . $op["name"] . "</a>";
  124. if ($index < $count - 1) {
  125. $string .= " | ";
  126. }
  127. }
  128. echo $string;
  129. }
  130. /**
  131. * Render database operations
  132. *
  133. * @param string $dbName database name
  134. * @since 1.1.0
  135. */
  136. function render_db_menu($dbName) {
  137. $menuItems = array(
  138. array( "action" => "db.index", "params" => array("db"=>$dbName), "name" => rock_lang("statistics") ),
  139. array( "action" => "db.newCollection", "params" => array("db"=>$dbName), "name" => rock_lang("create_collection") ),
  140. array( "action" => "server.command", "params" => array("db"=>$dbName), "name" => rock_lang("command") ),
  141. array( "action" => "server.execute", "params" => array("db"=>$dbName), "name" => rock_lang("execute") ),
  142. array( "action" => "db.dbTransfer", "params" => array("db"=>$dbName), "name" => rock_lang("transfer") ),
  143. array( "action" => "db.dbExport", "params" => array("db"=>$dbName, "can_download"=>1), "name" => rock_lang("export"), "can_download" => 1 ),
  144. array( "action" => "db.dbImport", "params" => array("db"=>$dbName), "name" => rock_lang("import") ),
  145. array( "action" => "db.profile", "params" => array("db"=>$dbName), "name" => rock_lang("profile")),
  146. array( "action" => "db.repairDatabase", "params" => array("db"=>$dbName), "name" => rock_lang("repair"), "attr.onclick" => "return window.confirm('" . rock_lang("repairdbmsg") . " {$dbName}?');" ),
  147. array( "action" => "db.auth", "params" => array("db"=>$dbName), "name" => rock_lang("authentication") ),
  148. array( "action" => "db.dropDatabase", "params" => array("db"=>$dbName), "name" => rock_lang("drop"), "attr.onclick" => "return window.confirm('" . rock_lang("dropwarning") . " " . $dbName . "? " . rock_lang("dropwarning2") . "');")
  149. );
  150. //plugin
  151. if (class_exists("RFilter")) {
  152. RFilter::apply("DB_MENU_FILTER", $menuItems, array( "dbName" => $dbName ) );
  153. }
  154. $displayCount = 7;
  155. $hasMore = false;
  156. $string = "";
  157. $count = count($menuItems);
  158. foreach ($menuItems as $index => $op) {
  159. if ($index >= $displayCount && !$hasMore) {
  160. $hasMore = true;
  161. $string .= "<a href=\"#\" onclick=\"showMoreMenus(this);return false;\">" . rock_lang("more") . " &raquo;</a>";
  162. $string .= "<div class=\"menu\">";
  163. }
  164. if (is_string($op)) {
  165. if ($op == "-") {
  166. $string .= "-----------";
  167. }
  168. else {
  169. $string .= $op;
  170. }
  171. }
  172. else if (!empty($op["action"])) {
  173. $string .= '<a href="' . url($op["action"], isset($op["params"]) ? $op["params"] : array()) . '"';
  174. if (__CONTROLLER__ . "." . __ACTION__ == $op["action"]) {
  175. $string .= ' class="current"';
  176. }
  177. foreach ($op as $attrName => $attrValue) {
  178. if (preg_match("/^attr\\.(\\w+)/", $attrName, $match)) {
  179. $string .= " " . $match[1] . "=\"" . $attrValue . "\"";
  180. }
  181. }
  182. $string .= ">" . $op["name"] . "</a>";
  183. }
  184. else {
  185. if (!empty($op["url"])) {
  186. $string .= "<a href=\"" . $op["url"] . "\" target=\"_blank\">";
  187. }
  188. $string .= $op["name"];
  189. if (!empty($op["url"])) {
  190. $string .= "</a>";
  191. }
  192. }
  193. if ($hasMore) {
  194. $string .= "<br/>";
  195. }
  196. else {
  197. if ($index < $count - 1) {
  198. $string .= " | ";
  199. }
  200. }
  201. }
  202. if ($hasMore) {
  203. $string .= "</div>";
  204. }
  205. echo $string;
  206. }
  207. /**
  208. * Render collection operations
  209. *
  210. * Menu definition:
  211. * - array ( "action" => "ACTION", "params" => array( ... ), "name" => "NAME" )
  212. * - array ( "url" => "http://....", "name" => "NAME" )
  213. * - - //separator line
  214. *
  215. * @param string $dbName database name
  216. * @param string $collectionName collection name
  217. * @since 1.1.0
  218. */
  219. function render_collection_menu($dbName, $collectionName) {
  220. $params = xn();
  221. $exportParams = $params;
  222. $exportParams["can_download"] = 1;
  223. $menuItems = array(
  224. array( "action" => "collection.createRow", "params" => $params, "name" => rock_lang("insert") ),
  225. array( "action" => "collection.clearRows", "params" => $params, "name" => rock_lang("clear"), "attr.onclick" => "return window.confirm('Are you sure to delete all records in collection \\'" . $collectionName . "\\'?');" ),
  226. array( "action" => "#", "params" => array(), "name" => rock_lang("new_field"), "attr.onclick" => "fieldOpNew();return false;" ),
  227. array( "action" => "collection.collectionStats", "params" => $params, "name" => rock_lang("statistics") ),
  228. array( "action" => "collection.collectionExport", "params" => $exportParams, "name" => rock_lang("export") ),
  229. array( "action" => "collection.collectionImport", "params" => $params, "name" => rock_lang("import") ),
  230. array( "action" => "collection.collectionProps", "params" => $params, "name" => rock_lang("properties") ),
  231. array( "action" => "collection.collectionIndexes", "params" => $params, "name" => rock_lang("indexes") ),
  232. array( "action" => "collection.collectionRename", "params" => $params, "name" => rock_lang("rename") ),
  233. array( "action" => "collection.collectionDuplicate", "params" => $params, "name" => rock_lang("duplicate") ),
  234. array( "action" => "collection.collectionTransfer", "params" => $params, "name" => rock_lang("transfer") ),
  235. array( "action" => "collection.collectionValidate", "params" => $params, "name" => rock_lang("validate") ),
  236. array( "action" => "collection.removeCollection", "params" => $params, "name" => rock_lang("drop"), "attr.onclick" => "return window.confirm('Are you sure to drop collection \\'" . $collectionName . "\\'?')" ),
  237. );
  238. //plugin
  239. if (class_exists("RFilter")) {
  240. RFilter::apply("COLLECTION_MENU_FILTER", $menuItems, array( "dbName" => $dbName, "collectionName" => $collectionName ));
  241. }
  242. $displayCount = 6;
  243. $hasMore = false;
  244. $string = "";
  245. $count = count($menuItems);
  246. foreach ($menuItems as $index => $op) {
  247. if ($index >= $displayCount && !$hasMore) {
  248. $hasMore = true;
  249. $string .= "<a href=\"#\" onclick=\"showMoreMenus(this);return false;\">" . rock_lang("more") . " &raquo;</a>";
  250. $string .= "<div class=\"menu\">";
  251. }
  252. if (is_string($op)) {
  253. if ($op == "-") {
  254. $string .= "-----------";
  255. }
  256. else {
  257. $string .= $op;
  258. }
  259. }
  260. else if (!empty($op["action"])) {
  261. $string .= '<a href="' . url($op["action"], isset($op["params"]) ? $op["params"] : array()) . '"';
  262. if (__CONTROLLER__ . "." . __ACTION__ == $op["action"]) {
  263. $string .= ' class="current"';
  264. }
  265. foreach ($op as $attrName => $attrValue) {
  266. if (preg_match("/^attr\\.(\\w+)/", $attrName, $match)) {
  267. $string .= " " . $match[1] . "=\"" . $attrValue . "\"";
  268. }
  269. }
  270. $string .= ">" . $op["name"] . "</a>";
  271. }
  272. else {
  273. if (!empty($op["url"])) {
  274. $string .= "<a href=\"" . $op["url"] . "\" target=\"_blank\">";
  275. }
  276. $string .= $op["name"];
  277. if (!empty($op["url"])) {
  278. $string .= "</a>";
  279. }
  280. }
  281. if ($hasMore) {
  282. $string .= "<br/>";
  283. }
  284. else {
  285. if ($index < $count - 1) {
  286. $string .= " | ";
  287. }
  288. }
  289. }
  290. if ($hasMore) {
  291. $string .= "</div>";
  292. }
  293. echo $string;
  294. }
  295. /**
  296. * Render document operations
  297. *
  298. * @param string $dbName database name
  299. * @param string $collectionName collection name
  300. * @param mixed $docId document id
  301. * @param integer $docIndex document index
  302. * @since 1.1.0
  303. */
  304. function render_doc_menu($dbName, $collectionName, $docId, $docIndex) {
  305. $menuItems = array(
  306. array ( "action" => "collection.none", "name" => rock_lang("text"), "attr.onclick" => "changeText('{$docIndex}');return false;" ),
  307. array ( "action" => "collection.none", "name" => "Expand", "attr.id" => "expand_{$docIndex}", "attr.onclick" => "expandText('{$docIndex}');return false;" ),
  308. );
  309. //plugin
  310. if (class_exists("RFilter")) {
  311. RFilter::apply("DOC_MENU_FILTER", $menuItems, array( "dbName" => $dbName, "collectionName" => $collectionName, "docId" => $docId, "docIndex" => $docIndex ));
  312. }
  313. $displayCount = 2;
  314. $hasMore = false;
  315. $string = "";
  316. $count = count($menuItems);
  317. foreach ($menuItems as $index => $op) {
  318. if ($index >= $displayCount && !$hasMore) {
  319. $hasMore = true;
  320. $string .= "<a href=\"#\" onclick=\"showMoreDocMenus(this, {$docIndex});return false;\">" . rock_lang("more") . " &raquo;</a>";
  321. $string .= "<div class=\"doc_menu doc_menu_{$docIndex}\">";
  322. }
  323. if (is_string($op)) {
  324. if ($op == "-") {
  325. $string .= "-----------";
  326. }
  327. else {
  328. $string .= $op;
  329. }
  330. }
  331. else if (!empty($op["action"])) {
  332. $string .= '<a href="' . url($op["action"], isset($op["params"]) ? $op["params"] : array()) . '"';
  333. if (__CONTROLLER__ . "." . __ACTION__ == $op["action"]) {
  334. $string .= ' class="current"';
  335. }
  336. foreach ($op as $attrName => $attrValue) {
  337. if (preg_match("/^attr\\.(\\w+)/", $attrName, $match)) {
  338. $string .= " " . $match[1] . "=\"" . $attrValue . "\"";
  339. }
  340. }
  341. $string .= ">" . $op["name"] . "</a>";
  342. }
  343. else {
  344. if (!empty($op["url"])) {
  345. $string .= "<a href=\"" . $op["url"] . "\" target=\"_blank\">";
  346. }
  347. $string .= $op["name"];
  348. if (!empty($op["url"])) {
  349. $string .= "</a>";
  350. }
  351. }
  352. if ($hasMore) {
  353. $string .= "<br/>";
  354. }
  355. else {
  356. if ($index < $count - 1) {
  357. $string .= " | ";
  358. }
  359. }
  360. }
  361. if ($hasMore) {
  362. $string .= "</div>";
  363. }
  364. echo $string;
  365. }
  366. /**
  367. * Render supported data types
  368. *
  369. * @param string $name tag name
  370. * @param string|null $selected selected type
  371. * @since 1.1.0
  372. */
  373. function render_select_data_types($name, $selected = null) {
  374. $types = array (
  375. "integer" => "Integer",
  376. "long" => "Long",
  377. "double" => "Double",
  378. "string" => "String",
  379. "boolean" => "Boolean",
  380. "null" => "NULL",
  381. "mixed" => "Mixed"
  382. );
  383. render_select($name, $types, $selected);
  384. }
  385. /**
  386. * Render a server list
  387. *
  388. * @param string $name tag name
  389. * @param array $servers server configs
  390. * @param integer $selectedIndex selected server index
  391. * @param array $attrs tag attributes
  392. * @since 1.1.0
  393. */
  394. function render_server_list($name, $servers, $selectedIndex = 0, array $attrs = array()) {
  395. $options = array();
  396. foreach ($servers as $index => $server) {
  397. $server = new MServer($server);
  398. $options[$index] = $server->mongoName();
  399. }
  400. render_select($name, $options, $selectedIndex, $attrs);
  401. }
  402. /**
  403. * Render server hosts
  404. *
  405. * @param string $name tag name
  406. * @param string|null $selected selected host index
  407. * @since 1.1.0
  408. */
  409. function render_select_hosts($name = "host", $selected = null) {
  410. global $MONGO;
  411. $hosts = array();
  412. foreach ($MONGO["servers"] as $config) {
  413. $server = new MServer($config);
  414. $hosts[] = $server->mongoName();
  415. }
  416. render_select($name, $hosts, $selected, array( "class" => "select_hosts" ));
  417. }
  418. /**
  419. * Render a view file
  420. *
  421. * examples:
  422. * - h_include("header", "title=DocumentTitle")
  423. * - h_include("footer")
  424. *
  425. * @param string $view view file name
  426. * @param string|array $vars a key-value array or name=iwind&age=18
  427. * @since 1.1.0
  428. */
  429. function render_view($view, $vars = null) {
  430. if (is_string($vars)) {
  431. parse_str($vars);
  432. }
  433. else if (is_array($vars)) {
  434. extract($vars);
  435. }
  436. $view = dirname(__ROOT__) . DS . rock_theme_path() . DS . "views" . DS . str_replace(".", DS, $view) . ".php";
  437. require $view;
  438. }
  439. function render_theme_path() {
  440. echo rock_theme_path();
  441. }
  442. /**
  443. * Render page header
  444. *
  445. * @since 1.1.0
  446. */
  447. function render_page_header() {
  448. if (class_exists("REvent")) {
  449. REvent::dispatch("RENDER_PAGE_HEADER_EVENT");
  450. }
  451. }
  452. /**
  453. * Render page footer
  454. *
  455. * @since 1.1.0
  456. */
  457. function render_page_footer() {
  458. if (class_exists("REvent")) {
  459. REvent::dispatch("RENDER_PAGE_FOOTER_EVENT");
  460. }
  461. }
  462. /**
  463. * Render response from server
  464. *
  465. * @param string $response response
  466. * @since 1.1.0
  467. */
  468. function render_server_response($response) {
  469. $string = "<div style=\"border:2px #ccc solid;margin-bottom:5px;background-color:#eeefff\">" .
  470. rock_lang("responseserver") . "
  471. <div style=\"margin-top:5px\">
  472. {$response}
  473. </div>
  474. </div>";
  475. echo $string;
  476. }
  477. /**
  478. * Render url for an action
  479. *
  480. * @param string $action action name
  481. * @param array $params parameters
  482. * @since 1.1.0
  483. */
  484. function render_url($action, array $params = array()) {
  485. echo url($action, $params);
  486. }
  487. ?>