Wtx ~ Wt Extension Library
WtxLib
ViewSummary.cpp
1 /**************************************************************************
2 ###########################################################################
3 ##
4 ## $SHOWOFFDB_BEGIN_LICENSE$
5 ## Copyright (C) 2011 Lorimark Solutions, LLC and/or its subsidiary(-ies).
6 ## All rights reserved.
7 ## Contact: Lorimark Solutions, LLC (info@showoff-db.org)
8 ##
9 ## This file is part of the Showoff Database Application Framework.
10 ##
11 ## Commercial Usage
12 ## Licensees holding valid ShowoffDB Commercial licenses may use this file in
13 ## accordance with the ShowoffDB Commercial License Agreement provided with the
14 ## Software or, alternatively, in accordance with the terms contained in
15 ## a written agreement between you and Lorimark Solutions, LLC.
16 ##
17 ## GNU Lesser General Public License Usage
18 ## Alternatively, this file may be used under the terms of the GNU Lesser
19 ## General Public License version 2.1 as published by the Free Software
20 ## Foundation and appearing in the file LICENSE.LGPL included in the
21 ## packaging of this file. Please review the following information to
22 ## ensure the GNU Lesser General Public License version 2.1 requirements
23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ##
25 ## In addition, as a special exception, Lorimark Solutions, LLC gives
26 ## you certain additional rights. These rights are described in the
27 ## Lorimark Solutions, LLC ShowoffDB LGPL Exception version 1.0, included in
28 ## the file LGPL_EXCEPTION.txt in this package.
29 ##
30 ## GNU General Public License Usage
31 ## Alternatively, this file may be used under the terms of the GNU
32 ## General Public License version 3.0 as published by the Free Software
33 ## Foundation and appearing in the file LICENSE.GPL included in the
34 ## packaging of this file. Please review the following information to
35 ## ensure the GNU General Public License version 3.0 requirements will be
36 ## met: http://www.gnu.org/copyleft/gpl.html.
37 ##
38 ## If you have questions regarding the use of this file, please contact
39 ## Lorimark Solutions, LLC at info@showoff-db.org.
40 ## $SHOWOFFDB_END_LICENSE$
41 ##
42 #############################################################################
43 ****************************************************************************/
44 
45 
46 #include <Wt/WAnchor.h>
47 #include <Wt/WApplication.h>
48 #include <Wt/WLink.h>
49 #include <Wt/WStackedWidget.h>
50 #include <Wt/WTable.h>
51 #include <Wt/WTemplate.h>
52 #include <Wt/WText.h>
53 
54 #include "Item.h"
55 #include "ViewSummary.h"
56 #include "ViewDetail.h"
57 
58 #define BLOG_ENTRY_URL "/blog/entry/"
59 
60 #define ID_ROW_ITEM "Wtx.Web.BlogEntryViewSummary.RowItem"
61 class RowItem
62 : public Wt::WContainerWidget
63 {
64  public:
65 
66  RowItem( const Wt::Dbo::ptr<Wtx::Web::BlogEntry::Item> & item )
67  : Wt::WContainerWidget()
68  {
69  /*
70  ** Try to load the template. This can be overwritten
71  ** by the application, but if the template is not found
72  ** then just default to a suitable alternative.
73  **
74  */
75  auto tr = Wt::WString::tr(ID_ROW_ITEM);
76  if( tr.toUTF8().find("??") != std::string::npos )
77  {
78  tr =
79  Wt::WString
80  (
81  "<div>"
82  " <strong><big>${title}</big></strong><br />"
83  " ${<if-date>}"
84  " <small>Posted on ${date}</small><br />"
85  " ${</if-date>}"
86  " ${shortBody}<br />"
87  " ${thumbnail}"
88  " <br />"
89  " <br />"
90  "</div>"
91  );
92  }
93 
94  auto templt = addNew<Wt::WTemplate>( tr );
95  templt-> setInternalPathEncoding(true);
96 
97  auto url = item-> keyField().value();
98 
99  auto link = Wt::WLink( Wt::LinkType::InternalPath, BLOG_ENTRY_URL + url );
100  auto anchor = std::make_unique<Wt::WAnchor>( link, item-> keyField().value() );
101 
102  templt-> bindWidget<Wt::WAnchor>( "title", std::move(anchor) );
103  templt-> bindNew<Wt::WText>( "shortBody", item-> summary.value() );
104 
105  templt-> setCondition( "if-date", item-> datePosted.value() != "" );
106  templt-> bindNew<Wt::WText>( "date", item-> datePosted.value() );
107 
108  if( item-> thumbnail.value() == "" )
109  templt-> bindEmpty("thumbnail");
110  else
111  templt-> bindString
112  (
113  "thumbnail",
114  Wt::WString::tr("kbw.thumbnail")
115  .arg( item-> thumbnail.value() )
116  );
117  }
118 
119  private:
120 
121 };
122 
123 Wtx::Web::BlogEntry::ViewSummary::ViewSummary( Wtx::Dbo::Session * s )
124 : Wt::WContainerWidget(),
125  m_session(s)
126 {
127  m_stack = addNew<Wt::WStackedWidget>();
128 
129  auto table = m_stack-> addNew<Wt::WTable>();
130 
131  Wt::Dbo::Transaction t(*session());
132  auto blogEntries =
133  session()-> find<Wtx::Web::BlogEntry::Item>()
134  .where( "\"isActive\" = 1 AND id > 1" )
135  .orderBy( "\"datePosted\" DESC" )
136  .resultList()
137  ;
138 
139  for( auto blogEntry : blogEntries )
140  table-> elementAt( table-> rowCount(), 0 )->
141  addWidget
142  (
143  std::make_unique<RowItem>(blogEntry)
144  );
145 
146 
147  wApp-> internalPathChanged().connect( this, &ViewSummary::on_internalPathChanged );
148  on_internalPathChanged();
149 
150 }
151 
152 void Wtx::Web::BlogEntry::ViewSummary::on_internalPathChanged()
153 {
154  auto ip = wApp-> internalPath();
155 
156  /*
157  ** If we are just loading the blog summary, then switch
158  ** back to the stack that has the main blog summary list.
159  **
160  */
161  if( ip == "/blog" )
162  {
163  if( m_stack-> currentIndex() == 0 )
164  return;
165 
166  /*
167  ** to go back to the main blog summary list, we just
168  ** remove any subsequent stack pages, and then the
169  ** stack will go back to the zero index
170  **
171  */
172  m_stack-> removeWidget( m_stack-> currentWidget() );
173  return;
174  }
175 
176  /*
177  ** if we are attempting to load a blog page then do that
178  ** now.
179  **
180  */
181  if( ip.find("/blog/entry/") != std::string::npos )
182  {
183  if( m_stack-> currentIndex() == 1 )
184  return;
185 
186  auto page = ip.substr( 12 );
187 
188  Wt::Dbo::Transaction t(*session());
189  auto blogEntry =
190  session()-> find<Wtx::Web::BlogEntry::Item>()
191  .where( "\"keyField\" = ?" )
192  .bind( page )
193  .resultValue()
194  ;
195 
196  if( !blogEntry )
197  {
198  m_stack-> addWidget( std::make_unique<Wt::WText>( Wt::WString("oh oh, I could not find the article: '{1}'").arg( page ) ) );
199  }
200  else
201  {
202  m_stack-> addWidget( std::make_unique<Wtx::Web::BlogEntry::ViewDetail>( blogEntry ) );
203  }
204 
205  m_stack-> setCurrentIndex( 1 );
206  }
207 
208 }
209 
210