PageRenderTime 356ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/util/Util.php

https://github.com/nateabele/Minerva-Plugin
PHP | 169 lines | 110 code | 12 blank | 47 comment | 16 complexity | 8b4e97032f88bd4f88c1f8ff8e0cfb4f MD5 | raw file
  1. <?php
  2. /*
  3. * A general utility class for Minerva. This includes several useful methods used throughout the site.
  4. */
  5. namespace util;
  6. use \RecursiveIteratorIterator;
  7. use \RecursiveArrayIterator;
  8. use lithium\core\Libraries;
  9. use lithium\util\Set;
  10. class Util {
  11. /*
  12. * in_array recursive function using Spl libraries. Quite useful.
  13. */
  14. public function in_array_recursive($needle=null, $haystack=null) {
  15. if((empty($needle)) || (empty($haystack))) {
  16. return false;
  17. }
  18. $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));
  19. foreach($it AS $element) {
  20. if($element === $needle) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. /*
  27. * A simple method to return a unique string, useful for approval codes and such.
  28. * An md5 hash of the unique id will be 32 characters long and the sha1 will be 40 characters long.
  29. * Without hashing, the unique id will be 13 characters long and 23 long if more entropy is used.
  30. *
  31. * @params $options Array
  32. * - hash: The hash method to use to hash the uid, md5, sha1, or false (default is md5)
  33. * - prefix: The prefix to use for uniqid() method
  34. * - entropy: Boolean, whether or not to add additional entropy (more unique)
  35. */
  36. public function unique_string($options=array()) {
  37. $options += array('hash' => 'md5', 'prefix' => '', 'entropy' => false);
  38. switch($options['hash']) {
  39. case 'md5':
  40. return md5(uniqid($options['prefix'], $options['entropy']));
  41. default:
  42. break;
  43. case 'sha1':
  44. return sha1(uniqid($options['prefix'], $options['entropy']));
  45. break;
  46. case false:
  47. return uniqid($options['prefix'], $options['entropy']);
  48. break;
  49. }
  50. }
  51. /**
  52. * Generate a unique pretty url for the model's record.
  53. *
  54. * @params $options Array
  55. * - url: The requested url (typically the inflector::slug() for a title)
  56. * - id: The current id (optional, only if editing a record, so it knows to exclude itself as a conflict)
  57. * - model: The model that's used as the lookup (to run the find() on)
  58. * - separator: The optional separator symbol for spaces (default: -)
  59. * @return String The unique pretty url.
  60. */
  61. public function unique_url($options=array()) {
  62. $options += array('url' => null, 'id' => null, 'model' => null, 'separator' => '-');
  63. if((!$options['url']) || (!$options['model'])) {
  64. return null;
  65. }
  66. $records = $options['model']::find('all', array('fields' => array('url'), 'conditions' => array('url' => array('like' => '/'.$options['url'].'/'))));
  67. $conflicts = array();
  68. foreach($records as $record) {
  69. // If the record id is an object, it's probably a MongoId, so make it a string to compare IF the passed id was not an object too.
  70. if((is_object($record->{$options['model']::key()})) && (!is_object($options['id']))) {
  71. $record_id = (string)$record->{$options['model']::key()};
  72. } else {
  73. $record_id = $record->{$options['model']::key()};
  74. }
  75. if($record_id != $options['id']) {
  76. $conflicts[] = $record->url;
  77. }
  78. }
  79. if (!empty($conflicts)) {
  80. $firstSlug = $options['url'];
  81. $i = 1;
  82. while($i > 0) {
  83. if (!in_array($firstSlug . $options['separator'] . $i, $conflicts)) {
  84. $options['url'] = $firstSlug . $options['separator'] . $i;
  85. $i = -1;
  86. }
  87. $i++;
  88. }
  89. }
  90. return strtolower($options['url']);
  91. }
  92. /**
  93. * Returns an array of all libraries that have models in order to provide
  94. * a list for page types, block types, or user types. The list is returned
  95. * in alphabetical order.
  96. *
  97. * @param $model String The model
  98. * @param $options Array Various options
  99. * - exclude_minerva Boolean Excludes the minerva\models classes
  100. * - exclude Array Other class paths to exclude
  101. * @return Array All of the class paths to the types for that model
  102. */
  103. public function list_types($model='Page', $options=array()) {
  104. $options += array('exclude_minerva' => false, 'exclude' => array());
  105. $model = ucfirst($model);
  106. $types = array();
  107. $libraries = Libraries::locate('models');
  108. foreach($libraries as $library) {
  109. if(end(explode('\\', $library)) == $model) {
  110. $types[] = $library;
  111. }
  112. }
  113. if($options['exclude_minerva'] === true) {
  114. $options['exclude'][] = 'minerva\models\\' . $model;
  115. }
  116. if(count($options['exclude']) > 0) {
  117. $i=0;
  118. foreach($types as $type) {
  119. if(in_array($type, $options['exclude'])) {
  120. unset($types[$i]);
  121. }
  122. $i++;
  123. }
  124. }
  125. sort($types);
  126. return $types;
  127. }
  128. /**
  129. * Formats the order array for the find() method's order option.
  130. * By default uses id descending, if invalid, an empty array is returned.
  131. * The order string is passed as dot separated field.direciton.
  132. * ex. created.desc or created.asc
  133. * Also valid: created.DESC or created.descending
  134. *
  135. * @param $order String The dot separated field.direction
  136. */
  137. public function format_dot_order($order='id.desc') {
  138. $order_pieces = explode('.', $order);
  139. if(count($order_pieces) > 1) {
  140. switch(strtolower($order_pieces[1])) {
  141. case 'desc':
  142. case 'descending':
  143. default:
  144. $direction = 'desc';
  145. break;
  146. case 'asc':
  147. case 'ascending':
  148. $direction = 'asc';
  149. break;
  150. }
  151. return array($order_pieces[0], $direction);
  152. }
  153. return array();
  154. }
  155. }
  156. ?>