Overview
Comment:Fix web page file creation/move/rename
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA3-256: fa6b527ca35f194c017fae8c96559102b25d940e1abf3ca3c1b1d36af1c11773
User & Date: bohwaz on 2021-04-09 01:35:10
Other Links: manifest | tags
Context
2021-04-09
01:49
Make message more contextual check-in: 4b897e31c6 user: bohwaz tags: trunk, stable
01:35
Fix web page file creation/move/rename check-in: fa6b527ca3 user: bohwaz tags: trunk, stable
00:50
Fix sync of files check-in: 610de89bf3 user: bohwaz tags: trunk, stable
Changes

Modified src/include/lib/Garradin/Entities/Web/Page.php from [a21e210a95] to [c5b7261d79].

79
80
81
82
83
84
85
86
87
88
89
90
91
92


93
94
95
96
97
98
99
79
80
81
82
83
84
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100







-






+
+







		$page = new self;
		$data = compact('type', 'parent', 'title', 'status');
		$data['content'] = '';

		$page->importForm($data);
		$page->published = new \DateTime;
		$page->modified = new \DateTime;
		$page->file_path = $page->filepath();
		$page->type = $type;

		$db = DB::getInstance();
		if ($db->test(self::TABLE, 'uri = ?', $page->uri)) {
			$page->importForm(['uri' => $page->uri . date('-Y-m-d-His')]);
		}

		$page->file_path = $page->filepath(false);

		return $page;
	}

	public function file(bool $force_reload = false)
	{
		if (null === $this->_file || $force_reload) {
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
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







-
+


-
-
+
+
+
+
+
+
+
+
+

-
-
-
-
+
+
+
+
-
-
-
+
+

-
+
-
-
-
-
+
+
+
-
-
-
-
+
+













+
+
+
-
+
+

+







	}

	public function path(): string
	{
		return $this->path;
	}

	public function syncFile(): void
	public function syncFile(string $path): void
	{
		$export = $this->export();
		$path = $this->filepath();
		$target = $this->filepath(false);

		$exists = Files::callStorage('exists', $path);

		// Create file if required
		if (!$exists) {
			$file = $this->_file = File::createAndStore(Utils::dirname($path), Utils::basename($path), null, $export);
		}
		else {
			$target = $this->filepath(false);

		// Move parent directory if needed
		if ($path && $path !== $target) {
			$dir = Files::get(Utils::dirname($path));
			$dir->rename(Utils::dirname($target));
			// Move parent directory if needed
			if ($path !== $target) {
				$dir = Files::get(Utils::dirname($path));
				$dir->rename(Utils::dirname($target));
			$this->set('file_path', $target);
			$this->_file = null;
		}
				$this->_file = null;
			}

		if (!$this->file()) {
			$file = $this->file();
			$file = $this->_file = File::createAndStore(Utils::dirname($target), Utils::basename($target), null, $export);
			$this->set('modified', new \DateTime);
		}
		elseif ($this->file()->fetch() !== $export) {

			// Or update file
			if ($file->fetch() !== $export) {
			$file = $this->file();
			$file->set('modified', new \DateTime);
			$this->set('modified', clone $file->modified);
			$file->store(null, $this->export());
				$file->store(null, $export);
			}
		}

		$this->syncSearch();
	}

	public function syncSearch(): void
	{
		$content = $this->format == self::FORMAT_ENCRYPTED ? null : strip_tags($this->render());
		$this->file()->indexForSearch(null, $content, $this->title);
	}

	public function save(): bool
	{
		if (isset($this->_modified['uri']) || isset($this->_modified['path'])) {
			$this->set('file_path', $this->filepath(false));
		}
		$this->syncFile();

		$current_path = $this->_modified['file_path'] ?? $this->file_path;
		parent::save();
		$this->syncFile($current_path);

		return true;
	}

	public function delete(): bool
	{
		Files::get(Utils::dirname($this->file_path))->delete();