/** * Adds a image * * @param {string} $id * @param {string} $name * @param {string} $caption * @param {string} $galleryId * @param {string} $siteId * @return {array} */ public static function add($id, $name, $url, $thumb, $caption, $galleryId, $siteId) { $gallery = Gallery::getById($galleryId, $siteId); $image = array('id' => $id, 'name' => $name, 'url' => $url, 'thumb' => $thumb, 'caption' => $caption); array_push($gallery->images, $image); $gallery->save($siteId); return $gallery; }
/** * Lists all galleries and images * * @param {string} $siteId * @return {array} */ public static function listExtended($siteId) { $galleries = Gallery::listAll($siteId); $i = 0; foreach ($galleries as $gallery) { $galleries[$i]['images'] = GalleryImage::listAll($gallery['id'], $siteId); $i++; } return $galleries; }
/** * Removes the gallery * * @return Response */ public function remove(Request $request) { // get request data $email = $request->input('auth-email'); $siteId = $request->input('auth-id'); // get id $id = $request->json()->get('id'); $gallery = Gallery::getById($id, $siteId); if ($gallery !== NULL) { $gallery->remove($siteId); // return OK return response('OK, gallery removed at = ' . $gallery->id, 200); } return response('Gallery not found', 400); }
/** * Removes the gallery image * * @return Response */ public function remove(Request $request) { // get request data $email = $request->input('auth-email'); $siteId = $request->input('auth-id'); // id, galleryId $id = $request->json()->get('id'); $galleryId = $request->json()->get('galleryId'); // get form $gallery = Gallery::getById($galleryId, $siteId); if ($gallery != NULL) { $index = GalleryImage::getIndexById($id, $galleryId, $siteId); array_splice($gallery->images, $index, 1); $gallery->save($siteId); // get site and user $site = Site::getById($siteId); $user = User::getByEmail($email, $siteId); // re-publish plugins Publish::publishPlugins($user, $site); return response('Ok', 200); } return response('Gallery not found', 400); }
/** * Publishes plugins for the site * * @param {Site} $site */ public static function publishPlugins($user, $site) { // get plugins for the site $dir = app()->basePath() . '/public/sites/' . $site->id . '/plugins/'; $exts = array('html'); $files = Utilities::listFiles($dir, $site->id, $exts); $plugins = array(); foreach ($files as $file) { $path = app()->basePath() . '/public/sites/' . $site->id . '/' . $file; if (file_exists($path)) { $html = file_get_contents($path); $id = basename($path); $id = str_replace('.html', '', $id); // push plugin to array array_push($plugins, $id); } } // location where twig should look for templates (local to site, then global) $template_dirs = array(app()->basePath() . '/public/sites/' . $site->id . '/plugins'); $global_plugin_dir = app()->basePath() . '/resources/plugins'; if (file_exists($global_plugin_dir)) { array_push($template_dirs, $global_plugin_dir); } // setup twig $loader = new \Twig_Loader_Filesystem($template_dirs); $twig = new \Twig_Environment($loader); $twig->addExtension(new BetterSortTwigExtension()); // get all pages $pages = Page::listAll($user, $site); // list all forms, menus, galleries $forms = Form::listExtended($site->id); $menus = Menu::listExtended($site->id); $galleries = Gallery::listExtended($site->id); $i = 0; // get html of pages foreach ($pages as $page) { // stript html $url = $page['url']; $url = preg_replace('/\\.[^.\\s]{3,4}$/', '', $url); // get html of page $file = app()->basePath() . '/public/sites/' . $site->id . '/' . $url . '.html'; if (file_exists($file)) { $html = file_get_contents($file); // set parser $dom = HtmlDomParser::str_get_html($html, $lowercase = true, $forceTagsClosed = false, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = false, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT); // find main content $el = $dom->find('[role=main]'); $main_content = ''; // get the fragment content if (isset($el[0])) { $main_content = $el[0]->innertext; } // set html $pages[$i]['html'] = $main_content; } $i++; } $i = 0; // public plugin for pages foreach ($pages as $item) { // get page $page = new Page($item); // setup current page $current_page = array('url' => $page->url, 'title' => $page->title, 'description' => $page->description, 'keywords' => $page->keywords, 'callout' => $page->callout, 'photo' => $page->photo, 'thumb' => $page->thumb, 'language' => $page->language, 'direction' => $page->direction, 'firstName' => $page->firstName, 'lastName' => $page->lastName, 'lastModifiedBy' => $page->lastModifiedBy, 'lastModifiedDate' => $page->lastModifiedDate); // setup whether the site is using friendly urls $useFriendlyURLs = false; if (env('FRIENDLY_URLS') === true || env('FRIENDLY_URLS') === 'true') { $useFriendlyURLs = true; } // setup current site $current_site = array('id' => $site->id, 'name' => $site->name, 'email' => $site->email, 'api' => Utilities::retrieveAppUrl() . '/api', 'useFriendlyURLs' => $useFriendlyURLs); // set url $url = $page->url; $url = preg_replace('/\\.[^.\\s]{3,4}$/', '', $url); $location = app()->basePath() . '/public/sites/' . $site->id . '/' . $url . '.html'; // check for valid location if (file_exists($location)) { // get html from page $html = file_get_contents($location); // walk through plugins foreach ($plugins as $plugin) { // insert into respond-plugin comments $start = '<!-- respond-plugin:' . $plugin . ' -->'; $end = '<!-- /respond-plugin:' . $plugin . ' -->'; // check for start and end if (strpos($html, $start) !== FALSE && strpos($html, $end) !== FALSE) { // load the template $template = $twig->loadTemplate($plugin . '.html'); // render the template $plugin_html = $template->render(array('pages' => $pages)); // replace content $html = Utilities::replaceBetween($html, $start, $end, $plugin_html); } } // make sure the html is not empty if (!empty($html)) { // load the parser $dom = HtmlDomParser::str_get_html($html, $lowercase = true, $forceTagsClosed = false, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = false, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT); // insert into [respond-plugin] elements foreach ($dom->find('[respond-plugin]') as $el) { if (isset($el->type)) { if (array_search($el->type, $plugins) !== FALSE) { // load the template $template = $twig->loadTemplate($el->type . '.html'); $render_arr = array('page' => $current_page, 'site' => $current_site, 'pages' => $pages, 'forms' => $forms, 'galleries' => $galleries, 'menus' => $menus, 'attributes' => $el->attr); // render the template $plugin_html = $template->render($render_arr); // set the inner text $el->innertext = $plugin_html; } } } } // find main content $el = $dom->find('[role=main]'); $main_content = ''; // get the fragment content if (isset($el[0])) { $main_content = $el[0]->innertext; } // put html back file_put_contents($location, $dom); // update html in the array $pages[$i]['html'] = $main_content; // increment $i++; } } }
/** * Edits a page provided by the querystring, in format ?q=site-name/dir/page.html * * @return Response */ public function edit(Request $request) { $q = $request->input('q'); if ($q != NULL) { $arr = explode('/', $q); if (sizeof($arr) > 0) { $siteId = $arr[0]; // set html if hiddne $url = $q; // strip any trailing .html from url $url = preg_replace('/\\.[^.\\s]{3,4}$/', '', $url); // add .html for non-friendly URLs if (env('FRIENDLY_URLS') === false) { $url .= '.html'; } // load page $path = rtrim(app()->basePath('public/sites/' . $url), '/'); if (file_exists($path)) { $html = file_get_contents($path); // set dom $dom = HtmlDomParser::str_get_html($html, $lowercase = true, $forceTagsClosed = false, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = false, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT); // find base element $el = $dom->find('base', 0); $el->setAttribute('href', '/sites/' . $siteId . '/'); // get settings $sortable = Setting::getById('sortable', $siteId); $editable = Setting::getById('editable', $siteId); $blocks = Setting::getById('blocks', $siteId); $grid = Setting::getById('grid', $siteId); $framework = Setting::getById('framework', $siteId); // framework if ($framework === NULL) { $framework = 'bootstrap'; } // defaults if ($grid === NULL) { $grid = '[{"name": "1 Column","desc": "100%","html": "<div class=\\"block row\\" hashedit-block><div class=\\"col col-md-12\\" hashedit-sortable></div></div>"}]'; } else { $grid = json_encode($grid); } // defaults if ($sortable === NULL) { $sortable = '.col, .column'; } // get editable array if ($editable === NULL) { $editable = ['[role=main]']; } else { $editable = explode(',', $editable); // trim elements in the array $editable = array_map('trim', $editable); } if ($blocks === NULL) { $blocks = '.row'; } // find body element $el = $dom->find('body', 0); $el->setAttribute('hashedit-active', ''); // setup editable areas foreach ($editable as $value) { // find body element $els = $dom->find($value); foreach ($els as $el) { $el->setAttribute('hashedit', ''); $el->setAttribute('hashedit-selector', $value); } } // init $plugins_script = ''; // get custom plugins $js_file = app()->basePath() . '/resources/sites/' . $siteId . '/plugins.js'; if (file_exists($js_file)) { if (file_exists($js_file)) { $plugins_script .= file_get_contents($js_file); } } // inject forms into script if (strpos($plugins_script, 'respond.forms') !== false) { $arr = Form::listAll($siteId); $options = array(); // get id foreach ($arr as $item) { array_push($options, array('text' => $item['name'], 'value' => $item['id'])); } // inject forms into script $plugins_script = str_replace("['respond.forms']", json_encode($options), $plugins_script); } // inject galleries into script if (strpos($plugins_script, 'respond.galleries') !== false) { $arr = Gallery::listAll($siteId); $options = array(); // get id foreach ($arr as $item) { array_push($options, array('text' => $item['name'], 'value' => $item['id'])); } // inject galleries into script $plugins_script = str_replace("['respond.galleries']", json_encode($options), $plugins_script); } // inject routes into script if (strpos($plugins_script, 'respond.routes') !== false) { $dir = $file = app()->basePath() . '/public/sites/' . $siteId; $arr = array_merge(array('/'), Utilities::listRoutes($dir, $siteId)); $options = array(); // get id foreach ($arr as $item) { array_push($options, array('text' => $item, 'value' => $item)); } // inject galleries into script $plugins_script = str_replace("['respond.routes']", json_encode($options), $plugins_script); } // inject pages into script if (strpos($plugins_script, 'respond.pages') !== false) { $arr = Pages::listAllBySite($siteId); $options = array(); // get id foreach ($arr as $item) { array_push($options, array('text' => $item['title'], 'value' => $item['url'])); } // inject galleries into script $plugins_script = str_replace("['respond.galleries']", json_encode($options), $plugins_script); } // remove elements from that have been excluded $els = $dom->find('[hashedit-exclude]'); // add references to each element foreach ($els as $el) { $el->outertext = ''; } // setup references $parent = $dom->find('[role=main]', 0); if (env('APP_ENV') == 'development') { // hashedit development stack $hashedit = <<<EOD <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700" rel="stylesheet" type="text/css"> <script src="/dev/hashedit/js/fetch.min.js"></script> <script src="/dev/hashedit/js/i18next.js"></script> <script src="/dev/hashedit/node_modules/sortablejs/Sortable.js"></script> <script src="/dev/hashedit/node_modules/dropzone/dist/dropzone.js"></script> <script src="/dev/hashedit/js/hashedit.js"></script> <script>{$plugins_script}</script> <script> hashedit.setup({ dev: true, url: '{$url}', sortable: '{$sortable}', blocks: '{$blocks}', grid: {$grid}, framework: '{$framework}', login: '******', translate: true, languagePath: '/i18n/{{language}}.json', auth: 'token', authHeader: 'X-AUTH' }); </script> EOD; } else { // hashedit production stack $hashedit = <<<EOD <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700" rel="stylesheet" type="text/css"> <script src="/app/libs/hashedit/dist/hashedit-min.js"></script> <script>{$plugins_script}</script> <script> hashedit.setup({ url: '{$url}', sortable: '{$sortable}', blocks: '{$blocks}', grid: {$grid}, framework: '{$framework}', login: '******', path: '/app/libs/hashedit/', stylesheet: ['/app/libs/hashedit/dist/hashedit-min.css'], translate: true, languagePath: '/i18n/{{language}}.json', auth: 'token', authHeader: 'X-AUTH' }); </script> EOD; } $hashedit .= '<style type="text/css">' . '.respond-plugin {' . ' position: relative;' . ' padding: 10px 0;' . ' margin: 1px 0 20px 0;' . ' background-color: #f8f8f8;' . ' border: 1px solid #f0f0f0;' . ' text-align: center;' . ' color: #aaa;' . '}' . '[hashedit-element]:hover .respond-plugin { border: 1px solid #00ADE3 !important; }' . '.respond-plugin span {' . ' display: block;' . ' margin: 0; padding: 0;' . ' color: #aaa;' . ' text-align: center;' . ' text-transform: uppercase;' . ' font-size: 11px;' . ' font-family: "Open Sans", sans-serif;' . '}' . '.respond-plugin svg{' . ' fill: currentColor;' . ' width: 35px;' . ' height: 35px;' . '}'; // find body element $el = $dom->find('body', 0); // append $el->outertext = $el->makeup() . $el->innertext . $hashedit . '</body>'; // return updated html return $dom; } } } }