-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectIterator.php
More file actions
executable file
·399 lines (357 loc) · 14.5 KB
/
ProjectIterator.php
File metadata and controls
executable file
·399 lines (357 loc) · 14.5 KB
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
<?php namespace Directree;
/**
* @access Public
* @example called from default.php
* @author Keshav Mohta <xkeshav@gmail.com>
* @copyright 2014-01-13
* @name ProjectIterator.php
* @todo divide further functions and optimize the code
* @description
* This class is useful to find data from multi leveled directory system and interate over folders using SPL function .Suppose you require to find latest thumb of latest created folder from diffrent date folder of a location
*
* Files are saved in
* error_reporting(E_ALL ^ E_NOTICE);
* @date Dec 19, 2014
* $loc = "/opt/lampp/archive/data";
* $obj_latest = new latest();
* $thumbList = $obj_latest->latestThumb($loc);
*/
use DirectoryIterator;
use RecursiveDirectoryIterator;
use FilesystemIterator;
class ProjectIterator
{
public static $thumb_folder = 'thumbnails';
private $basepath;
private $flags;
protected $iterator;
protected $screens;
protected $screenFolder;
protected $thumbList;
protected $latestThumbList = [];
// all screen latest thumb list
protected $recentThumb = [];
// [thumb name , thumb location]
protected $dayWithTime = [];
public function __construct()
{
// var_dump(__FILE__);
// var_dump($base);
// echo basename(dirname(__DIR__));
$this->iterator = new DirectoryIterator(dirname(__FILE__));
// echo $this->iterator->getPath();
$this->basepath = $this->iterator->getPath();
// self::$thumb_folder = 'thumbnails';
$this->flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::NEW_CURRENT_AND_KEY | FilesystemIterator::KEY_AS_FILENAME;
}
/** get camera list of a project */
public function getProjectScreenList($projectDir)
{
$dir = (func_num_args()) ? $projectDir : $this->basepath;
$RealPath = realpath($dir);
// d($RealPath);
$Directory = new RecursiveDirectoryIterator($RealPath, $this->flags);
// echo "<pre>"; print_r($Directory); echo "</pre>";
// self::pre($Directory);
// d($Directory);
// +d($Directory->__toString());
// $Dir_ = iterator_to_array($Directory);
// ksort($Dir_); // NOTE: Most Important step
// get all folder (screen) name of project
$this->screens = $this->getAllFolder($Directory);
// d($this->screens);
return $this->screens;
}
/*
Here we first find all parent directory (project) name of given dir
and then check into each directory and find latest thumb of respective directory
hwre we call `findLatest()` method to find thumbnail
@param string $base_dir Physical location of projecr directory
@param string $camera camera name of given folder
@return associative array contains most recent thumbnail name and it's path under key of folder name
when $camera is not FALSE then
@return $this->screenFolder multidimensional associative array with `camera` and `thumbnails` key
eg for $camera = s5 it will return
$this->screenFolder array (2)
's5' => array (4)
'201409' => object SplFileInfo (0)
'201410' => object SplFileInfo (0)
'201411' => object SplFileInfo (0)
'201412' => object SplFileInfo (0)
'thumbnails' => array (8)
's0520141201111928_tn.jpg' => object SplFileInfo (0)
's0520141202110748_tn.jpg' => object SplFileInfo (0)
's0520141203111703_tn.jpg' => object SplFileInfo (0)
's0520141204110515_tn.jpg' => object SplFileInfo (0)
's0520141205111515_tn.jpg' => object SplFileInfo (0)
's0520141206110835_tn.jpg' => object SplFileInfo (0)
's0520141206164753_tn.jpg' => object SplFileInfo (0)
's0520141208110525_tn.jpg' => object SplFileInfo (0)
*/
public function getThumb($givenDir = false, $givenCamera = false, $givenDate = false)
{
// $args = func_get_args();
// d($args);
// $dir = (func_num_args()) ? $givenDir : $this->basepath;
// $RealPath = realpath($dir);
// // d($RealPath);
// $Directory = new RecursiveDirectoryIterator($RealPath, $this->flags);
// // echo "<pre>"; print_r($Directory); echo "</pre>";
// // self::pre($Directory);
// // d($Directory);
// // +d($Directory->__toString());
// // $Dir_ = iterator_to_array($Directory);
// // ksort($Dir_); // NOTE: Most Important step
// // get all folder (screen) name of project
// $this->screens = $this->getAllFolder($Directory);
$this->getProjectScreenList($givenDir);
if (!empty($givenCamera)) {
// d($this->screens);
// echo "<br/>Inside if";
$cameraExist = array_key_exists($givenCamera, $this->screens);
// d($cameraExist);
if ($cameraExist) {
// d($this->screenFolder);
//calling this function will save all folder name in a folder $this->screenFolder
// print("<br/>now we have 2 arguments");
$this->findLatest($this->screens[$givenCamera], $cameraExist);
// d($this->screenFolder);
if ($givenDate) {
// d($this->screenFolder);
// Now we will find whether the given date folder exist on $given screen folder
$dateExist = array_key_exists($givenDate, $this->screenFolder[$givenCamera]);
// d($dateExist);
if ($dateExist) {
// d($this->screenFolder);
// print("<br/>Okay now we have 3 argument");
// d($this->screenFolder[$givenCamera][$givenDate]);
$this->findLatest($this->screenFolder[$givenCamera][$givenDate], $cameraExist, $dateExist);
}
}
}
//return $this->screenFolder;
} else {
// echo "<br/>inside else";
foreach ($this->screens as $kDir => $vDir) {
// d($kDir,$vDir);
// var_dump($vDir->current()->isDir()); // FATAL ERROR
if ($vDir->isDir()) {
$this->findLatest($vDir);
$this->latestThumbList[$kDir] = $this->recentThumb;
}
}
// d($this->latestThumbList);
//return $this->latestThumbList;
}
}
/*
This function call recursively to get latest directoty each time till we get thumbnails folder
and when we get thumbnails folder we will find the latest thumb of that folder and it's path
@param object(SplFileInfo) $dir contains the directory name which we are scanning
@param string $screen camera name ( folder name )
@return array [thumbnail name, thumbnail path]
*/
private function findLatest($dir, $screen = false, $samay = false)
{
$farg = func_get_args();
// d($farg);
// d($dir);
$currentDirName = $dir->getFileName();
$currentDirPath = $dir->getRealPath();
$Directory = new RecursiveDirectoryIterator($currentDirPath, $this->flags);
// d($currentDirName,$currentDirPath,$Directory);
// d($Dir_);
// echo __LINE__."<BR/>BEFORE IF";
// d($this->screenFolder);
if ($screen) {
// echo "<br/>Inside IF : the required screen:$currentDirName";
$this->screenFolder[$currentDirName] = $this->getAllFolder($Directory);
// d($this->screenFolder);
if ($samay) {
// echo "<br/>inside samaya";
$thumbDir = $this->screenFolder[$currentDirName][self::$thumb_folder];
$this->thumbList = $this->getAllThumb($thumbDir);
//d($this->thumbList);
// you can either comment this line
$this->screenFolder[$currentDirName] = $this->thumbList;
}
// d($this->screenFolder);
}
// echo "<br/>outside of if";
// d($this->screenFolder);
// array all thumbnails
// if($currentDirName == self::$thumb_folder ) {
// $this->screenFolder[$currentDirName] = $this->getAllThumb($Directory);
// }
// echo __LINE__."<BR/>NEXT TO IF";
/*
we can not sort on $Directory as this is RecursiveDirectoryIterator Object
so first we changed object into array and the resultant default array is namewise ASC odrer so that latest created folder comes on last entry of array this is useful in this project because folder are genarted dynamically in below format
<foldername><num><YYYY><MM><DD><HH><MM><SS>_thumb.jpg
*/
$Dir_ = iterator_to_array($Directory);
ksort($Dir_);
// check whether we reached on final folder which have "thumbnails" folder inside
//IF we reach on the desired folder than we will iterate one more time inside thumbnail folder
$thumbFolderExist = array_key_exists(self::$thumb_folder, $Dir_);
// d($thumbFolderExist);
if ($thumbFolderExist) {
// echo "Towards the thumbnails";
$thumb = $this->getAllFolder($Directory);
// d($thumb);
return $this->findLatest($thumb[self::$thumb_folder]);
}
// ELSE get the last entry of sorted array which will be recent folder
// d($Dir_);
$dir_latest = array_pop($Dir_);
// echo __LINE__;
// d($dir_latest);
/* check whether it is file or Directory */
// var_dump("Parent Dierctory:".$currentDirName);
// self::pre($currentDirName);
// echo __LINE__;
// now we recahed inside the thumb folder
if ($currentDirName === self::$thumb_folder) {
// echo "<br/>YES Thumbnails";
// d(func_get_args());
// $this->screenFolder[$currentDirName] = $this->getAlThumb($Directory);
// d($this->screenFolder);
// d($dir_latest->getPath());
// d($dir_latest->getPathName());
// d($dir_latest);
$t_name = $dir_latest->getBaseName();
// $t_path = $dir_latest->getRealPath();
$t_path = $dir_latest->getPath(); // changed Jan 07 for similarity concern while output
// !d($t_name,$t_path);
$isFile = $dir_latest->isFile();
$isReadable = $dir_latest->isReadable();
// d($isFile , $isReadable);
if ($isFile && $isReadable) {
//save into protected variable
$this->recentThumb = ['thumb' => $t_name, 'path' => $t_path];
//return [$t_name,$t_path];
}
} else {
// echo "<br/>inside final else";
return $this->findLatest($dir_latest);
}
}
/*Get all folder name of a directory
@param object RecursiveDirectoryIterator $mainDir
@return array of object SplFileInfo in namewsie ASC order
*/
private function getAllFolder($currentDir)
{
// d($mainDir);
$subFolder = [];
foreach ($currentDir as $sub) {
if ($currentDir->current()->isDir()) { // << only if isDir()
$folderName = $currentDir->current()->getBaseName();
$subFolder[$folderName] = $currentDir->current();
}
}
// d($subFolder);
// $Dir_ = iterator_to_array($subFolder);
ksort($subFolder);
return $subFolder;
}
/* Get all thumb images inside thumbnail folder
@param object RecursiveDirectoryIterator $thumbDir
*/
private function hasThumbnailFolder($t_dir)
{
// d($t_dir['thumbnails']);
if ($t_dir['thumbnails']->current()->getBaseName() === 'thumbnails') {
d($t_dir->current());
return $t_dir->current();
//$this-> getAllThumb($t_dir->current());
}
}
private function getAllThumb($thumbDir)
{
// d($thumbDir);
$thumbDirIterator = new DirectoryIterator($thumbDir);
// Directory Iterator
// d($thumbDir_);
$thumbnails = [];
foreach ($thumbDirIterator as $thumb) {
if ($thumbDirIterator->current()->isFile()) {
// << only if isFile()
$thumbName = $thumbDirIterator->current()->getBaseName();
$thumbnails[$thumbName] = $thumbDirIterator->current();
}
}
// d($thumbnails);
ksort($thumbnails);
// d($thumbnails);
return $thumbnails;
}
/*
$is = s0320140801121500_tn.jpg";
@return ["01"=> "12:15:00", "02"=>"12:10:10", ...];
*/
public function getDayTime($is_a)
{
// d($is_a);
$ts = substr($is_a, 9, -7); // get 01121500
$as = str_split($ts, 2); //
$day = array_shift($as); // 01
$waqt = implode(':', $as);
// d($f,$waqt);
if (!isset($this->dayWithTime[$day])) {
$this->dayWithTime[$day] = [];
}
$this->dayWithTime[$day][] = $waqt;
}
// $ta ["01"=> "12:15:00"];
public function getTimeSlots($ts)
{
// $ta_ = array_map($this->getTime, $tk);
// d($ts);
$ta_ = array_walk($ts, [$this, 'getDayTime']);
d($ts);
d($this->dayWithTime);
d($ta_);
// $timeslot = [];
// foreach ($ta_ as $item) {
// $day = key($item);
// $waqt = current($item);
// if(!isset($timeslot[$day])) {
// $timeslot[$day] = array();
// }
// $timeslot[$day][] = $waqt;
// }
// return $timeslot;
}
// return recentThumb array with thumb name and thumb location
public function getRecentThumb()
{
return $this->recentThumb;
}
// return all screens latest thumbnail List
public function getLatestThumbList()
{
return $this->latestThumbList;
}
public function getScreen()
{
return $this->screens;
}
// return screen folder name in associative array and if date is given then it will comes as second array
public function getScreenFolder()
{
return $this->screenFolder;
}
// get all screen thumb of a specific date and screen
public function getScreenThumb()
{
return $this->thumbList;
}
//@param array $input
//@return array woth key name is day and value is time in hh:mm:ss format
public function getDayTimeList($input)
{
array_walk($input, [$this, 'getDayTime']);
return $this->dayWithTime;
}
}