-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileData_class.php
718 lines (648 loc) · 21.3 KB
/
FileData_class.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
<?php
use Aura\Html\Escaper as e;
/**
FileData_class.php - Builds file data objects
Copyright (C) 2002-2004 Stephen Lawrence Jr., Khoa Nguyen
Copyright (C) 2005-2014 Stephen Lawrence Jr.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
if (!defined('FileData_class')) {
define('FileData_class', 'true', false);
/*
mysql> describe data;
+-------------------+----------------------+------+-----+---------------------+----------------+
| id | smallint(5) unsigned | | PRI | NULL | auto_increment |
| category | tinyint(4) unsigned | | | 0 | |
| owner | tinyint(4) unsigned | | | 0 | |
| realname | varchar(255) | | | | |
| created | datetime | | | 0000-00-00 00:00:00 | |
| description | varchar(255) | YES | | NULL | |
| comment | varchar(255) | | | | |
| status | tinyint(4) unsigned | | | 0 | |
| department | tinyint(4) | | | 0 | |
| default_rights | int(4) | YES | | NULL | |
| publishable | int(4) | YES | | NULL | |
| reviewer | int(4) | YES | | NULL | |
| reviewer_comments | varchar(255) | YES | | NULL | |
+-------------------+----------------------+------+-----+---------------------+----------------+
*/
class FileData extends databaseData
{
public $category;
public $owner;
public $created_date;
public $description;
public $comment;
public $status;
public $department;
public $default_rights;
public $view_users;
public $read_users;
public $write_users;
public $admin_users;
public $filesize;
public $isLocked;
protected $connection;
public function __construct($id, $connection)
{
$this->field_name = 'realname';
$this->field_id = 'id';
$this->result_limit = 1; //EVERY FILE IS LISTED UNIQUELY ON THE DATABASE DATA;
$this->tablename = $this->TABLE_DATA;
$this->connection = $connection;
parent::__construct($id, $connection);
$this->loadData();
}
/**
* Return a boolean whether this file exists
* @return bool|string
*/
public function exists()
{
$query = "
SELECT
*
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->tablename
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
switch ($stmt->rowCount()) {
case 1: return true;
break;
case 0: return false;
break;
default: $this->error = 'Non-unique';
return $this->error;
break;
}
}
/**
* This is a more complex version of base class's loadData.
* This function loads up all the fields in data table
*/
public function loadData()
{
$query = "
SELECT
category,
owner,
created,
description,
comment,
status,
department,
default_rights
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->tablename
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetchAll();
if ($stmt->rowCount() == $this->result_limit) {
foreach ($result as $row) {
$this->category = $row['category'];
$this->owner = $row['owner'];
$this->created_date = $row['created'];
$this->description = stripslashes($row['description']);
$this->comment = stripslashes($row['comment']);
$this->status = $row['status'];
$this->department = $row['department'];
$this->default_rights = $row['default_rights'];
}
} else {
$this->error = 'Non unique file id';
}
$this->isLocked = $this->status == -1;
}
/**
* Update the dynamic values of the file
*/
public function updateData()
{
$query = "
UPDATE
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
SET
category = :category,
owner = :owner,
description = :description,
comment = :comment,
status = :status,
department = :department,
default_rights = :default_rights
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':category' => $this->category,
':owner' => $this->owner,
':description' => $this->description,
':comment' => $this->comment,
':status' => $this->status,
':department' => $this->department,
':default_rights' => $this->default_rights,
':id' => $this->id
));
}
/**
* return filesize
* @return mixed
*/
public function getFileSize()
{
return $this->filesize;
}
/**
* return this file's category id
* @return int
*/
public function getCategory()
{
return $this->category;
}
/**
* @param int $value
*/
public function setCategory($value)
{
$this->category = $value;
}
/**
* return this file's category name
* @return string
*/
public function getCategoryName()
{
$query = "
SELECT
name
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_CATEGORY
WHERE
id = :category_id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':category_id' => $this->category));
$result = $stmt->fetch();
$name = $result['name'];
return $name;
}
/**
* return a boolean on whether the user ID $uid is the owner of this file
* @param int $uid
* @return bool
*/
public function isOwner($uid)
{
return ($this->getOwner() == $uid);
}
/**
* return the ID of the owner of this file
* @return int
*/
public function getOwner()
{
return $this->owner;
}
/**
* set the user_id of the file
* @param int $value
*/
public function setOwner($value)
{
$this->owner = $value;
}
/**
* return the username of the owner
* @return mixed
*/
public function getOwnerName()
{
$user_obj = new User($this->owner, $this->connection);
return $user_obj->getName();
}
/**
* return owner's full name in an array where index=0 corresponds to the last name
* and index=1 corresponds to the first name
* @return mixed
*/
public function getOwnerFullName()
{
$user_obj = new User($this->owner, $this->connection);
return $user_obj->getFullName();
}
/**
* return the owner's dept ID. Often, this is also the department of the file.
* if the owner changes his/her department after he/she changes department, then
* the file's department will not be the same as it's owner's.
* @return string
*/
public function getOwnerDeptId()
{
$user_obj = new User($this->getOwner(), $this->connection);
return $user_obj->getDeptId();
}
/**
* This function serve the same purpose as getOwnerDeptId() except that it returns
* the department name instead of department id
* @return string
*/
public function getOwnerDeptName()
{
$user_obj = new User($this->getOwner(), $this->connection);
return $user_obj->getDeptName();
}
/**
* return file description
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $value
*/
public function setDescription($value)
{
$this->description = $value;
}
/**
* @return int
*/
public function getDefaultRights()
{
return $this->default_rights;
}
/**
* @param int $value
*/
public function setDefaultRights($value)
{
$this->default_rights = $value;
}
/**
* return file commnents
* @return mixed
*/
public function getComment()
{
return $this->comment;
}
/**
* @param string $value
*/
public function setComment($value)
{
$this->comment = $value;
}
/**
* return the status of the file
* @return int
*/
public function getStatus()
{
return $this->status;
}
/**
* @param int $status Status of file
*/
public function setStatus($status)
{
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA set status = :status where id = :id";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':status' => $status,
':id' => $this->id
));
}
/**
* return a User OBJ of the person who checked out this file
* @return User
*/
public function getCheckerOBJ()
{
$user = new User($this->status, $this->connection);
return $user;
}
/**
* return the department ID of the file
* @return int
*/
public function getDepartment()
{
return $this->department;
}
/**
* @param int $value
*/
public function setDepartment($value)
{
$this->department = $value;
}
/**
* return the name of the department of the file
* @return string
*/
public function getDeptName()
{
$query = "SELECT name FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPARTMENT WHERE id = :department_id";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':department_id' => $this->getDepartment()));
$result = $stmt->fetchColumn();
if ($stmt->rowCount() == 0) {
echo('ERROR: No database entry exists in department table for ID = '. e::h($this->getDepartment()) .'.');
return "ERROR";
//exit;
}
return $result;
}
/**
* return the date that the file was created
* @return string
*/
public function getCreatedDate()
{
return $this->created_date;
}
/**
* return the latest modifying date on the file
* @return string
*/
public function getModifiedDate()
{
$query = "SELECT modified_on FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_LOG WHERE id = :id ORDER BY modified_on DESC limit 1";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetch();
$name = $result['modified_on'];
return $name;
}
/**
* return the realname of the file
* @return string
*/
public function getRealName()
{
return parent::getName();
}
/**
* Return the dept rights on this file for a given department
* @param int $dept_id
* @return int
*/
public function getDeptRights($dept_id)
{
$query = "
SELECT
rights
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DEPT_PERMS
WHERE
fid = :fid
AND
dept_id = :dept_id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':fid' => $this->id,
':dept_id' => $dept_id
));
$result = $stmt->fetchColumn();
return $result;
}
/**
* convert an array of user id into an array of user objects
* @param array $uid_array
* @return array
*/
public function toUserOBJs($uid_array)
{
$UserOBJ_array = array();
for ($i = 0; $i<sizeof($uid_array); $i++) {
$UserOBJ_array[$i] = new User($uid_array[$i], $this->connection);
}
return $UserOBJ_array;
}
/**
* Return a boolean on whether or not this file is publishable
* @return string
*/
public function isPublishable()
{
$query = "
SELECT
publishable
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetchColumn();
if ($stmt->rowCount() != 1) {
echo('DB error. Unable to locate file id ' . e::h($this->id) . ' in table '.$GLOBALS['CONFIG']['db_prefix'].'data. Please contact ' . $GLOBALS['CONFIG']['site_mail'] . ' for help');
exit;
}
return $result;
}
/**
* @return bool
*/
public function isArchived()
{
$query = "
SELECT
publishable
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetchColumn();
if ($stmt->rowCount() != 1) {
echo('DB error. Unable to locate file id ' . e::h($this->id) . ' in table '.$GLOBALS['CONFIG']['db_prefix'].'data. Please contact ' . $GLOBALS['CONFIG']['site_mail'] . ' for help');
exit;
}
return ($result == 2);
}
/**
* This function sets the publishable field in the data table to $boolean
* @param bool $boolean
*/
public function Publishable($boolean = true)
{
$query = "
UPDATE
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
SET
publishable = :boolean,
reviewer = :uid
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':boolean' => $boolean,
':uid' => $_SESSION['uid'],
':id' => $this->id
));
}
/**
* return the user id of the reviewer
* @return int
*/
public function getReviewerID()
{
$query = "
SELECT
reviewer
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetchColumn();
return $result;
}
/**
* return the username of the reviewer
* @return bool
*/
public function getReviewerName()
{
$reviewer_id = $this->getReviewerID();
if (isset($reviewer_id)) {
$user_obj = new User($reviewer_id, $this->connection);
return $user_obj->getName();
}
return false;
}
/**
* Set $comments into the reviewer comment field in the DB
* @param $comments
*/
public function setReviewerComments($comments)
{
$query = "
UPDATE
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
SET
reviewer_comments = :comments
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(
':comments' => $comments,
':id' => $this->id
));
}
/**
* Return the reviewers' comment toward this file
* @return string
*/
public function getReviewerComments()
{
$query = "
SELECT
reviewer_comments
FROM
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
$result = $stmt->fetchColumn();
return $result;
}
/**
*
*/
public function temp_delete()
{
$query = "
UPDATE
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
SET
publishable = 2
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
}
/**
*
*/
public function undelete()
{
$query = "
UPDATE
{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA
SET
publishable = 0
WHERE
id = :id
";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':id' => $this->id));
}
/**
* @return bool
*/
public function isLocked()
{
return $this->isLocked;
}
public function getRegristrosBase()
{
$query = "
SELECT
rb_id, rb_operacion_id
FROM
{$GLOBALS['CONFIG']['db_prefix']}data_rb
WHERE
data_id = ?
";
$stmt = $this->connection->prepare($query);
$stmt->execute([$this->id]);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public function getRegistrosBaseExtendido()
{
$query = '
SELECT r.id AS rb_id, r.codigo, r.detalle, o.id AS rb_operacion_id, o.orden, o.taller, o.descripcion
FROM ' . MAZDEN_DB_NAME . '.rb r
JOIN ' . MAZDEN_DB_NAME . '.rb_operaciones o ON o.idrb IN (r.id, r.idrb_origen)
JOIN ' . $GLOBALS['CONFIG']['db_prefix'] . 'data_rb d ON d.rb_id = r.id AND d.rb_operacion_id = o.id
WHERE d.data_id = ?
';
$stmt = $this->connection->prepare($query);
$stmt->execute([$this->id]);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}
}