This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUberTools.php
1195 lines (920 loc) · 34 KB
/
UberTools.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace UberGallery;
/**
* UberTools is a PHP library for the UberGallery application.
*
* This software is distributed under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*
* More info available at http://www.ubergallery.net
*
* @author Chris Kankiewicz <[email protected]>
* @copyright Copyright (c) 2013 Chris Kankiewicz (http://www.chriskankiewicz.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link https://github.com/UberGallery/UberGallery Cannonical source URL
*/
class UberTools {
// Define application version
const VERSION = '0.1.3-dev';
// Reserve some variables
protected $_config = array();
protected $_imgDir = NULL;
protected $_appDir = NULL;
protected $_index = NULL;
protected $_rImgDir = NULL;
protected $_now = NULL;
/**
* UberTools construct function. Runs on object creation.
*/
public function __construct() {
// Define the OS specific directory separator
if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
// Get timestamp for the current time
$this->_now = time();
// Set class directory constant
if(!defined('__DIR__')) {
define('__DIR__', dirname(__FILE__));
}
// Set application directory
$this->_appDir = __DIR__;
// Check if temp dir is writable
if(!is_writable(sys_get_temp_dir())) {
throw new Exception('Temp directory not writeable');
}
// Set cache dir to system temp dir
$this->_cacheDir = sys_get_temp_dir();
}
/**
* Special init method for simple one-line interface
*
* @return reflection
* @access public
*/
public static function init() {
$reflection = new ReflectionClass(__CLASS__);
return $reflection->newInstanceArgs(func_get_args());
}
/**
* Apply configuration options
*
* @param aray $config Array of config options
* @return object Self
* @access public
*/
public function loadConfig($config) {
$this->setThumbnailDirectory($config['thumbnail_dir']);
$this->setThumbnailSize($config['thumbnail_width'], $config['thumbnail_height']);
$this->setThumbnailQuality($config['thumbnail_quality']);
$this->setTheme($config['theme']);
$this->setPaginatorThreshold($config['paginator_threshold']);
$this->setSortMethod($config['images_sort_by'], $config['reverse_sort']);
$this->setCacheExpiration($config['cache_expiration']);
if ($config['enable_pagination']) {
$this->setImagesPerPage($config['images_per_page']);
} else {
$this->setImagesPerPage(0);
}
return $this;
}
/**
* Returns pre-formatted XHTML of a gallery
*
* @param string $directory Relative path to images directory
* @param string $relText Text to use as the rel value
* @return object Self
* @access public
*/
public function createGallery($directory, $relText = 'colorbox') {
// Get the gallery data array and set the template path
$galleryArray = $this->readImageDirectory($directory);
$templatePath = $this->_appDir . '/templates/defaultGallery.php';
// Set the relative text attribute
$galleryArray['relText'] = $relText;
// Echo the template contents
echo $this->readTemplate($templatePath, $galleryArray);
return $this;
}
/**
* Returns an array of files and stats of the specified directory
*
* @param string $directory Relative path to images directory
* @return array File listing and statistics for specified directory
* @access public
*/
public function readImageDirectory($directory, $page = 1) {
// Set page number
$this->setPage($page);
// Set relative image directory
$this->setRelativeImageDirectory($directory);
// Instantiate gallery array
$galleryArray = array();
// Get the cached array
$galleryArray = $this->_readIndex($this->_index);
// If cached array is false, read the directory
if (!$galleryArray) {
// Get array of directory
$dirArray = $this->_readDirectory($directory);
// Loop through array and add additional info
foreach ($dirArray as $key => $image) {
// Get files relative path
$relativePath = $this->_rImgDir . '/' . $key;
$galleryArray['images'][htmlentities(pathinfo($image['real_path'], PATHINFO_BASENAME))] = array(
'file_title' => str_replace('_', ' ', pathinfo($image['real_path'], PATHINFO_FILENAME)),
'file_path' => htmlentities($relativePath),
'thumb_path' => $this->_createThumbnail($image['real_path'])
);
}
// Add statistics to gallery array
$galleryArray['stats'] = $this->_readGalleryStats($this->_readDirectory($directory, false));
// Add gallery paginator to the gallery array
$galleryArray['paginator'] = $this->_getPaginatorArray($galleryArray['stats']['current_page'], $galleryArray['stats']['total_pages']);
// Save the sorted array
if ($this->_config['cache_expire'] > 0) {
$this->_createIndex($galleryArray, $this->_index);
}
}
// Return the array
return $galleryArray;
}
/**
* Returns a template string with custom data injected into it
*
* @param string $templatePath Path to template file
* @param array $data Array of data to be injected into the template
* @return string Processed template string
* @access private
*/
public function readTemplate($templatePath, $data) {
// Extract array to variables
extract($data);
// Start the output buffer
ob_start();
// Include the template
include $templatePath;
// Set buffer output to a variable
$output = ob_get_clean();
// Return the output
return $output;
}
/**
* Returns the theme name
*
* @return string Theme name as set in user config
* @access public
*/
public function getTheme() {
// Return the theme name
return $this->_config['theme'];
}
/**
* Get an array of error messages or false when empty
*
* @return array|boolean Array of error messages or boolean false if none
* @access public
*/
public function getSystemMessages() {
if (isset($this->_systemMessage) && is_array($this->_systemMessage)) {
return $this->_systemMessage;
} else {
return false;
}
}
/**
* Returns valid XHTML link tag for chosen ColorBox stylesheet
*
* @param int $themeNum Integer (1-5) representing the ColorBox theme number
* @return string Valid XHTML link tag for chosen ColorBox stylesheet
* @access public
*/
public function getColorboxStyles($themeNum) {
// Get relative path to application dir
$realtivePath = $this->_getRelativePath(getcwd(), $this->_appDir);
// Set ColorBox path
$colorboxPath = $realtivePath . '/colorbox/' . $themeNum . '/colorbox.css';
return '<link rel="stylesheet" type="text/css" href="' . $colorboxPath . '" />';
}
/**
* Returns valid XHTML tags for ColorBox JavaScript include
*
* @return string Valid XHTML tags for ColorBox JavaScript include
* @access public
*/
public function getColorboxScripts() {
// Set some path variables
$templatePath = $this->_appDir . '/templates/colorboxScripts.php';
$colorboxPath = $this->_getRelativePath(getcwd(), $this->_appDir) . '/colorbox/jquery.colorbox.js';
// Get the template contents
$template = $this->readTemplate($templatePath, array('path' => $colorboxPath));
// Return the include text
return $template;
}
/**
* Returns current version of the UberTools library
*
* @return string UberTools version
*/
public function getVersion() {
return self::VERSION;
}
/**
* Set the current page number
*
* @param int $page Page number
* @return object Self
* @access public
*/
public function setPage($page = 1) {
$this->_page = $page;
return $this;
}
/**
* Set the number of images to be displayed per page
*
* @param int $imgPerPage Number of images to display per page (default = 0)
* @return object Self
* @access public
*/
public function setImagesPerPage($imgPerPage = 0) {
$this->_config['img_per_page'] = $imgPerPage;
return $this;
}
/**
* Set the thumbnail directory path
*
* @param string $directory Cache directory path
* @return object Self
* @access public
*/
public function setThumbnailDirectory($directory = 'thumbnails') {
$this->_config['thumbnail_dir'] = realpath($directory);
return $this;
}
/**
* Set thumbnail width and height in pixels
*
* @param int $width Thumbnail width in pixels (default = 100)
* @param int $height Thumbnail height in pixels (default = 100)
* @return object Self
* @access public
*/
public function setThumbnailSize($width = 100, $height = 100) {
$this->_config['thumbnail']['width'] = $width;
$this->_config['thumbnail']['height'] = $height;
return $this;
}
/**
* Set thumbnail quality as a value from 1 - 100
* This only affects JPEGs and has no effect on GIF or PNGs
*
* @param int $quality Thumbnail size in pixels (default = 75)
* @return object Self
* @access public
*/
public function setThumbnailQuality($quality = 75) {
$this->_config['thumbnail']['quality'] = $quality;
return $this;
}
/**
* Set theme
*
* @param string $name Theme name (default = uber-blue)
* @return object Self
* @access public
*/
public function setTheme($name = 'uber-neo') {
$this->_config['theme'] = $name;
return $this;
}
/**
* Set the paginator threshold
*
* @param int $threshold Paginator threshold value (default = 10)
* @return object Self
* @access public
*/
public function setPaginatorThreshold($threshold = 10) {
$this->_config['threshold'] = $threshold;
return $this;
}
/**
* Set the sortting method
*
* @param string $method Sorting method (default = natcasesort)
* @param boolean $reverse true = reverse sort order (default = false)
* @return object Self
* @access public
*/
public function setSortMethod($method = 'natcasesort', $reverse = false) {
$this->_config['sort_method'] = $method;
$this->_config['reverse_sort'] = $reverse;
return $this;
}
/**
* Set cache expiration time in minutes
*
* @param int $time Cache expiration time in minutes (default = 0)
* @return object Self
* @access public
*/
public function setCacheExpiration($time = 0) {
$this->_config['cache_expire'] = $time;
return $this;
}
/**
* Sets the relative path to the image directory
*
* @param string $directory Relative path to image directory
* @return object Self
* @access public
*/
public function setRelativeImageDirectory($directory) {
// Set real path to $directory
$this->_imgDir = realpath($directory);
// Set relative path to $directory
$this->_rImgDir = $directory;
// Set index name
if ($this->_config['img_per_page'] < 1) {
$this->_index = $this->_cacheDir . '/' . sha1($directory) . '-' . 'all.index';
} else {
$this->_index = $this->_cacheDir . '/' . sha1($directory) . '-' . $this->_page . '.index';
}
return $this;
}
/**
* Add a message to the system message array
*
* @param string $type The type of message (ie - error, success, notice, etc.)
* @param string $message The message to be displayed to the user
* @return boolean Returns true on success
* @access public
*/
public function setSystemMessage($type, $text) {
// Create empty message array if it doesn't already exist
if (isset($this->_systemMessage) && !is_array($this->_systemMessage)) {
$this->_systemMessage = array();
}
// Generate unique message key
$key = md5(trim($type . $text));
// Set the error message
$this->_systemMessage[$key] = array(
'type' => $type,
'text' => $text
);
return true;
}
/**
* Reads files in a directory and returns only images
*
* @param string $directory Path to directory
* @param boolean $paginate Whether or not paginate the array (default = true)
* @return array Array of images in the specified directory
* @access private
*/
private function _readDirectory($directory, $paginate = true) {
// Set index path
$index = $this->_cacheDir . '/' . sha1($directory) . '-' . 'files' . '.index';
// Read directory array
$dirArray = $this->_readIndex($index);
// Serve from cache if file exists and caching is enabled
if (!$dirArray) {
// Initialize the array
$dirArray = array();
// Loop through directory and add information to array
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
// Get files real path
$realPath = realpath($directory . '/' . $file);
// If file is an image, add info to array
if ($this->_isImage($realPath)) {
$dirArray[htmlentities(pathinfo($realPath, PATHINFO_BASENAME))] = array(
'real_path' => $realPath
);
}
}
}
// Close open file handle
closedir($handle);
}
// Create directory array
if ($this->_config['cache_expire'] > 0) {
$this->_createIndex($dirArray, $index);
}
}
// Set error message if there are no images
if (empty($dirArray)) {
$this->setSystemMessage('error', "No images found, please upload images to your gallery's image directory.");
}
// Sort the array
$dirArray = $this->_arraySort($dirArray, $this->_config['sort_method'], $this->_config['reverse_sort']);
// Paginate the array and return current page if enabled
if ($paginate == true && $this->_config['img_per_page'] > 0) {
$dirArray = $this->_arrayPaginate($dirArray, $this->_config['img_per_page'], $this->_page);
}
// Return the array
return $dirArray;
}
/**
* Creates a cropped, square thumbnail of given dimensions from a source image
*
* @param string $source Path to source image
* @param int $thumbWidth Desired thumbnail width size in pixels (default = null)
* @param int $thumbHeight Desired thumbnail height size in pixels (default = null)
* @param int $quality Thumbnail quality, from 1 to 100, applies to JPG and JPEGs only (default = null)
* @return string Relative path to thumbnail
* @access private
*/
private function _createThumbnail($source, $thumbWidth = NULL, $thumbHeight = NULL, $quality = NULL) {
// Set defaults thumbnail width if not specified
if ($thumbWidth === NULL) {
$thumbWidth = $this->_config['thumbnail']['width'];
}
// Set defaults thumbnail height if not specified
if ($thumbHeight === NULL) {
$thumbHeight = $this->_config['thumbnail']['height'];
}
// Set defaults thumbnail height if not specified
if ($quality === NULL) {
$quality = $this->_config['thumbnail']['quality'];
}
// MD5 hash of source image path
$fileHash = md5($source);
// Get file extension from source image
$fileExtension = pathinfo($source, PATHINFO_EXTENSION);
// Build file name
$fileName = $thumbWidth . 'x' . $thumbHeight . '-' . $quality . '-' . $fileHash . '.' . $fileExtension;
// Build thumbnail destination path
$destination = $this->_config['thumbnail_dir'] . '/' . $fileName;
// If file is cached return relative path to thumbnail
if ($this->_isFileCached($destination)) {
$relativePath = $this->_getRelativePath(getcwd(), $this->_config['thumbnail_dir']) . '/' . $fileName;
return $relativePath;
}
// Get needed image information
$imgInfo = getimagesize($source);
$width = $imgInfo[0];
$height = $imgInfo[1];
$x = 0;
$y = 0;
// Calculate ratios
$srcRatio = $width / $height;
$thumbRatio = $thumbWidth / $thumbHeight;
if ($srcRatio > $thumbRatio) {
// Preserver original width
$originalWidth = $width;
// Crop image width to proper ratio
$width = $height * $thumbRatio;
// Set thumbnail x offset
$x = ceil(($originalWidth - $width) / 2);
} elseif ($srcRatio < $thumbRatio) {
// Preserver original height
$originalHeight = $height;
// Crop image height to proper ratio
$height = ($width / $thumbRatio);
// Set thumbnail y offset
$y = ceil(($originalHeight - $height) / 2);
}
// Create new empty image of proper dimensions
$newImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
// Create new thumbnail
if ($imgInfo[2] == IMAGETYPE_JPEG) {
$image = imagecreatefromjpeg($source);
imagecopyresampled($newImage, $image, 0, 0, $x, $y, $thumbWidth, $thumbHeight, $width, $height);
imagejpeg($newImage, $destination, $quality);
} elseif ($imgInfo[2] == IMAGETYPE_GIF) {
$image = imagecreatefromgif($source);
imagecopyresampled($newImage, $image, 0, 0, $x, $y, $thumbWidth, $thumbHeight, $width, $height);
imagegif($newImage, $destination);
} elseif ($imgInfo[2] == IMAGETYPE_PNG) {
$image = imagecreatefrompng($source);
imagecopyresampled($newImage, $image, 0, 0, $x, $y, $thumbWidth, $thumbHeight, $width, $height);
imagepng($newImage, $destination);
}
// Return relative path to thumbnail
$relativePath = $this->_getRelativePath(getcwd(), $this->_config['thumbnail_dir']) . '/' . $fileName;
return $relativePath;
}
/**
* Return array from the cached index
*
* @param string $filePath Path to stored index
* @return array|boolean Decoded cached array or false when no valid index is found
* @access private
*/
private function _readIndex($filePath) {
// Return false if file doesn't exist or the cache has expired
if (!$this->_isFileCached($filePath)) {
return false;
}
// Read file index
$indexString = file_get_contents($filePath);
// Unsearialize the array
$indexArray = unserialize($indexString);
// Decode the array
$decodedArray = $this->_arrayDecode($indexArray);
// Return the array
return $decodedArray;
}
/**
* Create serialized index from file array
*
* @param string $array Array to be indexed
* @param string $filePath Path where index will be stored
* @return boolean Returns true on success, false on failure
* @access private
*/
private function _createIndex($array, $filePath) {
// Encode the array
$encodedArray = $this->_arrayEncode($array);
// Serialize array
$serializedArray = serialize($encodedArray);
// Write serialized array to index
if (file_put_contents($filePath, $serializedArray)) {
return true;
}
return false;
}
/**
* Runs all array strings through base64_encode to help
* prevent errors with non-English languages
*
* @param array $array Array to be encoded
* @return array The encoded array
* @access private
*/
private function _arrayEncode($array) {
$encodedArray = array();
foreach ($array as $key => $item) {
// Base64 encode the array keys
$key = base64_encode($key);
// Base64 encode the array values
if (is_array($item)) {
// Recursively call _arrayEncode()
$encodedArray[$key] = $this->_arrayEncode($item);
} elseif (is_string($item)) {
// Base64 encode the string
$encodedArray[$key] = base64_encode($item);
} else {
// Pass value unaltered to new array
$encodedArray[$key] = $item;
}
}
// Return the encoded array
return $encodedArray;
}
/**
* Decodes an encoded array
*
* @param array $array Array to be decoded
* @return array The decoded array
* @access private
*/
private function _arrayDecode($array) {
$decodedArray = array();
foreach ($array as $key => $item) {
// Base64 decode the array keys
$key = base64_decode($key);
// Base64 decode the array values
if (is_array($item)) {
// Recursively call _arrayDecode()
$decodedArray[$key] = $this->_arrayDecode($item);
} elseif (is_string($item)) {
// Base64 decode the string
$decodedArray[$key] = base64_decode($item);
} else {
// Pass value unaltered to new array
$decodedArray[$key] = $item;
}
}
// Return the decoded array
return $decodedArray;
}
/**
* Returns an array of gallery statistics
*
* @param array $array Array to gather stats from
* @return array Array of gallery statistics
* @access private
*/
private function _readGalleryStats($array) {
// Caclulate total array elements
$totalElements = count($array);
// Calculate total pages
if ($this->_config['img_per_page'] > 0) {
$totalPages = ceil($totalElements / $this->_config['img_per_page']);
} else {
$totalPages = 1;
}
// Set current page
if ($this->_page < 1) {
$currentPage = 1;
} elseif ($this->_page > $totalPages) {
$currentPage = $totalPages;
} else {
$currentPage = (integer) $this->_page;
}
// Add stats to array
$statsArray = array(
'current_page' => $currentPage,
'total_images' => $totalElements,
'total_pages' => $totalPages
);
// Return array
return $statsArray;
}
/**
* Returns a formatted array for the gallery paginator
*
* @param int $currentPage The current page being viewed
* @param int $totalPages Total number of pages in the gallery
* @return array Array for building the paginator
* @access private
*/
private function _getPaginatorArray($currentPage, $totalPages) {
// Set some variables
$range = ceil($this->_config['threshold'] / 2) - 1;
$firstPage = $currentPage - $range;
$lastPage = $currentPage + $range;
$firstDiff = NULL;
$lastDiff = NULL;
// Ensure first page is within the bounds of available pages
if ($firstPage <= 1) {
$firstDiff = 1 - $firstPage;
$firstPage = 1;
}
// Ensure last page is within the bounds of available pages
if ($lastPage >= $totalPages) {
$lastDiff = $lastPage - $totalPages;
$lastPage = $totalPages;
}
// Apply page differences
$lastPage = $lastPage + $firstDiff;
$firstPage = $firstPage - $lastDiff;
// Recheck first and last page to ensure they're within proper bounds
if ($firstPage <= 1 && $lastPage >= $totalPages) {
$firstPage = 1;
$lastPage = $totalPages;
}
// Create title element
$paginatorArray[] = array(
'text' => 'Page ' . $currentPage . ' of ' . $totalPages,
'class' => 'title'
);
// Create previous page element
if ($currentPage == 1) {
$paginatorArray[] = array(
'text' => '<',
'class' => 'inactive'
);
} else {
$paginatorArray[] = array(
'text' => '<',
'class' => 'active',
'href' => '?page=' . ($currentPage - 1)
);
}
// Set previous overflow
if ($firstPage > 1) {
$paginatorArray[] = array(
'text' => '...',
'class' => 'more',
'href' => '?page=' . ($currentPage - $range - 1)
);
}
// Generate the page elelments
for ($i = $firstPage; $i <= $lastPage; $i++) {
if ($i == $currentPage) {
$paginatorArray[] = array(
'text' => $i,
'class' => 'current'
);
} else {
$paginatorArray[] = array(
'text' => $i,
'class' => 'active',
'href' => '?page=' . $i
);
}
}
// Set next overflow
if ($lastPage < $totalPages) {
$paginatorArray[] = array(
'text' => '...',
'class' => 'more',
'href' => '?page=' . ($currentPage + $range + 1)
);
}
// Create next page element
if ($currentPage == $totalPages) {
$paginatorArray[] = array(
'text' => '>',
'class' => 'inactive'
);
} else {
$paginatorArray[] = array(
'text' => '>',
'class' => 'active',
'href' => '?page=' . ($currentPage + 1)
);
}
// Return the paginator array
return $paginatorArray;
}
/**
* Sorts an array by the provided sort method
*
* @param array $array Array to be sorted
* @param string $sort Sorting method (acceptable inputs: natsort, natcasesort, etc.)
* @param reverse Reverses the sorted array on true (default = false)
* @return array Sorted array
* @access private
*/
private function _arraySort($array, $sortMethod, $reverse = false) {
// Create empty array
$sortedArray = array();
// Create new array of just the keys and sort it
$keys = array_keys($array);
switch ($sortMethod) {
case 'asort':
asort($keys);
break;
case 'arsort':
arsort($keys);
break;
case 'ksort':
ksort($keys);
break;
case 'krsort':
krsort($keys);
break;
case 'natcasesort':
natcasesort($keys);
break;
case 'natsort':
natsort($keys);
break;
case 'shuffle':
shuffle($keys);
break;
}
// Loop through the sorted values and move over the data
foreach ($keys as $key) {
$sortedArray[$key] = $array[$key];
}
// Reverse array if set
if ($reverse) {
$sortedArray = array_reverse($sortedArray, true);
}
// Return sorted array
return $sortedArray;
}