fipamo/app/Services/DocService.php
ro 7024285b70
page update api up and running
the first part of the page editing API is working again after porting it over
form the old fipamo build. a few changes where made to make the code a
big more managable, but the end to end process works for updating pages.

the remaining page editing methods will be activated after the rendering
engine is in place because that's going to be a pretty siginficant
effort.

but this is a big step.
2024-03-08 14:09:43 -06:00

144 lines
3.6 KiB
PHP

<?php
namespace App\Services;
class DocService
{
public function __construct()
{
}
public static function writePages($task, $path, $fileLocation, $fileContents)
{
try {
if ($task == 'create') {
if (!is_dir('../content/pages/' . $path)) {
//Directory does not exist, so lets create it.
mkdir('../content/pages/' . $path, 0755, true);
}
file_put_contents($fileLocation, $fileContents);
} else {
($new = fopen($fileLocation, 'w')) or die('Unable to open file!');
fwrite($new, $fileContents);
fclose($new);
}
return true;
} catch (Error $error) {
return false;
}
}
public static function writeSettings($fileLocation, $fileContents)
{
if (!is_file($fileLocation)) {
file_put_contents($fileLocation, json_encode($fileContents));
} else {
($new = fopen($fileLocation, 'w')) or die('Unable to open file!');
fwrite($new, json_encode($fileContents));
fclose($new);
}
}
public static function writeHTML($location, $html, $path = null)
{
if ($path != null) {
if (!is_dir($path)) {
//Directory does not exist, so lets create it.
mkdir($path, 0755, true);
}
}
if (!is_file($location)) {
file_put_contents($location, $html);
} else {
($new = fopen($location, 'w')) or die('Unable to open file!');
fwrite($new, $html);
fclose($new);
}
}
public static function deleteFolder($path)
{
if (!empty($path) && is_dir($path)) {
$dir = new \RecursiveDirectoryIterator(
$path,
\RecursiveDirectoryIterator::SKIP_DOTS
); //upper dirs are not included,otherwise DISASTER HAPPENS :)
$files = new \RecursiveIteratorIterator(
$dir,
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $f) {
if (is_file($f)) {
unlink($f);
} else {
$empty_dirs[] = $f;
}
}
if (!empty($empty_dirs)) {
foreach ($empty_dirs as $eachDir) {
rmdir($eachDir);
}
}
rmdir($path);
}
}
public static function objectToMD($object)
{
$markdown = "---\n" .
'id: ' .
$object->id .
"\n" .
'uuid: ' .
$object->uuid .
"\n" .
'title: ' .
"'" .
$object->title .
"'" .
"\n" .
'feature: ' .
$object->imageList .
"\n" .
'files: ' .
$object->fileList .
"\n" .
'path: ' .
$object->path .
"\n" .
'layout: ' .
$object->layout .
"\n" .
'tags: ' .
$object->tags .
"\n" .
'author: ' .
$object->author .
"\n" .
'created: ' .
$object->created .
"\n" .
'updated: ' .
$object->updated .
"\n" .
'deleted: ' .
$object->deleted .
"\n" .
'slug: ' .
$object->slug .
"\n" .
'menu: ' .
$object->menu .
"\n" .
'published: ' .
$object->menu .
"\n" .
'featured: ' .
$object->featured .
"\n---\n" .
$object->content;
return $markdown;
}
}