PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/installer/models/install_m.php

https://gitlab.com/sheldonels/pyrocms
PHP | 289 lines | 219 code | 40 blank | 30 comment | 0 complexity | 41960b2f76305176d43c2981d621b715 MD5 | raw file
  1. <?php
  2. use Illuminate\Database\Connection;
  3. /**
  4. * Install model
  5. *
  6. * @author PyroCMS Dev Team
  7. * @package PyroCMS\Installer\Models
  8. *
  9. */
  10. class Install_m extends CI_Model
  11. {
  12. public function set_default_structure(Connection $conn, array $user, array $db)
  13. {
  14. // Include migration config to know which migration to start from
  15. require PYROPATH.'config/migration.php';
  16. $schema = $conn->getSchemaBuilder();
  17. // Remove any tables not installed by a module
  18. $schema->dropIfExists('core_users');
  19. $schema->dropIfExists('core_users_groups');
  20. $schema->dropIfExists('core_settings');
  21. $schema->dropIfExists('core_sites');
  22. $schema->dropIfExists(config_item('sess_table_name'));
  23. $schema->dropIfExists($db['site_ref'].'_modules');
  24. $schema->dropIfExists($db['site_ref'].'_migrations');
  25. $schema->dropIfExists($db['site_ref'].'_settings');
  26. $schema->dropIfExists($db['site_ref'].'_users');
  27. $schema->dropIfExists($db['site_ref'].'_users_groups');
  28. $schema->dropIfExists($db['site_ref'].'_profiles');
  29. // Create core_settings first
  30. $schema->create('core_settings', function($table) {
  31. $table->string('slug', 30);
  32. $table->text('default')->nullable();
  33. $table->text('value')->nullable();
  34. $table->unique('slug');
  35. $table->index('slug');
  36. });
  37. // Populate core settings
  38. $conn->table('core_settings')->insert(array(
  39. array(
  40. 'slug' => 'date_format',
  41. 'default' => 'g:ia -- m/d/y',
  42. ),
  43. array(
  44. 'slug' => 'lang_direction',
  45. 'default' => 'ltr',
  46. ),
  47. array(
  48. 'slug' => 'status_message',
  49. 'default' => 'This site has been disabled by a super-administrator.',
  50. ),
  51. ));
  52. // Core Sites
  53. $schema->create('core_sites', function($table) {
  54. $table->increments('id');
  55. $table->string('name', 100);
  56. $table->string('ref', 20);
  57. $table->string('domain', 100);
  58. $table->boolean('is_activated')->default(true);
  59. $table->integer('created_on');
  60. $table->integer('updated_on')->nullable();
  61. $table->unique('ref');
  62. $table->unique('domain');
  63. $table->index('ref');
  64. $table->index('domain');
  65. });
  66. // User Table is used for site users and core users
  67. $user_table = function($table) {
  68. $table->increments('id');
  69. $table->string('username', 20);
  70. $table->string('email', 60);
  71. $table->string('password', 255);
  72. $table->string('salt', 6);
  73. $table->string('ip_address');
  74. $table->boolean('is_activated')->default(false);
  75. $table->string('activation_code')->nullable();
  76. $table->string('persist_code')->nullable();
  77. $table->string('reset_password_code')->nullable();
  78. $table->integer('created_on');
  79. $table->integer('updated_on')->nullable();
  80. $table->integer('last_login')->nullable();
  81. $table->unique('email');
  82. $table->unique('username');
  83. $table->index('email');
  84. $table->index('username');
  85. };
  86. // Call upon the Mystical Hasher of Asoroth
  87. $hasher = new Cartalyst\Sentry\Hashing\NativeHasher;
  88. $password = $hasher->hash($user['password']);
  89. $user_data = array(
  90. 'username' => $user['username'],
  91. 'email' => $user['email'],
  92. 'password' => $password,
  93. 'ip_address' => $this->input->ip_address(),
  94. 'is_activated'=> true,
  95. 'created_on' => time(),
  96. );
  97. // Create User tables
  98. $schema->create('core_users', $user_table);
  99. $schema->create($db['site_ref'].'_users', $user_table);
  100. // Insert our new user to both
  101. $conn->table('core_users')->insert($user_data);
  102. $conn->table($db['site_ref'].'_users')->insert($user_data);
  103. // Create Users Groups
  104. $user_groups_table = function($table)
  105. {
  106. $table->increments('id');
  107. $table->integer('user_id');
  108. $table->integer('group_id');
  109. };
  110. $user_groups_data = array(
  111. 'user_id' => 1,
  112. 'group_id' => 1,
  113. );
  114. // Create both User Groups tables
  115. $schema->create('core_users_groups', $user_groups_table);
  116. $schema->create($db['site_ref'].'_users_groups', $user_groups_table);
  117. // Insert our new user to both
  118. $conn->table('core_users_groups')->insert($user_groups_data);
  119. $conn->table($db['site_ref'].'_users_groups')->insert($user_groups_data);
  120. // Create Session Table
  121. $schema->create(config_item('sess_table_name'), function($table) {
  122. $table->string('session_id', 40)->default(0);
  123. $table->string('ip_address', 16)->default(0);
  124. $table->string('user_agent', 120);
  125. $table->integer('last_activity')->default(0);
  126. $table->text('user_data');
  127. $table->index('last_activity', 'last_activity_idx');
  128. });
  129. // HEAR YE HEAR YE, THE DB PREFIX CHANGES NOW!
  130. $conn->getSchemaGrammar()->setTablePrefix($db['site_ref'].'_'); // Set for grammer ($schema)
  131. $conn->setTablePrefix($db['site_ref'].'_'); // Set for connection ($conn)
  132. // Migrations
  133. $schema->create('migrations', function($table) {
  134. $table->integer('version');
  135. });
  136. // Insert current latest migration
  137. $conn->table('migrations')->insert(array(
  138. 'version' => $config['migration_version'],
  139. ));
  140. // Modules
  141. // TODO make migration to remove "type" field from here
  142. $schema->create('modules', function($table) {
  143. $table->increments('id');
  144. $table->text('name');
  145. $table->string('slug', 50);
  146. $table->string('version', 20);
  147. $table->text('description');
  148. $table->boolean('skip_xss');
  149. $table->boolean('is_frontend');
  150. $table->boolean('is_backend');
  151. $table->string('menu', 20);
  152. $table->boolean('enabled');
  153. $table->boolean('installed');
  154. $table->boolean('is_core');
  155. $table->integer('updated_on')->nullable();
  156. $table->unique('slug');
  157. $table->index('enabled');
  158. $table->index('is_frontend');
  159. $table->index('is_backend');
  160. });
  161. $schema->dropIfExists('themes');
  162. $schema->create('themes', function($table) {
  163. $table->increments('id');
  164. $table->integer('site_id')->nullable();
  165. $table->string('slug');
  166. $table->string('name');
  167. $table->text('description');
  168. $table->string('author')->nullable();
  169. $table->string('author_website')->nullable();
  170. $table->string('website')->nullable();
  171. $table->string('version')->default('1.0.0');
  172. $table->string('type')->nullable();
  173. $table->boolean('enabled')->default(true);
  174. $table->integer('order')->default(0);
  175. $table->integer('created_on');
  176. $table->integer('updated_on')->nullable();
  177. });
  178. $schema->dropIfExists('theme_options');
  179. $schema->create('theme_options', function($table) {
  180. $table->increments('id');
  181. $table->string('slug', 30);
  182. $table->string('title', 100);
  183. $table->text('description');
  184. $table->enum('type', array('text', 'textarea', 'password', 'select', 'select-multiple', 'radio', 'checkbox', 'colour-picker'));
  185. $table->string('default', 255);
  186. $table->string('value', 255);
  187. $table->text('options');
  188. $table->boolean('is_required');
  189. $table->integer('theme_id')->nullable();
  190. });
  191. $schema->create('settings', function($table) {
  192. $table->string('slug', 30);
  193. $table->string('title', 100);
  194. $table->text('description');
  195. $table->enum('type', array('text','textarea','password','select','select-multiple','radio','checkbox'));
  196. $table->text('default')->nullable();
  197. $table->text('value')->nullable();
  198. $table->string('options', 255)->nullable();
  199. $table->boolean('is_required')->default(false);
  200. $table->boolean('is_gui')->default(true);
  201. $table->string('module', 50)->nullable();
  202. $table->integer('order')->default(0);
  203. $table->unique('slug');
  204. $table->index('slug');
  205. });
  206. // Streams Table
  207. $schema->dropIfExists('data_streams');
  208. $schema->create('data_streams', function($table) {
  209. $table->increments('id');
  210. $table->string('stream_name', 60);
  211. $table->string('stream_slug', 60);
  212. $table->string('stream_namespace', 60)->nullable();
  213. $table->string('stream_prefix', 60)->nullable();
  214. $table->string('about', 255)->nullable();
  215. $table->binary('view_options');
  216. $table->string('title_column', 255)->nullable();
  217. $table->enum('sorting', array('title', 'custom'))->default('title');
  218. $table->text('permissions')->nullable();
  219. $table->enum('is_hidden', array('yes','no'))->default('no');
  220. $table->string('menu_path', 255)->nullable();
  221. });
  222. // Fields Table
  223. $schema->dropIfExists('data_fields');
  224. $schema->create('data_fields', function($table) {
  225. $table->increments('id');
  226. $table->string('field_name', 60);
  227. $table->string('field_slug', 60);
  228. $table->string('field_namespace', 60)->nullable();
  229. $table->string('field_type', 50);
  230. $table->binary('field_data')->nullable();
  231. $table->binary('view_options')->nullable();
  232. $table->enum('is_locked', array('yes', 'no'))->default('no');
  233. });
  234. // Assignments Table
  235. $schema->dropIfExists('data_field_assignments');
  236. $schema->create('data_field_assignments', function($table) {
  237. $table->increments('id');
  238. $table->integer('sort_order');
  239. $table->integer('stream_id');
  240. $table->integer('field_id');
  241. $table->enum('is_required', array('yes', 'no'))->default('no');
  242. $table->enum('is_unique', array('yes', 'no'))->default('no');
  243. $table->text('instructions')->nullable();
  244. $table->string('field_name', 60);
  245. // $table->foreign('stream_id'); //TODO Set up foreign keys
  246. // $table->foreign('field_id'); //TODO Set up foreign keys
  247. });
  248. }
  249. }