-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.php
127 lines (91 loc) · 3.16 KB
/
bin.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
<?php
include_once("./database.php");
use RedBeanPHP\Logger as Logger;
if ($argv[1] == "nuke") {
R::nuke();
die();
}
if ($argv[1] == "truncate_category") {
R::wipe(T_CATEGORIES);
die();
}
if ($argv[1] == "list_tables") {
$listOfTables = R::inspect();
die(json_encode($listOfTables));
}
if ($argv[1] == "list_fields") {
$fields = R::inspect($argv[2]);
die(json_encode($fields));
}
if ($argv[1] == "create_scheme") {
class MigrationLogger implements Logger {
private $file;
public function __construct( $file ) {
$this->file = $file;
}
public function log() {
$query = func_get_arg(0);
if (preg_match( '/^(CREATE|ALTER)/', $query )) {
file_put_contents( $this->file, "{$query};\n", FILE_APPEND );
}
}
}
$ml = new MigrationLogger( sprintf( __DIR__.'/sql/migration_%s.sql', date('Y_m_d__H_i_s') ) );
R::getDatabaseAdapter()
->getDatabase()
->setLogger($ml)
->setEnableLogging(TRUE);
R::nuke();
$oCategory = R::dispense(T_CATEGORIES);
$oCategory->name = 'Тестовая категория';
$oCategory->description = 'Тестовая категория';
$oCategory2 = R::dispense(T_CATEGORIES);
$oCategory2->name = 'Тестовая категория 2';
$oCategory2->description = 'Тестовая категория 2';
R::store($oCategory2);
$oCategory->tcategories = $oCategory2;
R::store($oCategory);
$oFile = R::dispense(T_FILES);
$oFile->created_at = date("Y-m-d H:i:s");
$oFile->updated_at = date("Y-m-d H:i:s");
$oFile->timestamp = time();
$oFile->name = 'Тестовая заметка';
$oFile->description = 'Тестовая заметка';
$oFile->type = "php";
$oFile->filename = 'test.php';
$oFile->tcategories = $oCategory2;
R::store($oFile);
$oTag = R::dispense(T_TAGS);
$oTag->created_at = date("Y-m-d H:i:s");
$oTag->updated_at = date("Y-m-d H:i:s");
$oTag->timestamp = time();
$oTag->name = 'Тестовый тэг';
R::store($oTag);
$oTagToObjects = R::dispense(T_TAGS_TO_OBJECTS);
$oTagToObjects->ttags = $oTag;
$oTagToObjects->content_id = $oNote->id;
$oTagToObjects->content_type = 'tnotes';
$oTagToObjects->poly('contentType');
R::store($oTagToObjects);
R::trashBatch(T_FILES, [$oTask->id]);
R::trashBatch(T_CATEGORIES, [$oCategory->id]);
R::trashBatch(T_CATEGORIES, [$oCategory2->id]);
R::trashBatch(T_TAGS, [$oTag->id]);
R::trashBatch(T_TAGS_TO_OBJECTS, [$oTagToObjects->id]);
die(json_encode([]));
}
function fnBuildRecursiveCategoriesTree(&$aResult, $aCategories)
{
$aResult = [];
foreach ($aCategories as $oCategory) {
$aTreeChildren = [];
$aChildren = R::children($oCategory, " id != {$oCategory->id}");
fnBuildRecursiveCategoriesTree($aTreeChildren, $aChildren);
$aResult[] = [
'id' => $oCategory->id,
'text' => $oCategory->name,
'children' => $aTreeChildren,
'notes_count' => $oCategory->countOwn(T_FILES)
];
}
}