PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Teamleaf.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 708 lines | 537 code | 98 blank | 73 comment | 42 complexity | b1c4281d3c441215a26e47d7eb9a2ea1 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /*
  3. * Teamleaf
  4. *
  5. * Main conversion lib for Teamleaf 2
  6. *
  7. * @package Teamleaf
  8. * @author Matt Hiscock
  9. * @version 1.0.1
  10. * @license Copyright (c) s3d designs
  11. */
  12. class Teamleaf
  13. {
  14. function create_db_test($majortable, $has_one, $has_many)
  15. {
  16. echo '<hr>';
  17. echo $majortable . ' <br/>';
  18. echo print_r($has_one, true) . ' <br/>';
  19. echo print_r($has_many, true) . ' <br/>';
  20. }
  21. function create_table($majortable, $table)
  22. {
  23. $CI = &get_instance();
  24. $CI -> load -> dbforge();
  25. $newtable = array();
  26. $newtable[0] = $majortable;
  27. $newtable[1] = $table . 's';
  28. sort($newtable);
  29. //print_r($this->has_one);
  30. $newTableName = $newtable[0] . '_' . $newtable[1];
  31. $table_non_plural = substr_replace($majortable, "", -1);
  32. $fields = array(
  33. 'id' => array(
  34. 'type' => 'INT',
  35. 'constraint' => 6,
  36. 'unsigned' => TRUE,
  37. 'auto_increment' => TRUE
  38. ),
  39. '' . $table_non_plural . '_id' => array(
  40. 'type' => 'INT',
  41. 'constraint' => 6,
  42. ),
  43. '' . $table . '_id' => array(
  44. 'type' => 'INT',
  45. 'constraint' => 6,
  46. )
  47. );
  48. $CI -> dbforge -> add_field($fields);
  49. $CI -> dbforge -> add_key('id', TRUE);
  50. $CI -> dbforge -> create_table($newTableName, TRUE);
  51. echo '<div style="padding:5px; padding-top:20px; width:600px; border-bottom: 1px solid #ccc">' . $newTableName . ' Table Created: </div>';
  52. echo '<div style="padding:5px; width:600px; border-bottom: 1px dashed #ccc">';
  53. foreach ($fields as $field => $vals)
  54. {
  55. echo 'Field: ' . $field . ' <br/>';
  56. }
  57. echo '</div>';
  58. }
  59. function create_db($majortable, $has_one, $has_many)
  60. {
  61. $CI = &get_instance();
  62. $CI -> load -> dbforge();
  63. foreach ($has_one as $table => $vals)
  64. {
  65. $this -> create_table($majortable, $table);
  66. }
  67. foreach ($has_many as $table => $vals)
  68. {
  69. $this -> create_table($majortable, $table);
  70. }
  71. }
  72. function shortDate($date, $return = true)
  73. {
  74. if (!$return)
  75. {
  76. echo date("d M, Y", strtotime($date));
  77. }
  78. else
  79. {
  80. return date("d M, Y", strtotime($date));
  81. }
  82. }
  83. function date($date, $return = true)
  84. {
  85. if (!$return)
  86. {
  87. echo date("d M, Y", strtotime($date));
  88. }
  89. else
  90. {
  91. return date("d M, Y", strtotime($date));
  92. }
  93. }
  94. function dateTime($date, $return = true)
  95. {
  96. if (!$return)
  97. {
  98. echo date("d M, Y - H:i", strtotime($date));
  99. }
  100. else
  101. {
  102. return date("d M, Y - H:i", strtotime($date));
  103. }
  104. }
  105. function dateTimeRange($dateFrom, $dateTo)
  106. {
  107. if (date("d M, Y", strtotime($dateFrom)) == date("d M, Y", strtotime($dateTo)))
  108. {
  109. return date("d M, Y - H:i", strtotime($dateFrom)) . ' until ' . date("H:i", strtotime($dateTo));
  110. }
  111. else
  112. {
  113. return date("d M, Y - H:i", strtotime($dateFrom)) . ' until ' . date("d M, Y - H:i", strtotime($dateTo));
  114. }
  115. }
  116. function timeRange($dateFrom, $dateTo)
  117. {
  118. if (date("d M, Y", strtotime($dateFrom)) == date("d M, Y", strtotime($dateTo)))
  119. {
  120. return date("H:i", strtotime($dateFrom)) . ' until ' . date("H:i", strtotime($dateTo));
  121. }
  122. else
  123. {
  124. return date("d M, Y - H:i", strtotime($dateFrom)) . ' until ' . date("d M, Y - H:i", strtotime($dateTo));
  125. }
  126. }
  127. function dateTimeRangeDifference($dateFrom, $dateTo)
  128. {
  129. $date_a = new DateTime($dateFrom);
  130. $date_b = new DateTime($dateTo);
  131. $interval = $date_a -> diff($date_b);
  132. $rt = '';
  133. $days = $interval -> format('%d');
  134. if ($days)
  135. {
  136. $rt .= $interval -> format('%d day');
  137. if ($days > 1)
  138. {
  139. $rt .= 's, ';
  140. }
  141. else
  142. {
  143. $rt .= ', ';
  144. }
  145. }
  146. $hours = $interval -> format('%h');
  147. if ($hours)
  148. {
  149. $rt .= $interval -> format('%h hour');
  150. if ($hours > 1)
  151. {
  152. $rt .= 's ';
  153. }
  154. else
  155. {
  156. $rt .= ', ';
  157. }
  158. }
  159. $mins = $interval -> format('%i');
  160. if ($mins)
  161. {
  162. $rt .= 'and ';
  163. $rt .= $interval -> format('%i minute');
  164. if ($mins > 1)
  165. {
  166. $rt .= 's.';
  167. }
  168. else
  169. {
  170. $rt .= '. ';
  171. }
  172. }
  173. return $rt;
  174. }
  175. function workingCost($dateFrom, $dateTo, $perhourcost)
  176. {
  177. $d1 = strtotime($dateFrom);
  178. $d2 = strtotime($dateTo);
  179. $hours = floor(($d2 - $d1) / 3600);
  180. return $hours * $perhourcost;
  181. }
  182. function checkPath($path)
  183. {
  184. // path is from the UPLOADS define onwards only
  185. $folders = explode('/', $path);
  186. //print_r($folders);
  187. for ($x = 0; $x < count($folders); $x++)
  188. {
  189. // Build path
  190. $path = '';
  191. $count = 0;
  192. while ($count <= $x)
  193. {
  194. if ($count)
  195. {
  196. $path .= '/';
  197. }
  198. $path .= $folders[$count];
  199. $count++;
  200. }
  201. if (!is_dir(UPLOAD_PATH . $path))
  202. {
  203. mkdir(UPLOAD_PATH . $path, 0777);
  204. chmod(UPLOAD_PATH . $path, 0777);
  205. //echo "The directory " . $path . " was successfully created.";
  206. }
  207. else
  208. {
  209. //echo "The directory " . $path . " existed.";
  210. }
  211. }
  212. }
  213. function filelink($file)
  214. {
  215. $extension = end(explode('.', $file -> file_name));
  216. $myPath = explode('uploads/', $file -> full_path);
  217. $markup = anchor_popup(UPLOAD_URL . $myPath[1], '<img class="tl-file" src="' . IMAGE_URL . 'icons/files/' . $extension . '.png" />');
  218. return $markup;
  219. }
  220. function updateDepotAssetRelationships($id = null)
  221. {
  222. // we must relate the asset to the depot(s) when they are under ownership
  223. $today = date("Y-m-d H:i:s");
  224. $depots = $this -> getObject('depot', null, null, false, array('state' => 'Active'), '', '') -> dm_object;
  225. if (!$id)
  226. {
  227. $assets = $this -> getObject('asset', null, null, false, array('state' => 'Active'), '', '') -> dm_object;
  228. }
  229. else
  230. {
  231. $assets = $this -> getObject('asset', $id, null, false, array('state' => 'Active'), '', '') -> dm_object;
  232. }
  233. foreach ($assets as $asset)
  234. {
  235. // clear all relationships for this asset and depot
  236. $asset -> delete($depots -> all);
  237. $assetownership = $this -> getObject('assetownership', $asset -> id, null, false, array(
  238. 'start >' => $today,
  239. 'end < ' => $today,
  240. 'state' => 'Active'
  241. ), '', '') -> dm_object;
  242. if ($assetownership -> result_count())
  243. {
  244. foreach ($assetownership as $ao)
  245. {
  246. $depot = $this -> getObject('depot', $ao -> depot, null, false, array('state' => 'Active'), '', '') -> dm_object;
  247. $asset -> save($depot);
  248. }
  249. }
  250. //Tester to create cache
  251. // get all depots related to this asset. (e.g. current owners.)
  252. $tdepot = $this -> getObject('assets', null, $asset, false, array('state' => 'Active'), '', '') -> dm_object;
  253. }
  254. }
  255. function money($amount)
  256. {
  257. return '&pound;' . $amount;
  258. }
  259. function getContrastColor($color)
  260. {
  261. return (hexdec($color) > 0xffffff / 2) ? '000000' : 'ffffff';
  262. }
  263. function negativeColor($color)
  264. {
  265. //get red, green and blue
  266. $r = substr($color, 0, 2);
  267. $g = substr($color, 2, 2);
  268. $b = substr($color, 4, 2);
  269. //revert them, they are decimal now
  270. $r = 0xff - hexdec($r);
  271. $g = 0xff - hexdec($g);
  272. $b = 0xff - hexdec($b);
  273. //now convert them to hex and return.
  274. return dechex($r) . dechex($g) . dechex($b);
  275. }
  276. /**
  277. * Returns a lowercase string with keywords ordered by occurance in content seperated with comma's
  278. * Use KeywordsGenerator::generateKeywords($string);
  279. *
  280. * @Author Martijn van Nieuwenhoven
  281. * @Email info@axyrmedia.nl
  282. */
  283. /**
  284. * Extract Keywords
  285. * Returns a lowercase string with keywords ordered by occurance in content seperated with comma's
  286. * @var $string
  287. * @var $min_word_char
  288. * @var $keyword_amount
  289. * @var $exclude_words
  290. */
  291. function generateKeywords($string = '', $min_word_char = 4, $keyword_amount = 25)
  292. {
  293. $stopwords = file(base_url() . 'assets/stopwords.txt');
  294. $exclude_words = implode(', ', array_slice($stopwords, 0));
  295. return $this -> calculateKeywords($string, $min_word_char, $keyword_amount, $exclude_words);
  296. }
  297. function calculateKeywords($string = '', $min_word_char = 2, $keyword_amount = 1, $exclude_words = 'the,and, there, take, during, combined, interesting, from, known, looking')
  298. {
  299. $exclude_words = explode(",", $exclude_words);
  300. //add space before br tags so words aren't concatenated when tags are stripped
  301. $string = preg_replace('/\<br(\s*)?\/?\>/i', " ", $string);
  302. // get rid off the htmltags
  303. $string = html_entity_decode(strip_tags($string), ENT_NOQUOTES, 'UTF-8');
  304. // count all words with str_word_count_utf8
  305. $initial_words_array = $this -> str_word_count_utf8($string, 1);
  306. $total_words = sizeof($initial_words_array);
  307. $new_string = $string;
  308. //convert to lower case
  309. $new_string = mb_convert_case($new_string, MB_CASE_LOWER, "UTF-8");
  310. // strip excluded words
  311. foreach ($exclude_words as $filter_word)
  312. {
  313. $new_string = preg_replace("/\b" . $filter_word . "\b/i", "", $new_string);
  314. }
  315. // calculate words again without the excluded words using str_word_count_utf8
  316. $words_array = $this -> str_word_count_utf8($new_string, 1);
  317. $words_array = array_filter($words_array, create_function('$var', 'return (strlen($var) >= ' . $min_word_char . ');'));
  318. //print_r($words_array);
  319. $popularity = array_count_values($words_array);
  320. $unique_words_array = array_unique($words_array);
  321. //echo '<hr>Popularity<hr>';
  322. //print_r($popularity);
  323. array_multisort($popularity, $unique_words_array);
  324. //echo '<hr>$unique_words_array<hr>';
  325. //print_r($unique_words_array);
  326. // create density array
  327. //echo '<hr>Popularity Sorted<hr>';
  328. //print_r($popularity);
  329. $popularity = array_reverse($popularity);
  330. $keys = array();
  331. foreach ($popularity as $key => $value)
  332. {
  333. $keys[] = $key;
  334. }
  335. // glue keywords to string seperated by comma, maximum 15 words
  336. $keystring = implode(', ', array_slice($keys, 0, $keyword_amount));
  337. // return the keywords
  338. return $keystring;
  339. }
  340. /**
  341. * Sort array by count value
  342. */
  343. function cmp($a, $b)
  344. {
  345. return ($a['count'] > $b['count']) ? +1 : -1;
  346. }
  347. /** Word count for UTF8
  348. /* Found in: http://www.php.net/%20str_word_count#85592
  349. /* The original mask contained the apostrophe, not good for Meta keywords:
  350. /* "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}..."
  351. */
  352. function str_word_count_utf8($string, $format = 0)
  353. {
  354. switch ($format)
  355. {
  356. case 1 :
  357. preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}]*/u", $string, $matches);
  358. return $matches[0];
  359. case 2 :
  360. preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}]*/u", $string, $matches, PREG_OFFSET_CAPTURE);
  361. $result = array();
  362. foreach ($matches[0] as $match)
  363. {
  364. $result[$match[1]] = $match[0];
  365. }
  366. return $result;
  367. }
  368. return preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}]*/u", $string, $matches);
  369. }
  370. public function slugify($text)
  371. {
  372. // replace non letter or digits by -
  373. $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  374. // trim
  375. $text = trim($text, '-');
  376. // lowercase
  377. $text = strtolower($text);
  378. // remove unwanted characters
  379. $text = preg_replace('~[^-\w]+~', '', $text);
  380. if (empty($text))
  381. {
  382. return 'n-a';
  383. }
  384. return $text;
  385. }
  386. function isActive($me, $currentURL = '')
  387. {
  388. $CI = &get_instance();
  389. if ($currentURL == '')
  390. {
  391. $currentURL = $CI -> uri -> uri_string();
  392. }
  393. if ($currentURL == $me)
  394. {
  395. return 'active';
  396. }
  397. else
  398. {
  399. return '';
  400. }
  401. }
  402. function link_subdestination($dmobject, $linkedItem, $sitemap = 0)
  403. {
  404. $md = new Destination($dmobject -> parentdestination);
  405. if ($sitemap)
  406. {
  407. return base_url() . 'destinations/' . $this -> slugify($md -> title) . '/' . $this -> slugify($dmobject -> title);
  408. }
  409. else
  410. {
  411. return anchor('destinations/' . $this -> slugify($md -> title) . '/' . $this -> slugify($dmobject -> title), $linkedItem);
  412. }
  413. }
  414. function link_majordestination($dmobject, $linkedItem, $sitemap = 0)
  415. {
  416. if ($sitemap)
  417. {
  418. return base_url() . 'destinations/' . $this -> slugify($dmobject -> title);
  419. }
  420. else
  421. {
  422. return anchor('destinations/' . $this -> slugify($dmobject -> title), $linkedItem);
  423. }
  424. }
  425. function link_hotel($dmobject, $linkedItem, $sitemap = 0)
  426. {
  427. if ($sitemap)
  428. {
  429. return base_url() . 'hotels/viewDetail/' . $dmobject -> id . '/' . $this -> slugify($dmobject -> title);
  430. }
  431. else
  432. {
  433. return anchor('hotels/viewDetail/' . $dmobject -> id . '/' . $this -> slugify($dmobject -> title), $linkedItem);
  434. }
  435. }
  436. function link_person($dmobject, $linkedItem, $sitemap = 0)
  437. {
  438. if ($sitemap)
  439. {
  440. return base_url() . '' . $dmobject -> username;
  441. }
  442. else
  443. {
  444. return anchor($dmobject -> username, $linkedItem);
  445. }
  446. }
  447. function getCacheView($cacheFolder, $cacheFile, $path, $file, $data = null)
  448. {
  449. $CI = &get_instance();
  450. if ($cacheRes = $CI -> maintaincache -> getCache($cacheFolder . '/' . $cacheFile . '.txt'))
  451. {
  452. return $cacheRes;
  453. }
  454. else
  455. {
  456. $cacheRes = $CI -> load -> view($path . '/' . $file, $data, true);
  457. $CI -> maintaincache -> setCache($cacheFolder . '/' . $cacheFile . '.txt', $cacheRes);
  458. return $cacheRes;
  459. }
  460. }
  461. function personNameFull($p, $link = true)
  462. {
  463. if ($p -> id)
  464. {
  465. if ($link)
  466. {
  467. return anchor('admin/persons/viewDetail/' . $p -> id, $p -> firstname . ' ' . $p -> surname);
  468. }
  469. else
  470. {
  471. return $p -> firstname . ' ' . $p -> surname;
  472. }
  473. }
  474. else
  475. {
  476. // no id was found
  477. log_message('error', 'No person ID was passed. Library:teamleaf:personNameFull');
  478. return false;
  479. }
  480. }
  481. function personNameFullFrontend($p, $link = true)
  482. {
  483. if ($p -> id)
  484. {
  485. if ($link)
  486. {
  487. return anchor('travel-expert/'.$this->slugify($p->firstname.' '.$p->surname).'/' . $p -> id, $p -> firstname . ' ' . $p -> surname);
  488. }
  489. else
  490. {
  491. return $p -> firstname . ' ' . $p -> surname;
  492. }
  493. }
  494. else
  495. {
  496. // no id was found
  497. log_message('error', 'No person ID was passed. Library:teamleaf:personNameFull');
  498. return false;
  499. }
  500. }
  501. function personNameShort($p, $link = true)
  502. {
  503. if ($p -> id)
  504. {
  505. if ($link)
  506. {
  507. return anchor('admin/persons/viewDetail/' . $p -> id, $p -> firstname . ' ' . substr($p -> surname, 0, 1));
  508. }
  509. else
  510. {
  511. return $p -> firstname . ' ' . substr($p -> surname, 0, 1);
  512. }
  513. }
  514. else
  515. {
  516. // no id was found
  517. log_message('error', 'No person ID was passed. Library:teamleaf:personNameShort');
  518. return false;
  519. }
  520. }
  521. function personNameShortFrontend($p, $link = true)
  522. {
  523. if ($p -> id)
  524. {
  525. if ($link)
  526. {
  527. return anchor('persons/viewDetail/' . $p -> id, $p -> firstname . ' ' . substr($p -> surname, 0, 1));
  528. }
  529. else
  530. {
  531. return $p -> firstname . ' ' . substr($p -> surname, 0, 1);
  532. }
  533. }
  534. else
  535. {
  536. // no id was found
  537. log_message('error', 'No person ID was passed. Library:teamleaf:personNameShort');
  538. return false;
  539. }
  540. }
  541. function words($str, $length)
  542. {
  543. $str = strip_tags($str);
  544. $str = explode(" ", $str);
  545. return implode(" ", array_slice($str, 0, $length));
  546. }
  547. function getTel($text = true)
  548. {
  549. if($text)
  550. {
  551. return 'Call us now on 0845 123 9482';
  552. }
  553. else
  554. {
  555. return '0845 123 9482';
  556. }
  557. }
  558. function expertFlashSet($expertid)
  559. {
  560. $CI = &get_instance();
  561. $data['travelexpertID'] = $expertid;
  562. $CI->teamleaf->setvars($data);
  563. return;
  564. }
  565. function leadFlashSet($leader)
  566. {
  567. $CI = &get_instance();
  568. $data['leader'] = $leader;
  569. $CI->teamleaf->setvars($data);
  570. return;
  571. }
  572. function leadFlashMemberSet($memberID, $leader)
  573. {
  574. $CI = &get_instance();
  575. $data['leader_memberID'] = $memberID;
  576. $data['leader'] = $leader;
  577. $CI->teamleaf->setvars($data);
  578. return;
  579. }
  580. function getvars()
  581. {
  582. $CI = &get_instance();
  583. $a = unserialize((string)$CI->session->userdata('pagedata_array'));
  584. //$CI->fb->info($a, 'getvars');
  585. return $a;
  586. }
  587. function setvars($array, $overwrite = false)
  588. {
  589. $CI = &get_instance();
  590. $arr = $this->getvars();
  591. if (!$overwrite)
  592. {
  593. $result = array_merge((array)$arr, (array)$array);
  594. foreach ($result as $key => $val)
  595. {
  596. if ($val == '')
  597. {
  598. unset($result[$key]);
  599. }
  600. }
  601. $CI->session->set_userdata('pagedata_array', serialize($result));
  602. //$CI->fb->info($result, 'setvars');
  603. }
  604. else
  605. {
  606. $CI->session->set_userdata('pagedata_array', serialize($array));
  607. // $CI->fb->info($array, 'setvars');
  608. }
  609. }
  610. }