public function viewListAction($gadgetId, $page, $format) { $em = $this->get('doctrine')->getManager(); $gadget = $em->getRepository('KeosuCoreBundle:Gadget')->find($gadgetId); $gadgetConfig = $gadget->getConfig(); $articlesperpage = $gadgetConfig['articlesPerPage']; $tag = $gadgetConfig['tag']; //Preparation of queries count and listArticle $queryArticle = $em->createQueryBuilder(); $queryCount = $em->createQueryBuilder(); $queryArticle->add('select', 'a'); $queryCount->add('select', 'count(DISTINCT a.id)'); if ($tag != "") { $where = 'a.id = t.articleBody and t.tagName= ?1 '; $queryCount->add('from', 'Keosu\\DataModel\\ArticleModelBundle\\Entity\\ArticleBody a, Keosu\\DataModel\\ArticleModelBundle\\Entity\\ArticleTags t')->add('where', $where); $queryCount->setParameter(1, $tag); $queryArticle->add('from', 'Keosu\\DataModel\\ArticleModelBundle\\Entity\\ArticleBody a, Keosu\\DataModel\\ArticleModelBundle\\Entity\\ArticleTags t')->add('where', $where)->setParameter(1, $tag); } else { $queryCount->add('from', 'Keosu\\DataModel\\ArticleModelBundle\\Entity\\ArticleBody a'); $queryArticle->add('from', 'Keosu\\DataModel\\ArticleModelBundle\\Entity\\ArticleBody a'); } $queryArticle->add('orderBy', 'a.date DESC')->setFirstResult($page * $articlesperpage)->setMaxResults($articlesperpage); //Execution of queries $count = $queryCount->getQuery()->execute(); $count = $count[0][1]; $articleList = $queryArticle->getQuery()->execute(); foreach ($articleList as $article) { $article->setBody(TemplateUtil::formatTemplateString($article->getBody())); } //Prepare data result $data = array(); foreach ($articleList as $key => $article) { $data[$key]['id'] = $article->getId(); $data[$key]['title'] = $article->getTitle(); $data[$key]['content'] = $article->getBody(); $data[$key]['dataModelObjectName'] = $article->getDataModelObjectName(); $data[$key]['enableComments'] = $article->getEnableComments(); $attachments = $article->getAttachments(); if (count($attachments) > 0) { foreach ($attachments as $k => $attachment) { $data[$key]['attachments'][$k]['path'] = $this->container->getParameter('url_base') . $attachment->getWebPath(); $data[$key]['attachments'][$k]['thumbpath'] = $this->container->getParameter('url_base') . $attachment->getThumbWebPath(); } } } $ret = array('data' => $data); $isFirst = false; if ($page == 0) { $isFirst = true; } $isLast = false; if (($page + 1) * $articlesperpage >= $count) { $isLast = true; } $ret['isFirst'] = $isFirst; $ret['isLast'] = $isLast; return new JsonResponse($ret); }
public function onAppSaveData(PackageSaveAppEvent $event) { $em = $this->container->get('doctrine')->getManager(); $packageManager = $this->container->get('keosu_core.packagemanager'); $app = $event->getApp(); $appPrivate = !!$app->getConfigPackages()[KeosuGadgetAuthenticationGadgetBundle::PACKAGE_NAME]['privateApp']; // if the app is private we have to add an authentication page $authenticationPage = $em->getRepository('KeosuCoreBundle:Page')->findOneBy(array('appId' => $app->getId(), 'templateId' => KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_TEMPLATE_ID, 'name' => KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_PAGE_NAME)); if ($authenticationPage == null && $appPrivate) { // remove other main $pagesMain = $em->getRepository('KeosuCoreBundle:Page')->findBy(array('appId' => $app->getId(), 'isMain' => true)); foreach ($pagesMain as $pageMain) { $pageMain->setIsMain(false); $em->persist($pageMain); } $page = new Page(); $page->setIcon('glyphicon-lock'); $page->setTemplateId(KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_TEMPLATE_ID); $page->setName(KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_PAGE_NAME); $page->setAppId($app->getId()); // the authentication become main $page->setIsMain(true); $em->persist($page); $gadget = new Gadget(); $gadget->setShared(false); $listTemplate = $packageManager->getListTemplateForGadget(KeosuGadgetAuthenticationGadgetBundle::PACKAGE_NAME); $keys = array_keys($listTemplate); $gadget->setName(KeosuGadgetAuthenticationGadgetBundle::PACKAGE_NAME); $gadget->setTemplate($listTemplate[$keys[0]]); // 0 by default $gadget->setPage($page); // there is only one zone on this template $templateHtml = file_get_contents(TemplateUtil::getPageTemplateAbsolutePath() . KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_TEMPLATE_ID); //Get all the elements of class "zone" in template dom $crawler = new Crawler($templateHtml); $zones = $crawler->filter('.zone')->extract(array('id')); $gadget->setZone($zones[0]); $em->persist($gadget); } else { if (!$appPrivate && $authenticationPage != null) { $gadgets = $em->getRepository('KeosuCoreBundle:Gadget')->findByPage($authenticationPage->getId()); //First delete manually all its gadget foreach ($gadgets as $gadget) { $em->remove($gadget); } $em->remove($authenticationPage); } } $em->flush(); }
/** * Edit page form */ private function buildPageForm($formBuilder) { $appId = $this->container->get('keosu_core.curapp')->getCurApp(); $em = $this->get('doctrine')->getManager(); $app = $em->getRepository('KeosuCoreBundle:App')->find($appId); $formBuilder->add('name', 'text')->add('isMain', 'checkbox', array('required' => false))->add('templateId', 'choice', array('choices' => TemplateUtil::getTemplateList(), 'required' => true, 'expanded' => true)); }
/** * Create the form to edit/add the gadget */ private function formGadget($gadget) { $em = $this->get('doctrine')->getManager(); $request = $this->get('request'); $dispatcher = $this->get('event_dispatcher'); $formBuilder = $this->createFormBuilder($gadget); $configType = new ConfigGadgetType($dispatcher, $request, $this->container->get('keosu_core.packagemanager'), $gadget); $listTemplate = $this->get('keosu_core.packagemanager')->getListTemplateForGadget($gadget->getName()); if (count($listTemplate) > 1) { $formBuilder->add('template', 'choice', array('choices' => $listTemplate, 'required' => true, 'expanded' => true)); } else { $formBuilder->add('template', 'text', array('label' => false, 'data' => PackageManager::DEFAULT_TEMPLATE_GADGET_NAME, 'attr' => array('style' => 'display:none;'))); } $formBuilder->add('shared', 'checkbox', array('label' => 'Shared with all pages', 'required' => false))->add('config', $configType); $form = $formBuilder->getForm(); if ($request->getMethod() == 'POST') { $form->bind($request); if ($form->isValid()) { $event = new GadgetSaveConfigEvent($form, $request, $gadget); $dispatcher->dispatch(KeosuEvents::GADGET_CONF_SAV . $gadget->getName(), $event); if ($event->getResponse() !== null) { return $event->getResponse(); } $em->persist($gadget); $em->flush(); $this->get('keosu_core.exporter')->exportApp(); return $this->redirect($this->generateUrl('keosu_core_views_page', array('id' => $gadget->getPage()->getId()))); } } $event = new FormEvent($form, $request); $dispatcher->dispatch(KeosuEvents::GADGET_CONF_VIEW . $gadget->getName(), $event); if ($event->getResponse() !== null) { return $event->getResponse(); } return $this->render('KeosuCoreBundle:Page:editGadget.html.twig', array('form' => $form->createView(), 'gadgetDir' => TemplateUtil::getTemplateGadgetDir(), 'gadgetName' => $gadget->getName())); }
/** * Set default data for the form. */ public function __construct() { $this->templateId = array_keys(TemplateUtil::getTemplateList())[0]; }
public function export($appId) { $em = $this->doctrine->getManager(); $baseurl = $this->container->getParameter('url_base'); $dispatcher = $this->container->get('event_dispatcher'); $pages = $em->getRepository('KeosuCoreBundle:Page')->findByAppId($appId); $this->cleanDir(); //Export theme $app = $em->getRepository('KeosuCoreBundle:App')->find($appId); $json = json_encode(array('name' => $app->getName(), 'host' => $baseurl . $this->container->getParameter('url_param'), 'appId' => $appId)); FilesUtil::copyContent($json, $this->getExportAbsolutePath() . '/simulator/www/data/globalParam.json'); FilesUtil::copyFolder(ThemeUtil::getAbsolutePath() . $app->getTheme() . '/style', $this->getExportAbsolutePath() . '/simulator/www/theme'); //cordova_plugins.json copy(TemplateUtil::getAbsolutePath() . '/main-header/cordova_plugins.js', $this->getExportAbsolutePath() . '/simulator/www/cordova_plugins.js'); copy(TemplateUtil::getAbsolutePath() . '/main-header/index.html', $this->getExportAbsolutePath() . '/simulator/www/index.html'); //Copy all theme/header/js dir to web/export/www/js FilesUtil::copyFolder(ThemeUtil::getAbsolutePath() . $app->getTheme() . '/header/js', $this->getExportAbsolutePath() . '/simulator/www/js'); //Copy Splashcreens and icons FilesUtil::copyFolder($this::getImageDir($app->getId()) . '/', $this->getExportAbsolutePath() . '/simulator/www/res/'); // list of imported gadgets $importedPackages = array(); $httpLinks = array(); $jsInit = $jsCore = $jsEnd = ''; $css = ''; // load index.html (main template) $indexHtml = new SmartDOMDocument(); $fileContent = file_get_contents($this->getExportAbsolutePath() . '/simulator/www/index.html'); @$indexHtml->loadHTML($fileContent); /////////////////////////////////////////////////// // Generate config.xml ////////////////////////////////////////////////// $configXml = new \DOMDocument('1.0', 'UTF-8'); $configXml->formatOutput = true; $widget = $configXml->createElement('widget'); $widget->setAttribute('xmlns', 'http://www.w3.org/ns/widgets'); $widget->setAttribute('xmlns:gap', 'http://phonegap.com/ns/1.0'); $widget->setAttribute('id', $app->getPackageName()); $configXml->appendChild($widget); $widget->appendChild($configXml->createElement('name', $app->getName())); $widget->appendChild($configXml->createElement('description', $app->getDescription())); $author = $configXml->createElement('author', $app->getAuthor()); $author->setAttribute('href', $app->getWebsite()); $author->setAttribute('email', $app->getEmail()); $widget->setAttribute('version', $app->getVersion()); $widget->setAttribute('versionCode', $app->getVersion()); $widget->appendChild($author); $mainPage = null; $paramGadget = array(); $paramGadget['host'] = $baseurl . $this->container->getParameter('url_param'); //////////////////////////////////////// // Generate Keosu's base //////////////////////////////////////// try { $this->importPackage('keosu-base', $indexHtml, $configXml, $jsInit, $jsCore, $jsEnd, $css, $importedPackages, $app, $httpLinks); } catch (\Exception $e) { throw new \LogicException('Unable to import keosu-base because ' . $e->getMessage()); } //////////////////////////////////////// // Generate view for each page //////////////////////////////////////// foreach ($pages as $page) { if ($mainPage == null) { $mainPage = $page->getId(); } if ($page->getIsMain()) { $mainPage = $page->getId(); } $document = new SmartDOMDocument(); $fileContent = file_get_contents(TemplateUtil::getPageTemplateAbsolutePath() . $page->getTemplateId()); @$document->loadHTML($fileContent); $finder = new \DOMXPath($document); $classname = "zone"; //Find all the zone div in page template $zones = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' {$classname} ')]"); foreach ($zones as $zone) { $zoneId = $zone->getAttribute('id'); //Look if there is a shared gadget in this zone $gadget = $em->getRepository('KeosuCoreBundle:Gadget')->findSharedByZoneAndApp($zoneId, $appId); //If there is no share gadget we try to find the specific one if ($gadget == null) { //Find the gadget associated with page and zone $gadget = $em->getRepository('KeosuCoreBundle:Gadget')->findOneBy(array('zone' => $zoneId, 'page' => $page->getId())); } if ($gadget != null) { // import if it's needed if (array_search($gadget->getName(), $importedPackages) === false) { try { $this->importPackage($gadget->getName(), $indexHtml, $configXml, $jsInit, $jsCore, $jsEnd, $css, $importedPackages, $app, $httpLinks); } catch (\Exception $e) { throw new \LogicException('Unable to import ' . $gadget->getName() . ' because ' . $e->getMessage()); } } $package = $this->packageManager->findPackage($gadget->getName()); $packageConfig = $this->packageManager->getConfigPackage($package->getPath()); // set param for gadgets $paramGadget['gadgetId'] = $gadget->getId(); $paramGadget['pageId'] = $page->getId(); $paramGadget['gadgetParam'] = array(); $paramGadget['gadgetParam'] = $this->secureParameters($gadget->getConfig(), $packageConfig); // globalParam $paramGadget['appParam'] = array(); if (isset($app->getConfigPackages()[$gadget->getName()])) { $paramGadget['appParam'] = $this->secureParameters($app->getConfigPackages()[$gadget->getName()], $packageConfig); } $event = new ExportConfigPackageEvent($paramGadget); $dispatcher->dispatch(KeosuEvents::PACKAGE_EXPORT_CONFIG . $package->getName(), $event); if ($event->getNewConfig() !== null) { $paramGadget = $event->getNewConfig(); } $dataEvent = new ExportDataPackageEvent($appId, $paramGadget); $dispatcher->dispatch(KeosuEvents::PACKAGE_EXPORT_DATA . $package->getName(), $dataEvent); if ($dataEvent->getData() !== null) { if ($dataEvent->getFileName() != null) { $this->writeFile($dataEvent->getData(), $dataEvent->getFileName(), '/simulator/www/data/'); } else { $this->writeFile($dataEvent->getData(), $gadget->getId() . ".json", '/simulator/www/data/'); } } //Copy in HTML $gadgetTemplateHtml = file_get_contents($package->getPath() . '/templates/' . $gadget->getTemplate()); $zone->nodeValue = $gadgetTemplateHtml; //Add the angularJS directive to zone // import param $zone->setAttribute('ng-controller', $gadget->getName() . 'Controller'); $zone->setAttribute('ng-init', 'init(' . json_encode($paramGadget) . ')'); //Saving node $zone->ownerDocument->saveXML($zone); } } $bodyEl = $document->getElementsByTagName('body')->item(0); $html = $this->domInnerHtml($bodyEl); $html = StringUtil::decodeString($html); $this->writeFile($html, $page->getFileName(), '/simulator/www/'); } /////////////////////////////////////////////////// // Generate main view index.html /////////////////////////////////////////////////// // import theme in index.html $tmpthemeHeader = new SmartDOMDocument(); $fileContent = file_get_contents(ThemeUtil::getAbsolutePath() . $app->getTheme() . '/header/header.html'); @$tmpthemeHeader->loadHTML($fileContent); $children = $tmpthemeHeader->getElementsByTagName('head')->item(0)->childNodes; foreach ($children as $child) { $indexHtml->getElementsByTagName('head')->item(0)->appendChild($indexHtml->importNode($child)); } // import weinre if the app is in debug mode // @see https://people.apache.org/~pmuellr/weinre/docs/latest/Home.html if ($app->getDebugMode() == true) { $script = $indexHtml->createElement('script'); $domainName = parse_url($baseurl); $script->setAttribute('src', 'http://' . $domainName['host'] . ':8080/target/target-script-min.js#anonymous'); $indexHtml->getElementsByTagName('head')->item(0)->appendChild($script); } // this should always be at the end $script = $indexHtml->createElement('script'); $script->setAttribute('src', 'js/app.js'); $indexHtml->getElementsByTagName('head')->item(0)->appendChild($script); $link = $indexHtml->createElement('link'); $link->setAttribute('rel', 'stylesheet'); $link->setAttribute('type', 'text/css'); $link->setAttribute('href', 'js/app.css'); $indexHtml->getElementsByTagName('head')->item(0)->appendChild($link); $this->writeFile(StringUtil::decodeString($indexHtml->saveHTML()), 'index.html', '/simulator/www/'); //////////////////////////////////////////////////// // import all gadget required controller //////////////////////////////////////////////////// $appJs = 'var importedPackages = ' . \json_encode($importedPackages) . ";\n"; $appJs .= $jsInit . $jsCore . $jsEnd; $this->writeFile($appJs, 'app.js', '/simulator/www/js/'); // import CSS of all packages $this->writeFile($css, 'app.css', '/simulator/www/js/'); //Enable individual API permissions here. //The "device" permission is required for the 'deviceready' event. $basePlugin = array('org.apache.cordova.device', 'org.apache.cordova.device-motion', 'org.apache.cordova.device-orientation', 'org.apache.cordova.dialogs'); foreach ($basePlugin as $plugin) { $device = $configXml->createElement('gap:plugin'); $device->setAttribute('name', $plugin); $widget->appendChild($device); } // Render preferences $preferences = $app->getPreferences(); foreach ($preferences as $p) { $preference = $configXml->createElement('preference'); $preference->setAttribute('name', $p['key']); $preference->setAttribute('value', $p['value']); $widget->appendChild($preference); } // Define app icon for each platform. $icons = array(array("src" => "icon.png"), array("src" => "res/icons/android/iconA36.png", "gap:platform" => "android", "gap:density" => "ldpi"), array("src" => "res/icons/android/iconA48.png", "gap:platform" => "android", "gap:density" => "mdpi"), array("src" => "res/icons/android/iconA72.png", "gap:platform" => "android", "gap:density" => "hdpi"), array("src" => "res/icons/android/iconA96.png", "gap:platform" => "android", "gap:density" => "xhdpi"), array("src" => "res/icons/ios/iconI57.png", "gap:platform" => "ios", "width" => "57", "height" => "57"), array("src" => "res/icons/ios/iconI72.png", "gap:platform" => "ios", "width" => "72", "height" => "72"), array("src" => "res/icons/ios/iconI114.png", "gap:platform" => "ios", "width" => "114", "height" => "114"), array("src" => "res/icons/ios/iconI120.png", "gap:platform" => "ios", "width" => "120", "height" => "120"), array("src" => "res/icons/ios/iconI76.png", "gap:platform" => "ios", "width" => "76", "height" => "76"), array("src" => "res/icons/ios/iconI152.png", "gap:platform" => "ios", "width" => "152", "height" => "152"), array("src" => "res/icons/ios/iconI144.png", "gap:platform" => "ios", "width" => "144", "height" => "144")); foreach ($icons as $i) { $icon = $configXml->createElement('icon'); foreach ($i as $k => $v) { $icon->setAttribute($k, $v); } $widget->appendChild($icon); } // Define app splash screen for each platform. $splashScreen = array(array("src" => $this->getAndroidSplashscreenPath("splashscreenA320x436"), "gap:platform" => "android", "gap:density" => "ldpi"), array("src" => $this->getAndroidSplashscreenPath("splashscreenA320x470"), "gap:platform" => "android", "gap:density" => "mdpi"), array("src" => $this->getAndroidSplashscreenPath("splashscreenA640x480"), "gap:platform" => "android", "gap:density" => "hdpi"), array("src" => $this->getAndroidSplashscreenPath("splashscreenA960x720"), "gap:platform" => "android", "gap:density" => "xhdpi"), array("src" => "res/splashscreens/ios/splashscreenI320x480.png", "gap:platform" => "ios", "width" => "320", "height" => "480"), array("src" => "res/splashscreens/ios/splashscreenI640x960.png", "gap:platform" => "ios", "width" => "640", "height" => "960"), array("src" => "res/splashscreens/ios/splashscreenI640x1136.png", "gap:platform" => "ios", "width" => "640", "height" => "1136"), array("src" => "res/splashscreens/ios/splashscreenI1024x768.png", "gap:platform" => "ios", "width" => "1024", "height" => "768"), array("src" => "res/splashscreens/ios/splashscreenI768x1024.png", "gap:platform" => "ios", "width" => "768", "height" => "1024"), array("src" => "res/splashscreens/ios/splashscreenI2048x1536.png", "gap:platform" => "ios", "width" => "2048", "height" => "1536"), array("src" => "res/splashscreens/ios/splashscreenI1536x2048.png", "gap:platform" => "ios", "width" => "1536", "height" => "2048"), array("src" => "res/splashscreens/ios/splashscreenI750x1334.png", "gap:platform" => "ios", "width" => "750", "height" => "1334"), array("src" => "res/splashscreens/ios/splashscreenI1242x2208.png", "gap:platform" => "ios", "width" => "1242", "height" => "2208"), array("src" => "res/splashscreens/ios/splashscreenI2208x1242.png", "gap:platform" => "ios", "width" => "2208", "height" => "1242")); foreach ($splashScreen as $asplash) { $splash = $configXml->createElement('gap:splash'); foreach ($asplash as $k => $v) { $splash->setAttribute($k, $v); } $widget->appendChild($splash); } // Define access to external domains. // <access), - a blank access tag denies access to all external resources. // <access origin="*"), - a wildcard access tag allows access to all external resource. // Otherwise, you can specify specific domains: // <access origin="http://phonegap.com"), - allow any secure requests to http://phonegap.com/ // <access origin="http://phonegap.com" subdomains="true"), - same as above, but including subdomains, such as http://build.phonegap.com/ // <access origin="http://phonegap.com" browserOnly="true"), - only allows http://phonegap.com to be opened by the child browser. $access = array(array('origin' => '*')); foreach ($access as $a) { $accesses = $configXml->createElement('access'); foreach ($a as $k => $v) { $accesses->setAttribute($k, $v); } $widget->appendChild($accesses); } $configXml->save($this->getExportAbsolutePath() . DIRECTORY_SEPARATOR . 'simulator' . DIRECTORY_SEPARATOR . 'www' . DIRECTORY_SEPARATOR . 'config.xml'); /** * Duplicate Export for cordova and phonegapbuild */ //For ios FilesUtil::copyFolder($this->getExportAbsolutePath() . '/simulator/www', $this->getExportAbsolutePath() . '/cordova/www'); copy($this->getExportAbsolutePath() . '/cordova/www/config.xml', $this->getExportAbsolutePath() . '/cordova/config/config.xml'); unlink($this->getExportAbsolutePath() . '/cordova/www/config.xml'); //For phonegapbuild FilesUtil::copyFolder($this->getExportAbsolutePath() . '/simulator/www', $this->getExportAbsolutePath() . '/phonegapbuild/www'); copy(TemplateUtil::getAbsolutePath() . '/main-header/ios/cordova.js', $this->getExportAbsolutePath() . '/simulator/www/cordova.js'); //Generate ZIP files for all //Cordova ZipUtil::ZipFolder($this->getExportAbsolutePath() . '/cordova/www', $this->getExportAbsolutePath() . '/cordova/export.zip'); ZipUtil::ZipFolder($this->getExportAbsolutePath() . '/cordova/config', $this->getExportAbsolutePath() . '/cordova/config.zip'); //Phonegapbuild ZipUtil::ZipFolder($this->getExportAbsolutePath() . '/phonegapbuild/www', $this->getExportAbsolutePath() . '/phonegapbuild/export.zip'); }