PageRenderTime 27ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/template_wizard/include/functions.inc.php

https://github.com/tcmiller/Header-Footer-Wizard
PHP | 483 lines | 248 code | 120 blank | 115 comment | 90 complexity | 8ffd4de47185777186ea5ddf9ba0e387 MD5 | raw file
  1. <?php
  2. require_once('tmplgen-db.inc.php');
  3. /**
  4. * curlRequestGenerator - returns a particular CURL resource, based on some inputs
  5. *
  6. * @param string $url
  7. * @param string $type optional
  8. * @return $curl
  9. */
  10. function curlRequestGenerator($url,$type = '') {
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_URL, 'http://depts.washington.edu/uweb/inc/'.$url);
  13. curl_setopt($ch, CURLOPT_HEADER, false);
  14. // we just want the plain text back, not an html preview
  15. if ($type == 'plain') {
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  17. $curl = curl_exec($ch);
  18. return $curl;
  19. } else {
  20. curl_exec($ch);
  21. curl_close($ch);
  22. }
  23. }
  24. /**
  25. * accountLookup() - Look up just the owner info
  26. *
  27. * @todo - Look up any current info we might have for the logged in user
  28. *
  29. * @return array $accountData
  30. */
  31. function accountLookup() {
  32. global $mdb2;
  33. // this query just retrieves the account owner
  34. $query = sprintf('SELECT owner,
  35. email,
  36. site_url,
  37. active,
  38. code_pref
  39. FROM account
  40. WHERE owner = \'%s\'',$_SERVER['REMOTE_USER']);
  41. // Proceed with getting some data...
  42. $res =& $mdb2->query($query);
  43. // build the data array
  44. while (($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  45. $accountData = $row;
  46. }
  47. return $accountData;
  48. }
  49. function createAccount() {
  50. // call and store the results from accountLookup()
  51. $account = accountLookup();
  52. // make sure the results exist and are in array format
  53. if (!empty($account) && is_array($account)) {
  54. // no need to create an account, one already exists
  55. $accountStartup = '';
  56. } else {
  57. // no account exists, create one
  58. $accountStartup = '$(window).load(function () {
  59. // run code
  60. $.post(\'generate.php\',{ owner: $(\'#owner\').val(),
  61. processType: \'initA\' },function(data) {
  62. $(\'#results\').text(data);
  63. },\'html\');
  64. });';
  65. }
  66. echo $accountStartup;
  67. }
  68. /**
  69. * setAccountDefaults() - Look through any returned account data and set if exists
  70. *
  71. *
  72. * @return string $accountDefaults
  73. */
  74. function setAccountDefaults() {
  75. // call and store the results from accountLookup()
  76. $account = accountLookup();
  77. // call our header and footer info as well
  78. $header = headerLookup();
  79. $footer = footerLookup();
  80. // set up $accountDefaults
  81. $accountDefaults = '';
  82. // see if email exists and if so, show it
  83. if (!empty($account['email'])) {
  84. $accountDefaults .= '$(\'#email\').attr(\'value\',\''.$account['email'].'\');';
  85. }
  86. // see if site_url exists and if so, show it
  87. if (!empty($account['site_url'])) {
  88. $accountDefaults .= '$(\'#site_url\').attr(\'value\',\''.$account['site_url'].'\');';
  89. }
  90. // see if code preference exists and if so, show it
  91. if (!empty($account['code_pref'])) {
  92. // look to see if the user had selected the static chtml include output options, and if so, check the include radio button
  93. if ($header['selection'] == 'static' || $footer['static'] == 1) {
  94. $accountDefaults .= '$(\'#include\').attr(\'checked\',\'checked\');';
  95. $accountDefaults .= '$(\'#copy-paste\').attr(\'disabled\',\'disabled\');';
  96. $accountDefaults .= '$(\'#both\').attr(\'disabled\',\'disabled\');';
  97. } else {
  98. $accountDefaults .= '$(\'#'.$account['code_pref'].'\').attr(\'checked\',\'checked\');';
  99. }
  100. }
  101. echo $accountDefaults;
  102. }
  103. /**
  104. * headerLookup() - Look up any header info for the logged in user
  105. *
  106. *
  107. * @return array $headerData
  108. */
  109. function headerLookup() {
  110. global $mdb2;
  111. // this query retrieves header data
  112. $query = sprintf('SELECT hdr.selection,
  113. hdr.blockw,
  114. hdr.patch,
  115. hdr.sesqui,
  116. hdr.sesqui_sink,
  117. hdr.wordmark,
  118. hdr.color,
  119. hdr.search
  120. FROM header as hdr,
  121. account as acct
  122. WHERE acct.owner = \'%s\'
  123. AND hdr.owner = acct.owner',$_SERVER['REMOTE_USER']);
  124. // Proceed with getting some data...
  125. $res =& $mdb2->query($query);
  126. // build the data array
  127. while (($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  128. $headerData = $row;
  129. }
  130. return $headerData;
  131. }
  132. /**
  133. * setHeaderDefaults() - If the header row for this user doesn't exist, set some header option defaults
  134. *
  135. * @return string $headerDefaults
  136. */
  137. function setHeaderDefaults() {
  138. // call and store the results from headerLookup()
  139. $header = headerLookup();
  140. $headerDefaults = '';
  141. // make sure the results exist and are in array format
  142. if (!empty($header) && is_array($header)) {
  143. // header record exists, return current user's selections
  144. // selection lookups, primarily for block display
  145. if ($header['selection'] == 'strip') {
  146. $headerDefaults .= '$(\'#step2_sub\').css(\'display\',\'block\');';
  147. $headerDefaults .= '$(\'#step2_sink\').css(\'display\',\'none\');';
  148. }
  149. if ($header['selection'] == 'sink') {
  150. $headerDefaults .= '$(\'#step2_sub\').css(\'display\',\'none\');';
  151. $headerDefaults .= '$(\'#step2_sink\').css(\'display\',\'block\');';
  152. }
  153. // search lookups
  154. if ($header['search'] == 'basic') {
  155. $search = 's_basic';
  156. } else {
  157. $search = 's_no';
  158. }
  159. // patch lookups
  160. if ($header['patch'] == '1') {
  161. $patch = 'patch';
  162. $display = 'none';
  163. } else {
  164. $patch = 'no_patch';
  165. $display = 'block';
  166. }
  167. // sesqui lookups
  168. if ($header['sesqui'] == '1') {
  169. $sesqui = 'sesqui';
  170. $display = 'none';
  171. } else {
  172. $sesqui = 'no_sesqui';
  173. $display = 'block';
  174. }
  175. // sesqui sink lookups
  176. if ($header['sesqui_sink'] == '1') {
  177. $sesqui_sink = 'sesqui_sink';
  178. } else {
  179. $sesqui_sink = 'no_sesqui_sink';
  180. }
  181. // blockw lookups
  182. if ($header['blockw'] == '1') {
  183. $blockw = 'w_yes';
  184. } else {
  185. $blockw = 'w_no';
  186. }
  187. // build string
  188. $headerDefaults .= '$(\'#'.$header['selection'].'\').attr(\'checked\',\'checked\');';
  189. $headerDefaults .= '$(\'#'.$header['color'].'_bg\').attr(\'checked\',\'checked\');';
  190. $headerDefaults .= '$(\'#'.$search.'\').attr(\'checked\',\'checked\');';
  191. $headerDefaults .= '$(\'#'.$patch.'\').attr(\'checked\',\'checked\');';
  192. $headerDefaults .= '$(\'#'.$sesqui.'\').attr(\'checked\',\'checked\');';
  193. $headerDefaults .= '$(\'#'.$sesqui_sink.'\').attr(\'checked\',\'checked\');';
  194. $headerDefaults .= '$(\'#'.$blockw.'\').attr(\'checked\',\'checked\');';
  195. $headerDefaults .= '$(\'#blockwBlk\').css(\'display\',\''.$display.'\');';
  196. } else {
  197. // no header selections exist, set some defaults
  198. $headerDefaults .= '$(\'#gold_bg\').attr(\'checked\',\'checked\');
  199. $(\'#patch\').attr(\'checked\',\'checked\');
  200. $(\'#no_sesqui\').attr(\'checked\',\'checked\');
  201. $(\'#no_sesqui_sink\').attr(\'checked\',\'checked\');
  202. $(\'#w_yes\').attr(\'checked\',\'checked\');
  203. $(\'#s_basic\').attr(\'checked\',\'checked\');';
  204. $headerDefaults .= '$(\'#blockwBlk\').css(\'display\',\'none\');';
  205. }
  206. echo $headerDefaults;
  207. }
  208. /**
  209. * footerLookup() - Look up any footer info for the logged in user
  210. *
  211. *
  212. * @return array $footerData
  213. */
  214. function footerLookup() {
  215. global $mdb2;
  216. // this query just retrieves the color (a single piece of data to tell us if there's a row)
  217. $query = sprintf('SELECT ftr.selected,
  218. ftr.blockw,
  219. ftr.wordmark,
  220. ftr.patch,
  221. ftr.sesqui,
  222. ftr.static
  223. FROM footer as ftr,
  224. account as acct
  225. WHERE acct.owner = \'%s\'
  226. AND ftr.owner = acct.owner',$_SERVER['REMOTE_USER']);
  227. // Proceed with getting some data...
  228. $res =& $mdb2->query($query);
  229. // build the data array
  230. while (($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  231. $footerData = $row;
  232. }
  233. return $footerData;
  234. }
  235. /**
  236. * setFooterDefault
  237. *
  238. * @return string $footerDefault
  239. */
  240. function setFooterDefault() {
  241. $footer = footerLookup();
  242. $footerDefault = '';
  243. // make sure the results exist and are in array format
  244. if (!empty($footer) && is_array($footer)) {
  245. // we need an interpreter to handle the footer data
  246. if ($footer['selected'] == 0) {
  247. // no selection
  248. $footerType = 'ftr_no';
  249. } elseif ($footer['static'] == 1) {
  250. // static selection
  251. $footerType = 'ftr_static';
  252. } elseif ($footer['blockw'] == 0 && $footer['wordmark'] == 0) {
  253. // is it the purple 150th patch?
  254. if ($footer['patch'] == 'purple' && $footer['sesqui'] == 1) {
  255. // must be purple patch sesqui
  256. $footerType = 'ftr_purple_patch_sesqui';
  257. } elseif ($footer['patch'] == 'gold' && $footer['sesqui'] == 1) {
  258. // must be gold sesqui patch
  259. $footerType = 'ftr_gold_patch_sesqui';
  260. } elseif ($footer['patch'] == 'purple') {
  261. // must be purple patch
  262. $footerType = 'ftr_purple_patch';
  263. } else {
  264. // must be the gold patch
  265. $footerType = 'ftr_gold_patch';
  266. }
  267. // is it with the block "W"?
  268. } elseif ($footer['blockw'] == 1 && $footer['wordmark'] == 1) {
  269. $footerType = 'ftr_w';
  270. } else {
  271. // must be the basic footer
  272. $footerType = 'ftr_basic';
  273. }
  274. $footerDefault = '$(\'#'.$footerType.'\').attr(\'checked\',\'checked\');';
  275. } else {
  276. // only if we want a default (when they first enter the form)
  277. //$footerDefault = '$(\'#ftr_basic\').attr(\'checked\',\'checked\');';
  278. }
  279. echo $footerDefault;
  280. }
  281. /**
  282. * loadHdrPrvw() - Dynamically load a preview of the user's header
  283. *
  284. */
  285. function loadHdrPrvw() {
  286. $hdrPrvw = '';
  287. // see if a header exists for this user
  288. $header = headerLookup();
  289. // make sure the results exist and are in array format
  290. if (!empty($header) && ($header['selection'] == 'strip' || $header['selection'] == 'sink')) {
  291. $hdrPrvw = curlRequestGenerator('header.cgi?i='.$_SERVER['REMOTE_USER'].'&amp;c=0','plain');
  292. } elseif (!empty($header) && $header['selection'] == 'static') {
  293. $hdrPrvw = '<div class="no-selection-msg">chtml header include selected: Currently no preview available</div>';
  294. } else {
  295. $hdrPrvw = '<div class="no-selection-msg">No header selection</div>';
  296. }
  297. echo $hdrPrvw;
  298. }
  299. /**
  300. * loadFtrPrvw() - Dynamically load a preview of the user's footer
  301. *
  302. */
  303. function loadFtrPrvw() {
  304. $ftrPrvw = '';
  305. // see if a footer exists for this user
  306. $footer = footerLookup();
  307. // make sure the results exist and are in array format
  308. if (!empty($footer) && $footer['selected'] == '1' && $footer['static'] == 0) {
  309. $ftrPrvw = curlRequestGenerator('footer.cgi?i='.$_SERVER['REMOTE_USER'].'&amp;c=0','plain');
  310. } elseif (!empty($footer) && $footer['static'] == '1') {
  311. $ftrPrvw = '<div class="no-selection-msg">chtml footer include selected: Currently no preview available</div>';
  312. } else {
  313. $ftrPrvw = '<div class="no-selection-msg">No footer selection</div>';
  314. }
  315. echo $ftrPrvw;
  316. }
  317. /**
  318. * setStyleDefaults() - This sets some style defaults for the page, based on certain user info we know
  319. *
  320. */
  321. function setStyleDefaults() {
  322. $styleDefaults = '';
  323. // look for a header or a footer
  324. $header = headerLookup();
  325. $footer = footerLookup();
  326. // provide the appropriate stylesheet, based on the user's header selection
  327. if (!empty($header) && $header['selection'] == 'sink') {
  328. $styleDefaults .= '$(\'head\').append(\'<link rel="stylesheet" href="../inc/css/header-full.css" type="text/css" title="header-styles" />\');';
  329. } else {
  330. $styleDefaults .= '$(\'head\').append(\'<link rel="stylesheet" href="../inc/css/header.css" type="text/css" title="header-styles" />\');';
  331. }
  332. // does a user have a header or footer selected in the system?
  333. if ((!empty($header) && ($header['selection'] == 'strip' || $header['selection'] == 'sink')) || (!empty($footer) && $footer['selected'] == '1')) {
  334. $styleDefaults .= '$(\'#bodyTxt\').css(\'display\',\'block\');';
  335. } else {
  336. $styleDefaults .= '$(\'#bodyTxt\').css(\'display\',\'none\');';
  337. }
  338. $styleDefaults .= '$(\'.unavailable\').css(\'cursor\',\'arrow\').click(function(){
  339. return false;
  340. });';
  341. $styleDefaults .= '$(\'.available\').css(\'cursor\',\'arrow\').click(function(){
  342. return false;
  343. });';
  344. echo $styleDefaults;
  345. }
  346. /**
  347. * createAndSetAppDefaults() - This function just sets things in motion
  348. *
  349. */
  350. function createAndSetAppDefaults() {
  351. // set some dynamic styles
  352. setStyleDefaults();
  353. // create the account if it doesn't already exist
  354. createAccount();
  355. // set some account defaults
  356. setAccountDefaults();
  357. // set some header defaults
  358. setHeaderDefaults();
  359. // set a footer default
  360. setFooterDefault();
  361. }
  362. ?>