PageRenderTime 29ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/applications/dashboard/settings/structure.php

https://github.com/DMeganoski/Gallery-Designer
PHP | 322 lines | 205 code | 47 blank | 70 comment | 21 complexity | 823720a7203698d7d9dd96c0d98c311e MD5 | raw file
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. // Use this file to construct tables and views necessary for your application.
  11. // There are some examples below to get you started.
  12. if (!isset($Drop))
  13. $Drop = FALSE;
  14. if (!isset($Explicit))
  15. $Explicit = TRUE;
  16. $SQL = Gdn::SQL();
  17. $Construct = Gdn::Structure();
  18. $Px = $Construct->DatabasePrefix();
  19. $Construct->Table('AddonType')
  20. ->PrimaryKey('AddonTypeID')
  21. ->Column('Label', 'varchar(50)')
  22. ->Column('Visible', 'tinyint(1)', '1')
  23. ->Set($Explicit, $Drop);
  24. $SQL->Replace('AddonType', array('Label' => 'Plugin', 'Visible' => '1'), array('AddonTypeID' => 1), TRUE);
  25. $SQL->Replace('AddonType', array('Label' => 'Theme', 'Visible' => '1'), array('AddonTypeID' => 2), TRUE);
  26. $SQL->Replace('AddonType', array('Label' => 'Style', 'Visible' => '0'), array('AddonTypeID' => 3), TRUE);
  27. $SQL->Replace('AddonType', array('Label' => 'Locale', 'Visible' => '1'), array('AddonTypeID' => 4), TRUE);
  28. $SQL->Replace('AddonType', array('Label' => 'Application', 'Visible' => '1'), array('AddonTypeID' => 5), TRUE);
  29. $SQL->Replace('AddonType', array('Label' => 'Core', 'Visible' => '1'), array('AddonTypeID' => 10), TRUE);
  30. $Construct->Table('Addon');
  31. $Description2Exists = $Construct->ColumnExists('Description2');
  32. $Construct->PrimaryKey('AddonID')
  33. ->Column('CurrentAddonVersionID', 'int', TRUE, 'key')
  34. ->Column('AddonKey', 'varchar(50)', NULL, 'index')
  35. ->Column('AddonTypeID', 'int', FALSE, 'key')
  36. ->Column('InsertUserID', 'int', FALSE, 'key')
  37. ->Column('UpdateUserID', 'int', TRUE)
  38. ->Column('Name', 'varchar(100)')
  39. ->Column('Icon', 'varchar(200)', TRUE)
  40. ->Column('Description', 'text', TRUE)
  41. ->Column('Description2', 'text', NULL)
  42. ->Column('Requirements', 'text', TRUE)
  43. ->Column('CountComments', 'int', '0')
  44. ->Column('CountDownloads', 'int', '0')
  45. ->Column('Visible', 'tinyint(1)', '1')
  46. ->Column('Vanilla2', 'tinyint(1)', '1')
  47. ->Column('DateInserted', 'datetime')
  48. ->Column('DateUpdated', 'datetime', TRUE)
  49. ->Column('Checked', 'tinyint(1)', '0')
  50. ->Set($Explicit, $Drop);
  51. if (!$Description2Exists) {
  52. $Construct->Query("update {$Px}Addon set Description2 = Description where Checked = 0");
  53. }
  54. /*
  55. $Construct->Table('AddonComment')
  56. ->PrimaryKey('AddonCommentID')
  57. ->Column('AddonID', 'int', FALSE, 'key')
  58. ->Column('InsertUserID', 'int', FALSE, 'key')
  59. ->Column('Body', 'text')
  60. ->Column('Format', 'varchar(20)', TRUE)
  61. ->Column('DateInserted', 'datetime')
  62. ->Set($Explicit, $Drop);
  63. */
  64. $Construct->Table('AddonVersion')
  65. ->PrimaryKey('AddonVersionID')
  66. ->Column('AddonID', 'int', FALSE, 'key')
  67. ->Column('File', 'varchar(200)', TRUE)
  68. ->Column('Version', 'varchar(20)')
  69. ->Column('TestedWith', 'text', NULL)
  70. ->Column('FileSize', 'int', NULL)
  71. ->Column('MD5', 'varchar(32)')
  72. ->Column('Notes', 'text', NULL)
  73. ->Column('Format', 'varchar(10)', 'Html')
  74. ->Column('InsertUserID', 'int', FALSE, 'key')
  75. ->Column('DateInserted', 'datetime')
  76. ->Column('DateReviewed', 'datetime', TRUE)
  77. ->Column('Checked', 'tinyint(1)', '0')
  78. ->Column('Deleted', 'tinyint(1)', '0')
  79. ->Set($Explicit, $Drop);
  80. $Construct->Table('AddonPicture')
  81. ->PrimaryKey('AddonPictureID')
  82. ->Column('AddonID', 'int', FALSE, 'key')
  83. ->Column('File', 'varchar(200)')
  84. ->Column('DateInserted', 'datetime')
  85. ->Set($Explicit, $Drop);
  86. $Construct->Table('Download')
  87. ->PrimaryKey('DownloadID')
  88. ->Column('AddonID', 'int', FALSE, 'key')
  89. ->Column('DateInserted', 'datetime')
  90. ->Column('RemoteIp', 'varchar(50)', TRUE)
  91. ->Set($Explicit, $Drop);
  92. $Construct->Table('UpdateCheckSource')
  93. ->PrimaryKey('SourceID')
  94. ->Column('Location', 'varchar(255)', TRUE)
  95. ->Column('DateInserted', 'datetime', TRUE)
  96. ->Column('RemoteIp', 'varchar(50)', TRUE)
  97. ->Set($Explicit, $Drop);
  98. $Construct->Table('UpdateCheck')
  99. ->PrimaryKey('UpdateCheckID')
  100. ->Column('SourceID', 'int', FALSE, 'key')
  101. ->Column('CountUsers', 'int', '0')
  102. ->Column('CountDiscussions', 'int', '0')
  103. ->Column('CountComments', 'int', '0')
  104. ->Column('CountConversations', 'int', '0')
  105. ->Column('CountConversationMessages', 'int', '0')
  106. ->Column('DateInserted', 'datetime')
  107. ->Column('RemoteIp', 'varchar(50)', TRUE)
  108. ->Set($Explicit, $Drop);
  109. // Need to use this table instead of linking directly with the Addon table
  110. // because we might not have all of the addons being checked for.
  111. $Construct->Table('UpdateAddon')
  112. ->PrimaryKey('UpdateAddonID')
  113. ->Column('AddonID', 'int', FALSE, 'key')
  114. ->Column('Name', 'varchar(255)', TRUE)
  115. ->Column('Type', 'varchar(255)', TRUE)
  116. ->Column('Version', 'varchar(255)', TRUE)
  117. ->Set($Explicit, $Drop);
  118. $Construct->Table('UpdateCheckAddon')
  119. ->Column('UpdateCheckID', 'int', FALSE, 'key')
  120. ->Column('UpdateAddonID', 'int', FALSE, 'key')
  121. ->Set($Explicit, $Drop);
  122. $PermissionModel = Gdn::PermissionModel();
  123. $PermissionModel->Database = $Database;
  124. $PermissionModel->SQL = $SQL;
  125. // Define some global addon permissions.
  126. $PermissionModel->Define(array(
  127. 'Addons.Addon.Add',
  128. 'Addons.Addon.Manage',
  129. 'Addons.Comments.Manage'
  130. ));
  131. if (isset($$PermissionTableExists) && $PermissionTableExists) {
  132. // Set the intial member permissions.
  133. $PermissionModel->Save(array(
  134. 'RoleID' => 8,
  135. 'Addons.Addon.Add' => 1
  136. ));
  137. // Set the initial administrator permissions.
  138. $PermissionModel->Save(array(
  139. 'RoleID' => 16,
  140. 'Addons.Addon.Add' => 1,
  141. 'Addons.Addon.Manage' => 1,
  142. 'Addons.Comments.Manage' => 1
  143. ));
  144. }
  145. // Make sure that User.Permissions is blank so new permissions for users get applied.
  146. //$SQL->Update('User', array('Permissions' => ''))->Put(); // done in PermissionModel::Save()
  147. // Insert some activity types
  148. /// %1 = ActivityName
  149. /// %2 = ActivityName Possessive
  150. /// %3 = RegardingName
  151. /// %4 = RegardingName Possessive
  152. /// %5 = Link to RegardingName's Wall
  153. /// %6 = his/her
  154. /// %7 = he/she
  155. /// %8 = RouteCode & Route
  156. // X added an addon
  157. if ($SQL->GetWhere('ActivityType', array('Name' => 'AddAddon'))->NumRows() == 0)
  158. $SQL->Insert('ActivityType', array('AllowComments' => '0', 'Name' => 'AddAddon', 'FullHeadline' => '%1$s uploaded a new %8$s.', 'ProfileHeadline' => '%1$s uploaded a new %8$s.', 'RouteCode' => 'addon', 'Public' => '1'));
  159. // X edited an addon
  160. if ($SQL->GetWhere('ActivityType', array('Name' => 'EditAddon'))->NumRows() == 0)
  161. $SQL->Insert('ActivityType', array('AllowComments' => '0', 'Name' => 'EditAddon', 'FullHeadline' => '%1$s edited an %8$s.', 'ProfileHeadline' => '%1$s edited an %8$s.', 'RouteCode' => 'addon', 'Public' => '1'));
  162. /*
  163. // People's comments on addons
  164. if ($SQL->GetWhere('ActivityType', array('Name' => 'AddonComment'))->NumRows() == 0)
  165. $SQL->Insert('ActivityType', array('AllowComments' => '0', 'Name' => 'AddonComment', 'FullHeadline' => '%1$s commented on %4$s %8$s.', 'ProfileHeadline' => '%1$s commented on %4$s %8$s.', 'RouteCode' => 'addon', 'Notify' => '1', 'Public' => '1'));
  166. // People mentioning others in addon comments
  167. if ($SQL->GetWhere('ActivityType', array('Name' => 'AddonCommentMention'))->NumRows() == 0)
  168. $SQL->Insert('ActivityType', array('AllowComments' => '0', 'Name' => 'AddonCommentMention', 'FullHeadline' => '%1$s mentioned %3$s in a %8$s.', 'ProfileHeadline' => '%1$s mentioned %3$s in a %8$s.', 'RouteCode' => 'comment', 'Notify' => '1', 'Public' => '0'));
  169. */
  170. // People adding new language definitions
  171. if ($SQL->GetWhere('ActivityType', array('Name' => 'AddUserLanguage'))->NumRows() == 0)
  172. $SQL->Insert('ActivityType', array('AllowComments' => '0', 'Name' => 'AddUserLanguage', 'FullHeadline' => '%1$s added a new %8$s.', 'ProfileHeadline' => '%1$s added a new %8$s.', 'RouteCode' => 'language', 'Notify' => '0', 'Public' => '1'));
  173. // People editing language definitions
  174. if ($SQL->GetWhere('ActivityType', array('Name' => 'EditUserLanguage'))->NumRows() == 0)
  175. $SQL->Insert('ActivityType', array('AllowComments' => '0', 'Name' => 'EditUserLanguage', 'FullHeadline' => '%1$s edited a %8$s.', 'ProfileHeadline' => '%1$s edited a %8$s.', 'RouteCode' => 'language', 'Notify' => '0', 'Public' => '1'));
  176. // Contains list of available languages for translating
  177. $Construct->Table('Language')
  178. ->PrimaryKey('LanguageID')
  179. ->Column('Name', 'varchar(255)')
  180. ->Column('Code', 'varchar(10)')
  181. ->Column('InsertUserID', 'int', FALSE, 'key')
  182. ->Column('DateInserted', 'datetime')
  183. ->Column('UpdateUserID', 'int', TRUE)
  184. ->Column('DateUpdated', 'datetime', TRUE)
  185. ->Set($Explicit, $Drop);
  186. // Contains relationships of who owns translations and who can edit translations (owner decides who can edit)
  187. $Construct->Table('UserLanguage')
  188. ->PrimaryKey('UserLanguageID')
  189. ->Column('UserID', 'int', FALSE, 'key')
  190. ->Column('LanguageID', 'int', FALSE, 'key')
  191. ->Column('Owner', 'tinyint(1)', '0')
  192. ->Column('CountTranslations', 'int', '0') // The number of translations this UserLanguage contains
  193. ->Column('CountDownloads', 'int', '0')
  194. ->Column('CountLikes', 'int', '0')
  195. ->Set($Explicit, $Drop);
  196. // Contains individual translations as well as source codes
  197. $Construct->Table('Translation')
  198. ->PrimaryKey('TranslationID')
  199. ->Column('UserLanguageID', 'int', FALSE, 'key')
  200. ->Column('SourceTranslationID', 'int', TRUE, 'key') // This is the related TranslationID where LanguageID = 1 (the source codes for translations)
  201. ->Column('Application', 'varchar(100)', TRUE)
  202. ->Column('Value', 'text')
  203. ->Column('InsertUserID', 'int', FALSE, 'key')
  204. ->Column('DateInserted', 'datetime')
  205. ->Column('UpdateUserID', 'int', TRUE)
  206. ->Column('DateUpdated', 'datetime', TRUE)
  207. ->Set($Explicit, $Drop);
  208. // Contains records of when actions were performed on userlanguages (ie. it is
  209. // downloaded or "liked"). These values are aggregated in
  210. // UserLanguage.CountLikes and UserLanguage.CountDownloads for faster querying,
  211. // but saved here for reporting.
  212. $Construct->Table('UserLanguageAction')
  213. ->PrimaryKey('UserLanguageActionID')
  214. ->Column('UserLanguageID', 'int', FALSE, 'key')
  215. ->Column('Action', 'varchar(20)') // The action being performed (ie. "download" or "like")
  216. ->Column('InsertUserID', 'int', TRUE, 'key') // Allows nulls because you do not need to be authenticated to download a userlanguage
  217. ->Column('DateInserted', 'datetime')
  218. ->Set($Explicit, $Drop);
  219. // Make sure the default "source" translation exists
  220. if ($SQL->GetWhere('Language', array('LanguageID' => 1))->NumRows() == 0)
  221. $SQL->Insert('Language', array('Name' => 'Source Codes', 'Code' => 'SOURCE', 'InsertUserID' => 1, 'DateInserted' => '2009-10-19 12:00:00'));
  222. // Mark (UserID 1) owns the source translation
  223. if ($SQL->GetWhere('UserLanguage', array('LanguageID' => 1, 'UserID' => 1))->NumRows() == 0)
  224. $SQL->Insert('UserLanguage', array('LanguageID' => 1, 'UserID' => 1, 'Owner' => '1'));
  225. /*
  226. Apr 26th, 2010
  227. Changed all "enum" fields representing "bool" (0 or 1) to be tinyint.
  228. For some reason mysql makes 0's "2" during this change. Change them back to "0".
  229. */
  230. if (!$Construct->CaptureOnly) {
  231. $SQL->Query("update GDN_AddonType set Visible = '0' where Visible = '2'");
  232. $SQL->Query("update GDN_Addon set Visible = '0' where Visible = '2'");
  233. $SQL->Query("update GDN_Addon set Vanilla2 = '0' where Vanilla2 = '2'");
  234. $SQL->Query("update GDN_UserLanguage set Owner = '0' where Owner = '2'");
  235. }
  236. // Add AddonID column to discussion table for allowing discussions on addons.
  237. $Construct->Table('Discussion')
  238. ->Column('AddonID', 'int', NULL)
  239. ->Set();
  240. // Insert all of the existing comments into a new discussion for each addon
  241. $Construct->Table('AddonComment');
  242. $AddonCommentExists = $Construct->TableExists();
  243. $Construct->Reset();
  244. if ($AddonCommentExists) {
  245. if ($SQL->Query('select AddonCommentID from GDN_AddonComment')->NumRows() > 0) {
  246. // Create discussions for addons with comments
  247. $SQL->Query("insert into GDN_Discussion
  248. (AddonID, InsertUserID, UpdateUserID, LastCommentID, Name, Body, Format,
  249. CountComments, DateInserted, DateUpdated, DateLastComment, LastCommentUserID)
  250. select distinct a.AddonID, a.InsertUserID, a.UpdateuserID, 0, a.Name, a.Name,
  251. ac.Format, a.CountComments, a.DateInserted, a.DateUpdated, a.DateUpdated, 0
  252. from GDN_Addon a join GDN_AddonComment ac on a.AddonID = ac.AddonID");
  253. // Copy the comments across to the comment table
  254. $SQL->Query("insert into GDN_Comment
  255. (DiscussionID, InsertUserID, Body, Format, DateInserted)
  256. select d.DiscussionID, ac.InsertUserID, ac.Body, ac.Format, ac.DateInserted
  257. from GDN_Discussion d join GDN_AddonComment ac on d.AddonID = ac.AddonID");
  258. // Update the LastCommentID
  259. $SQL->Query("update GDN_Discussion d
  260. join (
  261. select DiscussionID, max(CommentID) as LastCommentID
  262. from GDN_Comment
  263. group by DiscussionID
  264. ) c
  265. on d.DiscussionID = c.DiscussionID
  266. set d.LastCommentID = c.LastCommentID");
  267. // Update the LastCommentUserID
  268. $SQL->Query("update GDN_Discussion d
  269. join GDN_Comment c on d.LastCommentID = c.CommentID
  270. set d.LastCommentUserID = c.InsertUserID");
  271. // Delete the comments from the addon comments table
  272. $SQL->Query('truncate table GDN_AddonComment');
  273. }
  274. }