PageRenderTime 35ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/core/controllers/SitesManager.php

http://kancms.googlecode.com/
PHP | 455 lines | 284 code | 79 blank | 92 comment | 28 complexity | 7cce079da2960d00b0d944ede927745f MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined('CORE_PATH') ) die("Access Denied");
  3. kan_import( array('PageManager','ThemeManager') );
  4. class SitesManager extends Manager {
  5. protected $models = array("Site","Theme","Image","Banner","Advert","DownloadCategory","MediaCategory","FeedbackCategory","Spotlight");
  6. protected $helpers = array("HTML","Cache");
  7. public function __construct() {
  8. parent::__construct('sites');
  9. $this->setItemClassName('Site');
  10. }
  11. public function getAllSites() {
  12. return $this->getItems(0, NULL, array(
  13. 'order' => array("SiteName ASC")
  14. ));
  15. }
  16. public function getSites($start = 0, $limit = NULL, $options = NULL) {
  17. $dOptions = array( 'order' => array("SiteName ASC") );
  18. if($options != NULL) {
  19. $dOptions = array_merge($dOptions, $options);
  20. }
  21. return $this->getItems($start, $limit, $dOptions);
  22. }
  23. public function getSite($siteid) {
  24. $site = $this->Site->findFirst(array(
  25. 'filter' => array(
  26. 'OR' => array(
  27. 'id' => $siteid,
  28. 'SiteIdentifier' => $siteid
  29. )
  30. )
  31. ));
  32. // initialize the theme object
  33. if( $site ) {
  34. $theme = $site->getTheme();
  35. }
  36. return $site;
  37. }
  38. public function findSite($options = NULL) {
  39. return $this->Site->find($options);
  40. }
  41. public function createNewSite() {
  42. if (!defined("IN_CMS") || !IN_CMS) {
  43. return;
  44. }
  45. if (!isset($_POST['SiteIdentifier'])) {
  46. return false;
  47. }
  48. $site = new Site(array(
  49. 'SiteIdentifier' => $_POST['SiteIdentifier'],
  50. 'SiteType' => $_POST['SiteType'],
  51. 'SiteName' => $_POST['SiteName'],
  52. 'SiteDomain' => $_POST['SiteDomain'],
  53. 'SiteEmail' => $_POST['SiteEmail'],
  54. 'SiteAddress' => $_POST['SiteAddress'],
  55. 'SitePhone' => $_POST['SitePhone'],
  56. 'SiteDescription' => $_POST['SiteDescription'],
  57. 'SiteCMSLogo' => $_POST['SiteCMSLogo'],
  58. 'SiteLogo' => $_POST['SiteLogo'],
  59. 'SiteSlogan' => $_POST['SiteSlogan'],
  60. 'SiteKeywords' => $_POST['SiteKeywords'],
  61. 'SiteHomePageText' => $_POST['SiteHomePageText'],
  62. 'SiteFooterText' => $_POST['SiteFooterText'],
  63. 'SiteThemeID' => $_POST['SiteThemeID'],
  64. 'SiteThemeCSSID' => $_POST['SiteThemeCSSID'],
  65. 'SiteFavicon' => $_POST['SiteFavicon']
  66. ));
  67. $site->save();
  68. $newSiteID = $site->getId();
  69. $feedbackCategory = new FeedbackCategory(array(
  70. 'SiteID' => $site->getId(),
  71. 'CategoryName' => 'General',
  72. 'CategoryTag' => 'general',
  73. 'CategoryDesc' => 'General Feedback',
  74. 'DateCreated' => date('Y-m-d')
  75. ));
  76. $feedbackCategory->save();
  77. // article categories
  78. $articleCategory = new ArticleCategory(array(
  79. 'SiteID' => $site->getId(),
  80. 'CategoryName' => "News",
  81. "CategoryAlias" => "news",
  82. "CategoryDescription" => "News Articles"
  83. ));
  84. $articleCategory->save();
  85. $articleCategory = new ArticleCategory(array(
  86. 'SiteID' => $site->getId(),
  87. 'CategoryName' => "Blog",
  88. "CategoryAlias" => "blog",
  89. "CategoryDescription" => "Blog Articles"
  90. ));
  91. $articleCategory->save();
  92. // download categories
  93. $downloadCategory = new DownloadCategory(array(
  94. 'SiteID' => $site->getId(),
  95. 'CategoryName' => "General",
  96. "CategoryAlias" => "general",
  97. "CategoryDescription" => "General Downloads"
  98. ));
  99. $articleCategory->save();
  100. // event categories
  101. $eventCategory = new EventCategory(array(
  102. 'SiteID' => $site->getId(),
  103. 'Category' => "General",
  104. "CategoryAlias" => "general",
  105. "CategoryDescription" => "General Events"
  106. ));
  107. $eventCategory->save();
  108. // quick links categories
  109. $linkCategory = new LinkCategory(array(
  110. 'SiteID' => $site->getId(),
  111. 'CategoryName' => "General",
  112. "CategoryDescription" => "General Events"
  113. ));
  114. $linkCategory->save();
  115. // return true to indicate all executed successfully
  116. return true;
  117. }
  118. public function updateSite() {
  119. if (!defined("IN_CMS") || !IN_CMS) {
  120. return;
  121. }
  122. $site = new Site(array(
  123. 'id' => $_POST['id'],
  124. 'SiteIdentifier' => $_POST['SiteIdentifier'],
  125. 'SiteType' => $_POST['SiteType'],
  126. 'SiteName' => $_POST['SiteName'],
  127. 'SiteDomain' => $_POST['SiteDomain'],
  128. 'SiteEmail' => $_POST['SiteEmail'],
  129. 'SiteAddress' => $_POST['SiteAddress'],
  130. 'SitePhone' => $_POST['SitePhone'],
  131. 'SiteDescription' => $_POST['SiteDescription'],
  132. 'SiteCMSLogo' => $_POST['SiteCMSLogo'],
  133. 'SiteLogo' => $_POST['SiteLogo'],
  134. 'SiteSlogan' => $_POST['SiteSlogan'],
  135. 'SiteKeywords' => $_POST['SiteKeywords'],
  136. 'SiteHomePageText' => $_POST['SiteHomePageText'],
  137. 'SiteFooterText' => $_POST['SiteFooterText'],
  138. 'SiteThemeID' => $_POST['SiteThemeID'],
  139. 'SiteThemeCSSID' => $_POST['SiteThemeCSSID'],
  140. 'SiteFavicon' => $_POST['SiteFavicon']
  141. ));
  142. $site->save();
  143. $this->Cache->clearCache($_POST['SiteIdentifier']);
  144. }
  145. public function updateSiteHomePageContent() {
  146. if (!defined("IN_CMS") || !IN_CMS) {
  147. return;
  148. }
  149. $site = new Site( array(
  150. 'id' => $_POST['id'],
  151. 'SiteHomePageText' => $_POST['SiteHomePageText']
  152. ));
  153. $site->save();
  154. return $site;
  155. }
  156. public function updateSiteFooterContent() {
  157. if (!defined("IN_CMS") || !IN_CMS) {
  158. return;
  159. }
  160. $site = new Site( array(
  161. 'id' => $_POST['id'],
  162. 'SiteFooterText' => $_POST['SiteFooterText']
  163. ));
  164. $site->save();
  165. return $site;
  166. }
  167. /**
  168. * Clears the Cache of the site with the specified ID
  169. *
  170. * @param int $siteid the database record ID of the site
  171. */
  172. public function clearSiteCache($siteid) {
  173. if( !is_array($siteid) ) {
  174. $siteid = array($siteid);
  175. }
  176. foreach($siteid as $id) {
  177. // get a Site object to work with
  178. $site = $this->getSite($id);
  179. // clear the system cache
  180. if( $site ) $this->Cache->clearCache($site->getSiteIdentifier());
  181. }
  182. }
  183. /**
  184. * Deletes a Site from the KAN system and clears the site cache as well
  185. *
  186. * @param int $siteid the database record ID of the site
  187. */
  188. public function deleteSite($siteid) {
  189. // get a Site object to work with
  190. $site = $this->getSite($siteid);
  191. // clear the system cache
  192. $this->Cache->clearCache($site->getSiteIdentifier());
  193. // before we proceed to delete the site information, we first need
  194. // to clear all related documents from the server first
  195. // first images
  196. $this->Image->deleteAll(array('SiteID' => $siteid));
  197. // then banners
  198. //$this->deleteSiteBanners($site->getSiteID());
  199. // then adverts
  200. //$this->deleteSiteAdverts($site->getSiteID());
  201. // then media
  202. $this->MediaCategory->deleteAll(array('SiteID' => $siteid));
  203. // then spotlights
  204. $this->Spotlight->deleteAll(array('SiteID' => $siteid));
  205. // then downloads
  206. $this->DownloadCategory->deleteAll(array('SiteID' => $siteid));
  207. // delete the site itself
  208. $site->delete();
  209. }
  210. public function getSiteLinksArray($siteid) {
  211. $site = $this->getSite($siteid);
  212. // generate links for the CMS section of the site
  213. $cms_quick_links = array(
  214. 'group_title' => 'CMS Quick Links',
  215. 'links' => array(
  216. array('title' => 'CMS Home', 'href' => '../pages/index.php?goto=../cms/index.php')
  217. )
  218. );
  219. // generate the links to all the KAN sites
  220. $sites = $this->getAllSites();
  221. $links_array = array();
  222. for ($i = 0; $i < count($sites); $i++) {
  223. $siteEl = $sites[$i];
  224. array_push($links_array, array('title' => $siteEl->getSiteName(), 'href' => $siteEl->getHomePageURL()));
  225. }
  226. // create the data group for the sites links
  227. $cms_sites = array(
  228. 'group_title' => 'Sites',
  229. 'links' => $links_array
  230. );
  231. // generate links for common kan areas
  232. $tag = $site->getSiteIdentifier();
  233. $common_kan_links = array(
  234. 'group_title' => 'Predefined Links',
  235. 'links' => array(
  236. array('title' => 'Home Page', 'href' => $site->getPageURL('index')),
  237. array('title' => 'Articles', 'href' => $site->getPageURL('articles')),
  238. array('title' => 'Upcoming Events', 'href' => $site->getPageURL('events')),
  239. array('title' => 'Downloads', 'href' => $site->getPageURL('downloads')),
  240. array('title' => 'Media', 'href' => $site->getPageURL('media')),
  241. array('title' => 'Gallery', 'href' => $site->getPageURL('gallery'))
  242. )
  243. );
  244. // generate the links used in the Quick Links Section
  245. $ql_array = array();
  246. $lm = new LinksManager();
  247. $links = $lm->getLinks();
  248. foreach($links as $link) {
  249. array_push($ql_array, array('title' => $link->getName(), 'href' => $link->getURL()));
  250. }
  251. $site_quick_links = array(
  252. 'group_title' => 'Quick Links',
  253. 'links' => $ql_array
  254. );
  255. // add the article category links
  256. $arts = new ArticlesManager();
  257. $categories = $arts->getArticleCategories();
  258. $article_cats = array();
  259. foreach ($categories as $cat) {
  260. array_push($article_cats, array('title' => $cat->getCategoryName(), 'href' => $cat->getURL()));
  261. }
  262. $cms_articles = array(
  263. 'group_title' => 'Article Categories',
  264. 'links' => $article_cats
  265. );
  266. // add the pages links
  267. $pm = new PageManager();
  268. $nodes = $pm->getAdjacencyTree()->getFullNodes(
  269. array("id", "SiteID", "PageName", "PageNameAlias", "PageType", "PagePath", "RedirectURL", "Published")
  270. );
  271. $cms_page_list = array();
  272. $this->processPages($cms_page_list, $nodes);
  273. $cms_pages = array(
  274. 'group_title' => 'Site Pages',
  275. 'links' => $cms_page_list
  276. );
  277. // create the final list of link arrays in the preferred order
  278. $link_array = array(
  279. $cms_sites,
  280. //$current_site,
  281. $common_kan_links,
  282. $cms_pages,
  283. $cms_articles,
  284. $site_quick_links,
  285. $cms_quick_links
  286. );
  287. return $link_array;
  288. }
  289. private function processPages(&$pages_array, $nodes, $depth = 0) {
  290. $currentDepth = $depth;
  291. foreach ($nodes as $data) {
  292. $page = new Page($data);
  293. $name = str_repeat("&nbsp; ", $depth) . $page->getName();
  294. $url = $page->getURL();
  295. array_push($pages_array, array('title' => $name, 'href' => $url));
  296. if (isset($data['children'])) {
  297. $this->processPages($pages_array, $data['children'], ++$depth);
  298. }
  299. $depth = $currentDepth;
  300. }
  301. }
  302. /**
  303. * Sets the site with the supplied database record ID as the default site in the systetem
  304. */
  305. public function setSiteAsDefault($siteid) {
  306. $this->Site->updateAll( array('SiteType' => 'sub'));
  307. $this->Site->updateAll( array('SiteType' => 'main'), array('id' => $siteid));
  308. }
  309. /**
  310. * Sets the site with the supplied database record ID as the default site in the systetem
  311. *
  312. * @param mixed $siteid array or int representing the Sites to be updated
  313. * @param int $status 1 or 0 representing the active state
  314. */
  315. public function setSiteAsActive($siteid, $status) {
  316. if (!defined("IN_CMS") || !IN_CMS) {
  317. return;
  318. }
  319. $this->Site->updateAll(array('SiteActive' => $status), array("id IN" => implode(",", $siteid)));
  320. }
  321. private function deleteFile($file) {
  322. if ($file != "none" && $file != "" && $file != '.' && $file != '..') {
  323. if (file_exists($file)) {
  324. try {
  325. unlink($file);
  326. } catch (Exception $e) {
  327. echo $e;
  328. }
  329. }
  330. }
  331. }
  332. /*private function deleteSiteBanners($siteID) {
  333. $db = $this->getDatabase();
  334. $selectSQL = sprintf("SELECT * FROM banner_images WHERE SiteID = %s", $db->sanitizeInput($siteID, "int"));
  335. $imageResult = mysql_query($selectSQL) or die(mysql_error());
  336. // if there are no images to delete, simply exit the procedure
  337. if (mysql_num_rows($imageResult) == 0) {
  338. mysql_free_result($imageResult);
  339. return;
  340. }
  341. while ($images = mysql_fetch_assoc($imageResult)) {
  342. $media = $images['BannerPath'];
  343. $this->deleteFile("../" . $media);
  344. }
  345. mysql_free_result($imageResult);
  346. }
  347. private function deleteSiteAdverts($siteID) {
  348. $db = $this->getDatabase();
  349. $selectSQL = sprintf("SELECT * FROM advert_images WHERE SiteID = %s", $db->sanitizeInput($siteID, "int"));
  350. $imageResult = mysql_query($selectSQL) or die(mysql_error());
  351. // if there are no images to delete, simply exit the procedure
  352. if (mysql_num_rows($imageResult) == 0) {
  353. mysql_free_result($imageResult);
  354. return;
  355. }
  356. while ($images = mysql_fetch_assoc($imageResult)) {
  357. $media = $images['AdvertPath'];
  358. $this->deleteFile("../" . $media);
  359. }
  360. mysql_free_result($imageResult);
  361. }*/
  362. }
  363. ?>