예제 #1
0
파일: Image.php 프로젝트: orangehill/photon
 public function __construct(\Orangehill\Photon\Field $field)
 {
     parent::__construct($field);
     $this->module = Module::find($field->module_id);
     $this->relativeFolderPath = Config::get('photon::photon.media_folder') . "/{$this->module->table_name}/" . $this->column_name;
     $this->storageFolderPath = public_path($this->relativeFolderPath);
 }
예제 #2
0
 public function __construct(\Orangehill\Photon\Field $field)
 {
     parent::__construct($field);
     $this->module = Module::find($field->module_id);
 }
예제 #3
0
 /**
  * Deletes a module by id
  *
  * @param $id
  */
 public function deleteModule($id)
 {
     $module = Module::find($id);
     if ($module) {
         \Schema::dropIfExists($module->table_name);
         $modelName = ucfirst(camel_case(str_singular($module->table_name)));
         $filePath = app_path('models') . '/' . $modelName . '.php';
         if (file_exists($filePath)) {
             unlink($filePath);
         }
         $module->delete();
     }
 }
예제 #4
0
 /**
  * Saves submitted and validated data
  *
  * @param  $verbose boolean $verbose If set to true function will only specify pending changes without making any
  *
  * @return array
  */
 protected function save($fieldIds, $verbose = false)
 {
     $report = '';
     // Instantiate Migration Generator Class (Laravel 4 Generator Module Wrapper)
     $generator = new MigrationGenerator();
     // Check if module_name is new or existing one
     if (is_numeric(\Input::get('module_id'))) {
         // Get module data
         $module = Module::find(\Input::get('module_id'));
         // Handle Folder module type
         if (\Input::get('is_folder') == '1') {
             return $this->updateFolderModuleType($module, $verbose);
         }
         // Get id's of fields that are stored in the database
         $existingFieldIds = array_fetch($module->fields()->get()->toArray(), 'id');
         // Check if remove module request is received
         if (\Input::get('remove_request') == 1) {
             $report .= $this->reportSectionTitleGenerator('remove', 'module', \Input::get('name'));
             // If in verbose mode return report array
             if ($verbose) {
                 return array('report' => $report);
             }
             // Populate migrationFieldsDropped array (needed for php artisan migrate:rollback)
             $migrationFieldsDropped = array();
             $existingFieldIds = $module->fields()->get();
             foreach ($existingFieldIds as $field) {
                 $migrationFieldsDropped[$field->column_name] = $field->column_type;
             }
             // Generate migration file
             $generator->destroy($module->table_name, $migrationFieldsDropped);
             // Run the migrations
             if (\Input::get('run-migrations') == 1) {
                 \Artisan::call('migrate');
             }
             // Remove the module (also removes orphans from fields table)
             $module->delete();
             // Notify about successful data removal
             \Session::flash('success', 'Data has been removed.');
             // Redirect to settings page
             return \Redirect::to('/admin/settings/');
         }
         // Create an array of deleted fields, if any
         $deletedFieldIds = array_diff($existingFieldIds, $fieldIds);
         // Create an array of new fields, if any
         $newFieldIds = array_diff($fieldIds, $existingFieldIds);
         // Create an array of edited fields
         $editedFieldIds = array_diff($fieldIds, $deletedFieldIds, $newFieldIds);
         if (!$module->get()->isEmpty()) {
             $moduleHasChanged = false;
             // Updating existing module
             $report .= $this->reportSectionTitleGenerator('change', 'module', \Input::get('name'));
             if ($module->name != \Input::get('module_name')) {
                 $report .= $this->reportGenerator('set', 'name', \Input::get('name'), $module->name);
                 $module->name = \Input::get('name');
                 $moduleHasChanged = true;
             }
             if ($module->parent_module != \Input::get('parent_module')) {
                 $report .= $this->reportGenerator('set', 'parent module', \Input::get('parent_module'), $module->parent_module);
                 $module->parent_module = \Input::get('parent_module');
                 $moduleHasChanged = true;
             }
             if ($module->nestable != \Input::get('nestable')) {
                 $report .= $this->reportGenerator('set', 'nestable', \Input::get('nestable'), $module->nestable);
                 $module->nestable = \Input::get('nestable');
                 $moduleHasChanged = true;
             }
             $report .= $moduleHasChanged ? '' : '<br/>';
             // Save changes to module table
             if (!$verbose) {
                 $module->save();
             }
             // Process new fields for an existing module
             if (count($newFieldIds) > 0) {
                 $migrationFields = array();
                 foreach ($newFieldIds as $newFieldId) {
                     // Generate a report line
                     $report .= $this->insertFieldReport($newFieldId);
                     // Insert a new record in fields table
                     if (!$verbose) {
                         $tmp = array();
                         $tmp['module_id'] = \Input::get('module_id');
                         $tmp['field_name'] = \Input::get('field_name' . $newFieldId);
                         $tmp['field_type'] = \Input::get('field_type' . $newFieldId);
                         $tmp['relation_table'] = \Input::get('relation_table' . $newFieldId);
                         $tmp['column_name'] = \Input::get('column_name' . $newFieldId);
                         $tmp['column_type'] = \Input::get('column_type' . $newFieldId);
                         $tmp['tooltip_text'] = \Input::get('tooltip_text' . $newFieldId);
                         $newField = Field::create($tmp);
                         // Populate migrationFields array
                         $migrationFields[\Input::get('column_name' . $newFieldId)] = \Input::get('column_type' . $newFieldId);
                     }
                     $moduleHasChanged = true;
                 }
                 // Generate migration files
                 if (!$verbose) {
                     $generator->add($module->table_name, $migrationFields);
                 }
             }
             // Process edited fields for an existing module
             if (count($editedFieldIds) > 0) {
                 $migrationFields = array();
                 $migrationFieldsDropped = array();
                 foreach ($editedFieldIds as $fieldId) {
                     $fieldHasChanged = false;
                     $fieldReport = '';
                     $field = $module->fields()->find($fieldId);
                     // Populate migrationFields and migrationFieldsDropped arrays
                     if ($field->column_name != \Input::get('column_name' . $fieldId) or $field->column_type != \Input::get('column_type' . $fieldId)) {
                         $migrationFields[\Input::get('column_name' . $fieldId)] = \Input::get('column_type' . $fieldId);
                         $migrationFieldsDropped[$field->column_name] = $field->column_type;
                     }
                     if ($field->field_name != \Input::get('field_name' . $fieldId)) {
                         $fieldReport .= $this->reportGenerator('set', 'field name', \Input::get('field_name' . $fieldId), $field->field_name);
                         $field->field_name = \Input::get('field_name' . $fieldId);
                         $fieldHasChanged = true;
                     }
                     if ($field->field_type != \Input::get('field_type' . $fieldId)) {
                         $fieldReport .= $this->reportGenerator('set', 'field type', \Input::get('field_type' . $fieldId), $field->field_type);
                         $field->field_type = \Input::get('field_type' . $fieldId);
                         $fieldHasChanged = true;
                     }
                     if ($field->relation_table != \Input::get('relation_table' . $fieldId)) {
                         $fieldReport .= $this->reportGenerator('set', 'relation table', \Input::get('relation_table' . $fieldId), $field->relation_table);
                         $field->relation_table = \Input::get('relation_table' . $fieldId);
                         $fieldHasChanged = true;
                     }
                     if ($field->column_name != \Input::get('column_name' . $fieldId)) {
                         $fieldReport .= $this->reportGenerator('set', 'column name', \Input::get('column_name' . $fieldId), $field->column_name);
                         $field->column_name = \Input::get('column_name' . $fieldId);
                         $fieldHasChanged = true;
                     }
                     if ($field->column_type != \Input::get('column_type' . $fieldId)) {
                         $fieldReport .= $this->reportGenerator('set', 'column type', \Input::get('column_type' . $fieldId), $field->column_type);
                         $field->column_type = \Input::get('column_type' . $fieldId);
                         $fieldHasChanged = true;
                     }
                     if ($field->tooltip_text != \Input::get('tooltip_text' . $fieldId)) {
                         $fieldReport .= $this->reportGenerator('set', 'tooltip text', \Input::get('tooltip_text' . $fieldId), $field->tooltip_text);
                         $field->tooltip_text = \Input::get('tooltip_text' . $fieldId);
                         $fieldHasChanged = true;
                     }
                     if ($fieldHasChanged) {
                         $fieldReport = $this->reportSectionTitleGenerator('change', 'field', $field->field_name) . $fieldReport;
                         $fieldReport .= '<br/>';
                         $report .= $fieldReport;
                         $moduleHasChanged = true;
                     }
                     if (!$verbose and $fieldHasChanged) {
                         $field->save();
                     }
                 }
                 // Generate migration files
                 if (!$verbose and $fieldHasChanged) {
                     // First drop fields with old column_type
                     $generator->remove($module->table_name, $migrationFieldsDropped);
                     // Wait 1 second so that remove migration is executed before add (filenaming issue)
                     sleep(1);
                     // Than add fields with changed column_type
                     $generator->add($module->table_name, $migrationFields);
                 }
             }
             // Process deleted fields
             if (count($deletedFieldIds) > 0) {
                 $migrationFieldsDropped = array();
                 foreach ($deletedFieldIds as $deletedFieldId) {
                     $field = $module->fields()->find($deletedFieldId);
                     $report .= $this->reportSectionTitleGenerator('remove', 'field', $field->field_name);
                     // Populate migrationFieldsDropped array
                     $migrationFieldsDropped[$field->column_name] = $field->column_type;
                     if (!$verbose) {
                         $field->delete();
                     }
                     $moduleHasChanged = true;
                 }
                 // Generate migration files
                 if (!$verbose) {
                     $generator->remove($module->table_name, $migrationFieldsDropped);
                 }
             }
             // This will yield 'no changes detected' message
             if (!$moduleHasChanged) {
                 return false;
             }
             // If in verbose mode return report array
             if ($verbose) {
                 return array('report' => $report);
             }
             // Run the migrations
             if (\Input::get('run-migrations') == 1) {
                 \Artisan::call('migrate');
             }
             // Notify about successful data entry
             \Session::flash('success', 'Data has been saved.');
             // Redirect to module settings page
             return \Redirect::to('/admin/settings/' . \Input::get('module_id'));
         }
     }
     // Inserting a new model
     $report .= $this->reportSectionTitleGenerator('create', 'module');
     $report .= $this->reportGenerator('set', 'module name', \Input::get('name'));
     $report .= $this->reportGenerator('set', 'table name', \Input::get('table_name'));
     $report .= $this->reportGenerator('set', 'parent module', \Input::get('parent_module'));
     $report .= $this->reportGenerator('set', 'nestable', \Input::get('nestable'));
     $report .= '<br/>';
     // Insert a new record in modules table, and get it's ID
     if (!$verbose) {
         $newModule = Module::create(\Input::all());
     }
     $migrationFields = array();
     // Inserting new fields
     foreach ($fieldIds as $fieldId) {
         // Generate a report line
         $report .= $this->insertFieldReport($fieldId);
         // Insert a new record in fields table
         if (!$verbose) {
             $tmp = array();
             $tmp['module_id'] = $newModule->id;
             $tmp['field_name'] = \Input::get('field_name' . $fieldId);
             $tmp['field_type'] = \Input::get('field_type' . $fieldId);
             $tmp['relation_table'] = \Input::get('relation_table' . $fieldId);
             $tmp['column_name'] = \Input::get('column_name' . $fieldId);
             $tmp['column_type'] = \Input::get('column_type' . $fieldId);
             $tmp['tooltip_text'] = \Input::get('tooltip_text' . $fieldId);
             $newField = Field::create($tmp);
             // Populate migrationFields array
             $migrationFields[\Input::get('column_name' . $fieldId)] = \Input::get('column_type' . $fieldId);
         }
     }
     // If in verbose mode return report array
     if ($verbose) {
         return array('report' => $report);
     }
     // Generate migration files (Only if module type is not folder)
     if (\Input::get('is_folder') !== '1') {
         $generator->create(\Input::get('table_name'), $migrationFields);
     }
     // Run the migrations (Only if module type is not folder)
     if (\Input::get('run-migrations') == 1 && \Input::get('is_folder') !== '1') {
         \Artisan::call('migrate');
     }
     // Notify about successful data entry
     \Session::flash('success', 'Data has been saved.');
     // Redirect to new module settings page
     return \Redirect::to('/admin/settings/' . $newModule->id);
 }
예제 #5
0
 public function init($tableName, $id = null)
 {
     $modelName = studly_case(str_singular($tableName));
     $model = '\\' . $modelName;
     // If there's no model in root namespace, check in Photon ns
     if (!class_exists($model)) {
         $model = '\\Orangehill\\Photon\\' . $modelName;
     }
     // Get model instance for current module
     $this->modelInstance = new $model();
     // Fetch module data by table name
     $this->data['module'] = Module::where('table_name', $tableName)->first();
     // Return error page if no module is found
     if (!$this->data['module']) {
         return \View::make('photon::errors.missing');
     }
     // Fetch Fields
     $this->data['fields'] = $this->data['module']->fields;
     // If entry ID has been provided fetch the entry data to hydrate a form
     if ($id) {
         $this->data['entry'] = $this->modelInstance->find($id);
     }
     // Check for certain field types that need to be handled specialy later in postEntry()
     foreach ($this->data['fields'] as $field) {
         if ($field->field_type == 'image') {
             $this->imageFields[] = $field;
         }
         if ($field->field_type == 'boolean') {
             $this->booleanFields[] = $field;
         }
         if ($field->field_type == 'one-to-many') {
             $this->oneToManyFields[] = $field;
         }
         if ($field->field_type == 'many-to-many') {
             $this->manyToManyFields[] = $field;
         }
     }
     // Handle oneToMany fields
     if (isset($this->oneToManyFields) and count($this->oneToManyFields) > 0) {
         // $data['oneToMany'] will be used to populate the select field
         $this->data['oneToMany'] = array();
         foreach ($this->oneToManyFields as $oneToManyField) {
             // Fetch the data from related table
             $module = \Orangehill\Photon\Module::find($oneToManyField->relation_table);
             $this->data['oneToMany'][$oneToManyField->column_name] = $this->fetchRelationTable($module, $oneToManyField->id);
         }
     }
     // Handle manyToMany fields
     if (isset($this->manyToManyFields) and count($this->manyTDoManyFields) > 0) {
         // $data['manyToMany'] will be used to populate the dual select field
         $this->data['manyToMany'] = array();
         foreach ($this->manyToManyFields as $manyToManyField) {
             // Fetch the data from related table
             $module = \Orangehill\Photon\Module::find($manyToManyField->relation_table);
             $this->data['manyToMany'][$manyToManyField->column_name]['available'] = $this->fetchRelationTable($module, $manyToManyField->id);
             $this->data['manyToMany'][$manyToManyField->column_name]['selected'] = array();
             // Fetch Selected Items
             if (is_numeric(\Request::segment(3))) {
                 $tableName = $module->table_name;
                 $selected = $this->data['entry']->{$tableName}()->get();
                 foreach ($selected as $item) {
                     $this->data['manyToMany'][$manyToManyField->column_name]['selected'][] = $item->id;
                 }
                 // Set a table name inside an array
                 $this->data['manyToMany'][$manyToManyField->column_name]['tableName'] = $tableName;
             }
         }
     }
     // Set the breadcrumbs array
     $this->data['breadcrumbs'] = array(array('url' => 'javascript:;', 'anchor' => $this->data['module']->name));
 }