Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsTemplate.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-Today Guy Deleeuw
3  *
4  * See the LICENSE file for terms of use.
5 */
6 
7 #include <boost/algorithm/string/replace.hpp>
8 
10 #include <User/WsUser.h>
11 
12 #include <Wt/WTheme>
13 
14 #include <gdcore/gdCore.h>
15 
16 #include "Main/WsLogo.h"
17 #include "Main/WsSiteMap.h"
18 #include "Main/WsSearchObject.h"
19 #include "Main/WsTopBanner.h"
20 #include "Main/WsBottomBanner.h"
23 #include "Main/WsContent.h"
24 
25 #include "WsTemplate.h"
26 
27 // http://www.johndcook.com/cpp_regex.html
28 //#include <regex>
29 
30 using namespace Wt;
31 
32 WsTemplate::WsTemplate(const std::string& templateFile, const std::string& initialPath, WContainerWidget* parent)
33  : WTemplate(parent), m_sInitialPath(initialPath)
34 {
35  propagateSetEnabled(true);
36  if ( WString::tr("byObjectStyleSheet").narrow() == "true" )
37  wApp->useStyleSheet(wApp->theme()->resourcesUrl() + "wittyshare/Css/WsTemplate.css");
38  addStyleClass("WsTemplate");
39  std::string tFile = matchTemplate(templateFile);
40  std::string fileContent;
41  if ( !gdcore_file_content2string(tFile.c_str(), fileContent) ) {
42  wApp->log("ERROR") << "WsTemplate::WsTemplate - cannot load " << tFile;
43  return;
44  }
45  setTemplateText(fileContent);
46 }
47 
49 { }
50 
51 std::string WsTemplate::matchTemplate(const std::string& templateFile)
52 {
53  WsUser* pUser = WsApp->wsUser();
54  std::string curPath = wApp->internalPath();
55  boost::algorithm::replace_all(curPath, "&amp;", "&");
56  std::string tFile = pUser->getRootPath() + curPath;
57  boost::filesystem::path current(tFile);
58  if ( !boost::filesystem::is_directory(current) )
59  current = current.parent_path();
60  std::string sCurrent;
61  while (1) {
62  sCurrent = current.string() + GlobalConfig::PathToTemplates + templateFile;
63  if ( boost::filesystem::exists(sCurrent) ) return sCurrent;
64  if ( pUser->getRootPath() == current.string() ) return templateFile;
65  current = current.parent_path();
66  }
67 }
68 
69 WWidget* WsTemplate::resolveWidget(const std::string& varName)
70 {
71  // if ( fileContent.find("${WsTopBanner}") != string::npos ) {
72  if ( varName == "WsLogo" ) {
73  WsLogo* pLogo = new WsLogo();
74  if (pLogo)
75  bindWidget(varName, pLogo);
76  return pLogo;
77  }
78  if ( varName == "WsSiteMap" ) {
79  WsSiteMap* pSiteMap = new WsSiteMap();
80  if (pSiteMap)
81  bindWidget(varName, pSiteMap);
82  return pSiteMap;
83  }
84  if ( varName == "WsSearchObject" ) {
85  WsSearchObject* pSearch = new WsSearchObject();
86  if (pSearch)
87  bindWidget(varName, pSearch);
88  return pSearch;
89  }
90  if ( varName == "WsTopBanner" ) {
91  WsTopBanner* pTB = new WsTopBanner();
92  if (pTB)
93  bindWidget(varName, pTB);
94  return pTB;
95  }
96  if ( varName == "WsContentButtonsBar" ) {
98  if (pCBB)
99  bindWidget(varName, pCBB);
100  return pCBB;
101  }
102  if ( varName == "WsFunctionnalities" ) {
103  WsFunctionnalities* pFunc = new WsFunctionnalities();
104  if (pFunc)
105  bindWidget(varName, pFunc);
106  return pFunc;
107  }
108  if ( varName == "WsBottomBanner" ) {
109  WsBottomBanner* pBB = new WsBottomBanner(WsLayoutProperties::instance()->get("global", "bottombanner_text", ""));
110  if (pBB)
111  bindWidget(varName, pBB);
112  return pBB;
113  }
114  return WTemplate::resolveWidget(varName);
115 }
116 
117 void WsTemplate::resolveString(const std::string& varName, const std::vector< WString >& args, std::ostream& result)
118 {
119  // TODO : remove when Koen adapt resolveWidget with a list of arguments.
120  // WsContent does exist only one time
121  if ( varName == "WsContent" ) {
122  WsContent* pContent = new WsContent();
123  WsApp->setContent(pContent);
124  std::string overflow;
125  std::string sHomePage;
126  bool bUseLayout = false;
127  for (int count = 0; count < args.size(); ++count) {
128  std::string attr;
129  std::string val;
130  gdcore_string_split(args[count].toUTF8(), attr, val, '=');
131  if ( attr == "useLayout" && val == "true" )
132  bUseLayout = true;
133  if ( attr == "homePage" )
134  sHomePage = val;
135  if ( attr == "overflow" )
136  overflow = val;
137  }
138  //if ( bUseLayout ) { }
139  if ( overflow == "auto" )
140  pContent->setOverflow(WContainerWidget::OverflowAuto);
141  if ( m_sInitialPath.size() > 0 ) sHomePage = m_sInitialPath;
142  if (pContent)
143  bindWidget(varName, pContent);
144  if ( sHomePage.size() > 0 )
145  pContent->setPath(sHomePage);
146  return WTemplate::resolveString(varName, args, result);
147  }
148  //if ( varName == "WsTemplate" ) {
149  std::string moduleName = "WsTemplate";
150  if ( varName.compare(0, moduleName.size(), moduleName) == 0 ) {
151  std::string templateFile;
152  std::string templateClass;
153  for (int count = 0; count < args.size(); ++count) {
154  std::string attr;
155  std::string val;
156  gdcore_string_split(args[count].toUTF8(), attr, val, '=');
157  if ( attr == "templateFile" )
158  templateFile = val;
159  if ( attr == "templateClass" )
160  templateClass = val;
161  }
162  if ( templateFile.size() > 0 ) {
163  WsTemplate* pTemplate = new WsTemplate(templateFile, m_sInitialPath);
164  if ( templateClass.size() > 0 )
165  pTemplate->addStyleClass(templateClass);
166  if (pTemplate)
167  bindWidget(varName, pTemplate);
168  }
169  return WTemplate::resolveString(varName, args, result);
170  }
171  WsUser* pUser = WsApp->wsUser();
172  std::string m_sDocumentRoot = pUser->getRootPath(); // /var/www/demo_site
173  std::string m_httpDocumentRoot = gdWApp->getParameter("DOCUMENT_ROOT", "/var/www");
174  std::string m_sRelativeDocumentRoot = m_sDocumentRoot;
175  boost::algorithm::replace_first(m_sRelativeDocumentRoot, m_httpDocumentRoot, "");
176  std::string sysPath(m_sDocumentRoot + wApp->internalPath());
177  for (int iModule = 0; iModule < WsApp->WsModules().modules().size(); ++iModule) {
178  WsModule* curModule = WsApp->WsModules().modules()[iModule]->module;
179  // if WsModMenu || WsModMenuImages crash, not possible to have 2 objects identical.
180  // I add a _ in place of by example WsModAnchor_Disclaimer and locate this _ after I split the string and compare the module name
181  std::string right;
182  gdcore_string_split(varName, moduleName, right, '_');
183  if ( curModule->moduleName() != moduleName ) continue;
184  // TODO : Verifier si c'est compatible dans tous les cas de figures
185  curModule->setSysPath(sysPath);
186  curModule->setDiffPath(m_sRelativeDocumentRoot);
187  WWidget* w = curModule->createContents();
188  for (int count = 0; count < args.size(); ++count) {
189  std::string attr;
190  std::string val;
191  gdcore_string_split(args[count].toUTF8(), attr, val, '=');
192  WsOptions* wOpt = dynamic_cast<WsOptions*>(w);
193  wOpt->setOption(attr, val);
194  }
195  if (w)
196  bindWidget(varName, w);
197  break;
198  }
199  return WTemplate::resolveString(varName, args, result);
200 }
WsTemplate(const std::string &templateFile, const std::string &initialPath=std::string(), Wt::WContainerWidget *parent=0)
Definition: WsTemplate.cpp:32
Wt::WWidget * resolveWidget(const std::string &varName)
Definition: WsTemplate.cpp:69
std::string m_sInitialPath
Definition: WsTemplate.h:27
void setPath(std::string newPath)
Definition: WsContent.cpp:191
void setDiffPath(const std::string &diffPath)
Set/Get the difference between the web server (example Apache /var/www) root path and the wittishare ...
Definition: WsOption.cpp:184
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
Definition: WsLogo.h:13
const std::string PathToTemplates
virtual Wt::WWidget * createContents(Wt::WContainerWidget *parent=0) const =0
Create the contents.
const std::string & moduleName() const
Definition: WsOption.cpp:100
void setOption(const std::string &attribute, boost::any value)
Set an options if previously set, update the value.
Definition: WsOption.cpp:47
const std::string getRootPath()
return the root path of the filesystem tree, example : /var/www/demo_site
Definition: WsUser.cpp:55
#define WsApp
Define a shortcut to the application instance.
Definition: WsApplication.h:62
static WsLayoutProperties * instance()
Get the singleton instance.
void resolveString(const std::string &varName, const std::vector< Wt::WString > &args, std::ostream &result)
Definition: WsTemplate.cpp:117
void setSysPath(const std::string &sysPath)
Set/Get the system path (example the selected file /var/www/mysite/about/me.fhtml).
Definition: WsOption.cpp:174
std::string matchTemplate(const std::string &templateFile)
Definition: WsTemplate.cpp:51
User class.