PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/gdisplay.php

http://inline-google-docs.googlecode.com/
PHP | 620 lines | 390 code | 89 blank | 141 comment | 20 complexity | b9903cd089a172e51594a02b19ba0cbe MD5 | raw file
  1. <?php
  2. /**
  3. * Display class, GDocs Wordpress plugin
  4. *
  5. * Handles HTML formatting
  6. *
  7. * @author Lim Jiunn Haur <codex.is.poetry@gmail.com>
  8. * @copyright Copyright (c) 2008, Lim Jiunn Haur
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @package gdocs
  11. * @subpackage gdocs.inc
  12. * @since 0.5
  13. * @version 0.9
  14. */
  15. /**
  16. * GDisplay class
  17. *
  18. * No special OOP techniques used here. This class just
  19. * groups all display-related functions together.
  20. * @author Lim Jiunn Haur <codex.is.poetry@gmail.com>
  21. * @copyright Copyright (c) 2008, Lim Jiunn Haur
  22. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  23. * @package gdocs
  24. * @subpackage gdocs.inc
  25. * @since 0.5
  26. * @version 0.9
  27. */
  28. class GDisplay {
  29. /**#@+
  30. * @static
  31. */
  32. /**
  33. * Array of all stylesheets used on this page
  34. *
  35. * Keeps a record so that any given stylesheet
  36. * isn't imported more than once on the same page
  37. * @var array
  38. */
  39. private static $stylesheets = array ();
  40. /**
  41. * Prints head of configuration page
  42. */
  43. public static function printHead (){
  44. ?>
  45. <style type="text/css">
  46. div#gdocs_left {
  47. float:left;
  48. width:50%;
  49. }
  50. div#gdocs_right {
  51. float:right;
  52. width:50%;
  53. }
  54. div#gdocs_right td.gdocs_loader {
  55. background:#cfebf7 url("<?php echo GDOCS_ADDRESS ?>/inc/img/ajax-loader.gif") center right no-repeat;
  56. }
  57. div#gdocs_right tr.gdocs_loader td{
  58. background-color:#cfebf7;
  59. }
  60. div#gdocs_right table.hor-zebra {
  61. font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
  62. font-size: 10px;
  63. margin: 15px 0;
  64. text-align: left;
  65. border-collapse: collapse;
  66. }
  67. div#gdocs_right table.hor-zebra th {
  68. font-size: 12px;
  69. font-weight: normal;
  70. padding: 10px 8px;
  71. color: #039;
  72. }
  73. div#gdocs_right table.hor-zebra td {
  74. padding: 8px;
  75. color: #669;
  76. }
  77. div#gdocs_right table.hor-zebra .odd {
  78. background: #e8edff;
  79. }
  80. span.description {
  81. font-style:normal;
  82. }
  83. </style>
  84. <div class='wrap'>
  85. <h2>Inline Google Docs</h2>
  86. <div id='gdocs_left'>
  87. <form method='post' action='options.php'>
  88. <?php
  89. if (function_exists ('settings_fields')){
  90. settings_fields ('gdocs-options');
  91. }else {
  92. ?>
  93. <input type="hidden" name="action" value="update" />
  94. <input type="hidden" name="page_options" value="gdocs_user,gdocs_pwd,gdocs_proxy_host,gdocs_proxy_port,gdocs_proxy_user,gdocs_proxy_pwd,gdocs_cache_expiry,gdocs_style_dir" />
  95. <?php
  96. wp_nonce_field ('update-options');
  97. }
  98. ?>
  99. <?php
  100. }
  101. /**
  102. * Prints login credentials input form
  103. */
  104. public static function printLogin (){
  105. ?>
  106. <!-- Login Credentials -->
  107. <h3>Google Account Login</h3>
  108. <table class='form-table'>
  109. <tbody>
  110. <tr valign="top">
  111. <th scope="row"><label for='gdocs_user'>Username</label></th>
  112. <td><input id='gdocs_user' type="text" size="40" name="gdocs_user" value="<?php echo get_option ('gdocs_user'); ?>" /><br/><span class='description'><strong>For Google Apps users, append your username with </strong><code>@yourdomain.com</code></span></td>
  113. </tr>
  114. <tr valign="top">
  115. <th scope="row"><label for='gdocs_pwd'>Password</label></th>
  116. <td><input id='gdocs_pwd' type="password" size="40" name="gdocs_pwd" value="<?php echo get_option ('gdocs_pwd'); ?>" /></td>
  117. </tr>
  118. </tbody>
  119. </table>
  120. <?php
  121. }
  122. /**
  123. * Prints proxy settings input form
  124. */
  125. public static function printProxy (){
  126. ?>
  127. <!-- Proxy Settings -->
  128. <h3>Proxy Settings</h3>
  129. <span class='description'>Leave this section blank if your host is not behind a proxy.</span>
  130. <table class='form-table'>
  131. <tbody>
  132. <tr valign="top">
  133. <th scope="row"><label for="gdocs_proxy_host">Host</label></th>
  134. <td><input type="text" size="40" name="gdocs_proxy_host" id="gdocs_proxy_host" value="<?php echo get_option ('gdocs_proxy_host'); ?>" /></td>
  135. </tr>
  136. <tr valign="top">
  137. <th scope="row"><label for="gdocs_proxy_port">Port</label></th>
  138. <td><input type="text" size="40" name="gdocs_proxy_port" id="gdocs_proxy_port" value="<?php echo get_option ('gdocs_proxy_port'); ?>" /></td>
  139. </tr>
  140. <tr valign="top">
  141. <th scope="row"><label for="gdocs_proxy_user">Username</label></th>
  142. <td><input type="text" size="40" name="gdocs_proxy_user" id="gdocs_proxy_user" value="<?php echo get_option ('gdocs_proxy_user'); ?>" /></td>
  143. </tr>
  144. <tr valign="top">
  145. <th scope="row"><label for="gdocs_proxy_pwd">Password</label></th>
  146. <td><input type="password" size="40" name="gdocs_proxy_pwd" id="gdocs_proxy_pwd" value="<?php echo get_option ('gdocs_proxy_pwd'); ?>" /></td>
  147. </tr>
  148. </tbody>
  149. </table>
  150. <?php
  151. }
  152. /**
  153. * Prints cache settings input form
  154. */
  155. public static function printCache (){
  156. ?>
  157. <!-- Cache Settings -->
  158. <h3>Cache Settings</h3>
  159. <table class='form-table'>
  160. <tbody>
  161. <tr valign="top">
  162. <th scope="row"><label for='gdocs_cache_expiry'>Lifespan</label></th>
  163. <td><input type="text" size="40" id='gdocs_cache_expiry' name="gdocs_cache_expiry" value="<?php echo get_option ('gdocs_cache_expiry'); ?>" /><span class='description'>minutes</span><br/><span class='description'><strong>Set to <code>0</code> to turn off caching.</strong></span></td>
  164. </tr>
  165. </tbody>
  166. </table>
  167. <?php
  168. }
  169. /**
  170. * Prints style settings input form
  171. */
  172. public static function printStyle (){
  173. ?>
  174. <!-- Stylesheet Settings -->
  175. <h3>Stylesheet Settings</h3>
  176. <table class='form-table'>
  177. <tbody>
  178. <tr valign='top'>
  179. <th scope='row'><label for='gdocs_style_dir'>Directory</label></th>
  180. <td><span class='description'><?php bloginfo ('wpurl')?>/</span><input type='text' size="30" id="gdocs_style_dir" name="gdocs_style_dir" value="<?php echo get_option ('gdocs_style_dir'); ?>" /><br /><span class='description'><strong>Specify the directory where you keep your CSS stylesheets in.</strong></span></td>
  181. </tr>
  182. </tbody>
  183. </table>
  184. <?php
  185. }
  186. /**
  187. * Prints foot of configuration page
  188. */
  189. public static function printFoot (){
  190. ?>
  191. <p class='submit'>
  192. <input type='submit' name='Submit' value="<?php _e('Save changes') ?>" class='button-primary' />
  193. </p>
  194. </form>
  195. </div>
  196. <?php
  197. }
  198. /**
  199. * Prints list of documents
  200. */
  201. public static function printDocList (){
  202. ?>
  203. <div id='gdocs_right'>
  204. <!-- Document List -->
  205. <h3>Google Documents <small><code id='dc' style='display:none'><span></span> documents</code></small></h3>
  206. <small>Use the corresponding Document ID in your shortcode.<br />
  207. <span style="color: blue">[gdocs id=<em>doc_id</em> type='document']</span></small>
  208. <table class='hor-zebra'>
  209. <thead>
  210. <tr><th>Title</th><th>Document ID</th></tr>
  211. </thead>
  212. <tbody id='gdocs_list_document'>
  213. <tr class='gdocs_loader'><td>Loading...</td><td class='gdocs_loader'></td></tr>
  214. </tbody>
  215. </table>
  216. <?php
  217. }
  218. /**
  219. * Prints list of spreadsheets
  220. */
  221. public static function printStList (){
  222. ?>
  223. <!-- Spreadsheet List -->
  224. <h3>Google Spreadsheets <small><code id='sc' style='display:none'><span></span> spreadsheets</code></small></h3>
  225. <small>Use the corresponding Spreadsheet ID and Worksheet ID in your shortcode.<br />
  226. <span style="color: blue">[gdocs st_id=<em>spreadsheet_id</em> wt_id=<em>worksheet_id</em> type='spreadsheet']</span></small>
  227. <table class='hor-zebra'>
  228. <thead>
  229. <tr><th>Title</th><th>Spreadsheet ID</th><th>Worksheets</th></tr>
  230. </thead>
  231. <tbody id='gdocs_list_spreadsheet'>
  232. <tr class='gdocs_loader'><td>Loading...</td><td class='gdocs_loader'></td><td></td></tr>
  233. </tbody>
  234. </table>
  235. </div>
  236. <p style='clear:both'></p>
  237. </div>
  238. <?php
  239. }
  240. /**
  241. * Prints cache not writable
  242. */
  243. public static function printCacheNotWritableError (){
  244. $user = exec ('whoami');
  245. $grp = exec ("groups {$user}");
  246. $grp = str_replace (' ', ', ', $grp);
  247. ?>
  248. <div class='error' id='message_100' style='background-color: rgb(255, 170, 150);'><p><strong><?php _e("The cache folder is not writable.<br/>Current user: <code>{$user}</code> | Groups: <code>$grp</code>") ?></strong></p></div>
  249. <?php
  250. }
  251. /**
  252. * Prints log not writable
  253. */
  254. public static function printLogNotWritableError (){
  255. $user = exec ('whoami');
  256. $grp = exec ("groups {$user}");
  257. $grp = str_replace (' ', ', ', $grp);
  258. ?>
  259. <div class='error' id='message_101' style='background-color: rgb(255, 170, 150);'><p><strong><?php _e("The <a href='" . GDOCS_ADDRESS . "/cache/error.log.php'>log file</a> is not writable.<br/>Current user: <code>{$user}</code> | Groups: <code>$grp</code>") ?></strong></p></div>
  260. <?php
  261. }
  262. /**
  263. * Prints CAPTCHA verfication form
  264. * @param Zend_Gdata_App_CaptchaRequiredException $e
  265. * @return string $html HTML string
  266. */
  267. public static function printCaptchaError (Zend_Gdata_App_CaptchaRequiredException $e){
  268. $obj = strpos ($_SERVER['HTTP_REFERER'], 'options-general.php') === FALSE ? 'GDocs' : 'GDocsOptions';
  269. return
  270. "Google requested CAPTCHA verification.<br/>
  271. <img src='" . $e->getCaptchaUrl() . "' style='margin:5px 0 4px' />
  272. <form method='post' action='options.php' onsubmit='javascript: {$obj}.verify (this); return false;'>
  273. <input type='hidden' name='gdocs_token' id='gdocs_token' value='" . $e->getCaptchaToken() . "' />
  274. <input type='text' name='gdocs_captcha' size='40' id='gdocs_captcha' />
  275. <input type='submit' value='Submit' onclick='javascript: {$obj}.verify (this); return false;' />
  276. </form>";
  277. }
  278. /**
  279. * Prints a postbox in the edit-post / edit-page page
  280. * Lists all Google documents and Google Spreadsheets
  281. * Lets user add shortcode tag to post by clicking
  282. * @param array $results array of rows returned from the database
  283. */
  284. public static function printHelper ($results){
  285. $html = ""; $doc_count = 0;
  286. if ($results) {
  287. foreach ($results as $row){
  288. // <a href='#' onclick="javascript: GDocs.ring('MAIN_ID+SUB_ID', 'TYPE', 'TITLE'); return false;" id='MAIN_ID+SUB_ID+type'>
  289. $sub = $row->sub_title ? "<br/>[" . $row->sub_title . "]" : "";
  290. $html .= sprintf ("<span class='gdocs_%s'><a href='#' onclick=\"javascript: GDocs.ring ('%s+%s', '%s'); return false;\" id='%s+%s'>%s{$sub}</a></span>", $row->type, $row->main_id, $row->sub_id, $row->type, $row->main_id, $row->sub_id, $row->title, $row->sub_title);
  291. if ($row->type === 'document') $doc_count++;
  292. }
  293. }
  294. ?>
  295. <!-- Begin G Docs Helper -->
  296. <style type='text/css'>
  297. div#gdocs_helper .inside span{
  298. display:block;
  299. float:left;
  300. margin:10px 0;
  301. min-height:110px;
  302. min-width:160px;
  303. text-align:center;
  304. }
  305. div#gdocs_helper .inside span a{
  306. position:relative;
  307. top:80px;
  308. line-height:1.3em;
  309. }
  310. img#gdocs_helper_ajax{
  311. border: 0 none;
  312. }
  313. .gdocs_head {
  314. float:right;
  315. margin-right:3px;
  316. }
  317. .gdocs_error {
  318. border-width: 1px;
  319. border-style: solid;
  320. border-color: red;
  321. padding: 0 0.6em;
  322. margin: 5px 15px 2px;
  323. -moz-border-radius: 3px;
  324. -khtml-border-radius: 3px;
  325. -webkit-border-radius: 3px;
  326. border-radius: 3px;
  327. }
  328. .gdocs_error p {
  329. margin: 0.5em 0;
  330. line-height: 1;
  331. padding: 2px;
  332. }
  333. span.gdocs_document {
  334. background:transparent url("<?php echo GDOCS_ADDRESS ?>/inc/img/document.png") no-repeat center top;
  335. }
  336. span.gdocs_document:hover {
  337. background:transparent url("<?php echo GDOCS_ADDRESS ?>/inc/img/document_highlight.png") no-repeat center top;
  338. }
  339. span.gdocs_spreadsheet {
  340. background:transparent url("<?php echo GDOCS_ADDRESS ?>/inc/img/spreadsheet.png") no-repeat center top;
  341. }
  342. span.gdocs_spreadsheet:hover {
  343. background:transparent url("<?php echo GDOCS_ADDRESS ?>/inc/img/spreadsheet_highlight.png") no-repeat center top;
  344. }
  345. </style>
  346. <div id='gdocs_helper' class='postbox open'>
  347. <h3>
  348. <a class='gdocs_head' href="#" onclick="javascript: GDocs.updateList(); return false;"><img id='gdocs_helper_ajax' src='<?php echo GDOCS_ADDRESS . "/inc/img/ajax-refresh.png" ?>' /></a>
  349. Google Documents/Spreadsheets
  350. <small class='gdocs_head'>
  351. <code id='gdocs_count'><span><?php echo $doc_count ?></span> documents | <span><?php echo (sizeof($results) - $doc_count) ?></span> worksheets</code>
  352. </small>
  353. </h3>
  354. <div class='inside'>
  355. <noscript><div class='gdocs_error' id='gdocs_js_error' style='background-color: rgb(255, 170, 150);'><p><strong><?php _e("Enable Javascript to use this panel.") ?></strong></p></div></noscript>
  356. <?php
  357. if ($results) echo $html;
  358. else {
  359. ?>
  360. <div class='gdocs_error' id='gdocs_db_error' style='background-color: rgb(255, 170, 150);'><p><strong><?php _e("The plugin was unable to connect to the database. Refresh this box to see the list of documents and spreadsheets available.") ?></strong></p></div>
  361. <?php } ?>
  362. </div>
  363. <div style="clear:both"></div>
  364. </div>
  365. <!-- End G Docs helper -->
  366. <?php
  367. }
  368. /**
  369. * Formats and prints tablesorter initialization script
  370. * @param string $st_id spreadsheet id
  371. * @param string $wt_id worksheet id
  372. * @param string $params parameters to pass to tablesorter
  373. */
  374. public static function printSortScript ($st_id, $wt_id, $params){
  375. return
  376. "<script type='text/javascript' language='javascript'>
  377. jQuery(document).ready(function() {
  378. jQuery('#gdocs_{$st_id}_{$wt_id}').tablesorter({$params});
  379. });
  380. </script>";
  381. }
  382. /**
  383. * Formats div tag for document
  384. * @param string $id id of document
  385. * @param string $content html to embed
  386. * @param string $style style classes to include
  387. * @return string $html formatted string
  388. */
  389. public static function printDocTag ($id, $style=NULL){
  390. $classes = preg_split ("/,(\s)?/", $style);
  391. $classes = implode (' ', $classes);
  392. return "<div class='gdocs {$classes}' id='gdocs_{$id}'>";
  393. }
  394. /**
  395. * Formats table tag
  396. * @param string $st_id spreadsheet id
  397. * @param string $wt_id worksheet id
  398. * @param string $style predefined stylesheet
  399. * @return string $html formatted table tag
  400. */
  401. public static function printStTblTag ($st_id, $wt_id, $style = NULL){
  402. /*
  403. * class=$st_id to identify with a certain spreadsheet
  404. * id=$st_id $wt_id to make this worksheet unique
  405. * class=gdocs allows global styles to be set across all Google Docs/Spreadsheets
  406. * class=$style to associate this table with a predefined stylesheet
  407. */
  408. $classes = preg_split ("/,(\s)?/", $style);
  409. $classes = implode (' ', $classes);
  410. return "<table class='gdocs gdocs_{$st_id} {$classes}' id='gdocs_{$st_id}_{$wt_id}'>\r\n";
  411. }
  412. /**
  413. * Formats HTML to display spreadsheet content in a table
  414. * @param Zend_Gdata_Feed $feed list feed
  415. * @param string $headers comma-separated custom column titles to replace original titles with
  416. */
  417. public static function printStTbl (Zend_Gdata_Feed $feed, $headers = NULL){
  418. // convert to spreadsheet list feed object
  419. $feed = new Zend_Gdata_Spreadsheets_ListFeed ($feed->getDom ());
  420. // if headers are specified or entries are given
  421. if (isset ($headers) || $feed->entries[0]){
  422. // start <thead>
  423. $html .= "\t<thead>\r\n";
  424. $html .= "\t\t<tr class='row_0'>\r\n\t\t\t";
  425. $k = 0;
  426. if (isset ($headers)){
  427. // custom headers given
  428. $colHeads = preg_split ("/,(\s)?/", $headers);
  429. // get all the headers specified by user
  430. foreach ($colHeads as $colHead){
  431. $colax = $k%2==0 ?'odd' : 'even';
  432. $html .= "<th class='col_{$k} {$colax}'>" . $colHead . "</th>";
  433. $k++;
  434. }
  435. }
  436. if ($feed->entries[0]){
  437. // custom headers not given
  438. // extract column headings
  439. $firstRow = $feed->entries[0]->getCustom ();
  440. // if user did not specify all, get rest from list feed
  441. while ($colHead = $firstRow [$k]){
  442. $colax = $k%2==0 ?'odd' : 'even';
  443. $html .= "<th class='col_{$k} {$colax}'>" . $colHead->getColumnName () . "</th>";
  444. $k++;
  445. }
  446. }
  447. // end <thead>
  448. $html .= "\r\n\t\t</tr>\r\n";
  449. $html .= "\t</thead>\r\n";
  450. }
  451. $html .= "\t<tbody>\r\n";
  452. // for every row
  453. $i = 1;
  454. foreach ($feed->entries as $entry){
  455. $rowlax = $i%2!=0 ?'odd' : 'even';
  456. // start table row
  457. $html .= "\t\t<tr class='row_{$i} {$rowlax}'>\r\n\t\t\t";
  458. // get all the cells in this row
  459. $cells = $entry->getCustom ();
  460. // for every cell, display the contents
  461. $j = 0;
  462. foreach ($cells as $cell){
  463. $colax = $j%2!=0 ?'even' : 'odd';
  464. $html .= "<td class='col_{$j} {$colax}'>" . $cell->getText () . "</td>";
  465. $j++;
  466. }
  467. // end table row
  468. $html .= "\r\n\t\t</tr>\r\n";
  469. $i++;
  470. }
  471. $html .= "\t</tbody>\r\n";
  472. $html .= "</table>\r\n";
  473. return $html;
  474. }
  475. /**
  476. * Formats <span> tag for cell
  477. * @param string $st_id Spreadsheet ID
  478. * @param string $wt_id Worksheet ID
  479. * @param string $cell_id Cell ID
  480. * @param string $style Style classes to include
  481. */
  482. public static function printCellTag ($st_id, $wt_id, $cell_id, $style=NULL){
  483. $classes = preg_split ("/,(\s)?/", $style);
  484. $classes = implode (' ', $classes);
  485. return "<span class='gdocs {$classes}' id='gdocs_{$st_id}_{$wt_id}_{$cell_id}'>";
  486. }
  487. /**
  488. * Formats contents of spreadsheet cell for display
  489. */
  490. public static function printCell (Zend_Gdata_Feed $feed){
  491. if (is_null($feed[0])) throw new Exception ();
  492. $entry = new Zend_Gdata_Spreadsheets_CellEntry ($feed[0]->getDOM());
  493. return $entry->getCell()->getText() . "</span>";
  494. }
  495. /**
  496. * Prints stylesheet <link>
  497. * @param string $style Style classes for this spreadsheet
  498. * @return string $html <style> tag to add to the HTML output
  499. */
  500. public static function printStylesheet ($style){
  501. // split style classes
  502. $classes = preg_split ("/,(\s)?/", $style);
  503. // parse gdocs_style_dir option
  504. $dir = get_option ('gdocs_style_dir');
  505. if ($dir){
  506. if ($dir[strlen ($dir) -1] !== "/") $dir .= "/"; // if no ending slash, add ending slash
  507. if ($dir[0] !== "/") $dir = "/" . $dir; // if no starting slash, add starting slash
  508. }
  509. $html = "";
  510. foreach ($classes as $class){
  511. if (in_array ($class, self::$stylesheets)) continue;
  512. self::$stylesheets[] = $class;
  513. if (file_exists (ABSPATH . $dir . "{$class}.css")){
  514. $path = get_bloginfo ('wpurl') . $dir . "{$class}.css";
  515. $html .= "<link href='{$path}' rel='stylesheet' type='text/css' />\r\n";
  516. }else if (file_exists (ABSPATH . $dir . "{$class}/{$class}.css")) {
  517. $path = get_bloginfo ('wpurl') . $dir . "{$class}/{$class}.css";
  518. $html .= "<link href='{$path}' rel='stylesheet' type='text/css' />\r\n";
  519. }else if (file_exists (GDOCS_PATH . "/styles/{$class}.css")){
  520. $path = GDOCS_ADDRESS . "/styles/{$class}.css";
  521. $html .= "<link href='{$path}' rel='stylesheet' type='text/css' />\r\n";
  522. }else if (file_exists (GDOCS_PATH . "/styles/{$class}/{$class}.css")) {
  523. $path = GDOCS_ADDRESS . "/styles/{$class}/{$class}.css";
  524. $html .= "<link href='{$path}' rel='stylesheet' type='text/css' />\r\n";
  525. }
  526. }
  527. return $html;
  528. }
  529. /**#@-*/
  530. }
  531. ?>