示例#1
0
 /**
  * Deletes a particular resource
  *
  * @param $tableName Table Name
  * @param $fieldName Field Name
  * @param $entryId   Entry ID
  * @param $fileName  File Name
  *
  * @return JsonResponse
  */
 public function deleteIndex($tableName, $fieldName, $entryId, $fileName)
 {
     $module = Module::where('table_name', $tableName)->first();
     $moduleField = Field::where('module_id', $module->id)->where('column_name', $fieldName)->first();
     FieldFactory::make($moduleField)->delete($entryId);
     return \Response::json($this->apiResponse->toArray());
 }
示例#2
0
 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);
 }
示例#3
0
 protected function fetchOptions($relatedModuleTableName)
 {
     $relatedModule = Module::where('table_name', $relatedModuleTableName)->first();
     $modelName = studly_case(str_singular($relatedModule->table_name));
     $data = $modelName::get();
     return $data;
 }
示例#4
0
 /**
  * Gets the main menu
  *
  * @return array
  */
 public static function getMainMenu()
 {
     /* @var $folders Collection */
     $folders = Folder::with('modules')->orderBy('lft')->get()->filter(function ($e) {
         return !$e->modules->isEmpty();
     });
     /* @var $freeModules Collection */
     $freeModules = Module::whereNull('folder_id')->orderBy('lft')->get();
     return $folders->merge($freeModules);
 }
示例#5
0
 /**
  * Creates a module diff based on an module input array
  *
  * @param $moduleInput
  *
  * @return array
  */
 public static function diffChanges($moduleInput)
 {
     $comparator = new ModuleComparator();
     $id = isset($moduleInput['id']) ? $moduleInput['id'] : 0;
     $module = Module::with('Fields')->find($id);
     $module = is_object($module) ? $module->toArray() : array();
     $changes = $comparator->compare($moduleInput, $module);
     foreach ($changes as &$changesets) {
         foreach ($changesets as &$changeset) {
             $changeset = $changeset->toArray();
         }
     }
     return $changes;
 }
示例#6
0
 public function __construct(\Orangehill\Photon\Field $field)
 {
     parent::__construct($field);
     $this->module = Module::find($field->module_id);
 }
示例#7
0
 /**
  * Fetches the Module object based on a table name
  *
  * @param $tableName
  *
  * @return Module
  */
 protected function getModule($tableName)
 {
     $tableName = snake_case(str_plural($tableName));
     $module = Module::where('table_name', $tableName)->first();
     return $module;
 }
示例#8
0
 public static function getTableName()
 {
     $instance = new Module();
     return $instance->getTable();
 }
示例#9
0
 /**
  * Take the module creator form input and apply changes
  *
  * @param array $input
  * @param array $changeGroups
  *
  * @todo Break this up into smaller parts
  */
 public function applyModuleChanges(array $input = array(), array $changeGroups = array())
 {
     // Initialize needed variables
     $tableName = $input['table_name'];
     // Module table name
     $moduleId = $input['id'];
     // Module ID
     $moduleChangeset = ArrayLib::getKey(array_shift($changeGroups), 0);
     // Get the module changeset
     // Initialize the generator map array
     $generatorMap = array('created_fields' => array(), 'deleted_fields' => array(), 'updated_fields' => array());
     // Define artisan generator methods to be used with generator map keys
     $generatorMethods = array('created_fields' => 'add', 'deleted_fields' => 'remove');
     // If there's no module id, spawn a migration for creation of a new module
     if (!$moduleId) {
         MigrationGenerator::create($tableName);
         sleep(1);
         // Pause a second so succeeding migrations are named (ordered) properly
     }
     // Gather fields
     foreach ($changeGroups as $groupType => $changesets) {
         // Skip if we're dealing with updates
         if ($groupType === 'updated_fields') {
             continue;
         }
         foreach ($changesets as $changeset) {
             // Skip if changeset is empty
             if (!is_array($changeset['changes']) || empty($changeset['changes'])) {
                 continue;
             }
             // Retrieve the column type of a current changeset
             $columnType = ArrayLib::getKey(ArrayLib::findByKeyValue($changeset['changes'], 'name', 'column_type') ?: array(), 'new');
             // Retrieve the column name of a current changeset
             $columnName = ArrayLib::getKey(ArrayLib::findByKeyValue($changeset['changes'], 'name', 'column_name') ?: array(), 'new');
             $generatorMap[$groupType][$columnName] = $columnType;
         }
     }
     // Generate migrations for new fields
     foreach ($generatorMap as $key => $fields) {
         // Check if a migration should be generated
         if (array_key_exists($key, $generatorMethods) && !empty($fields)) {
             // Bam!
             MigrationGenerator::$generatorMethods[$key]($tableName, array_filter($fields, function ($value) {
                 return !empty($value);
             }));
         }
     }
     // Update model
     if (!empty($moduleChangeset['changes'])) {
         // Map of field => value entries
         $changemap = [];
         // Fill the changemap
         foreach ($moduleChangeset['changes'] as $change) {
             $changemap[$change['name']] = $change['new'];
         }
         // If module already exists, update it with new entries
         if ($moduleChangeset['item_id']) {
             \DB::table(\Orangehill\Photon\Module::getTableName())->where('table_name', $tableName)->update($changemap);
         } else {
             // Module doesn't exist yet, it should be created
             // Create a new module based on an form input entry
             $newModule = new Module($changemap);
             // Generate a model file
             $modelName = ucfirst(camel_case(str_singular($tableName)));
             \Artisan::call('generate:model', array('modelName' => $modelName));
             // Store the path to the model file
             $pathToModel = app_path('models') . '/' . $modelName . '.php';
             // Open the newly created file and make the model extend Baum's Node instead of Eloquent
             $fileContent = file_get_contents($pathToModel);
             $replaced = str_replace('extends \\Eloquent', 'extends Baum\\Node', $fileContent);
             // Try to find a name of first added column, fallback to the id
             $toStringField = 'id';
             if (is_array($changeGroups['created_fields'])) {
                 $head = head($changeGroups['created_fields']);
                 if (is_array($head['changes'])) {
                     foreach ($head['changes'] as $fieldSegment) {
                         if ($fieldSegment['name'] == 'column_name') {
                             $toStringField = $fieldSegment['new'];
                         }
                     }
                 }
             }
             // Create a __toString method stub
             $toStringStub = StubFactory::make('ModelToStringMethod', array('field' => $toStringField))->append("\n}")->get();
             // Add the method to the model files
             $replaced = substr_replace($replaced, $toStringStub, strrpos($replaced, '}'));
             file_put_contents($pathToModel, $replaced);
             // Save a model and update the module id
             $newModule->save();
             $moduleId = $newModule->id;
         }
     }
     // Add newborn fields
     foreach ($changeGroups['created_fields'] as $field) {
         $fieldData = array('module_id' => $moduleId);
         foreach ($field['changes'] as $element) {
             $fieldData[$element['name']] = $element['new'];
         }
         $field = FieldCreator::make($fieldData);
         $field->save();
     }
     // Update fields
     foreach ($changeGroups['updated_fields'] as $field) {
         $fieldData = array();
         foreach ($field['changes'] as $element) {
             $fieldData[$element['name']] = $element['new'];
         }
         \DB::table(Field::getTableName())->where('id', $field['item_id'])->update($fieldData);
     }
     // Delete fields
     $deletedFieldIds = array_fetch($changeGroups['deleted_fields'], 'item_id') + array(0);
     $moduleFields = Field::whereIn('id', $deletedFieldIds)->get();
     foreach ($moduleFields as $moduleField) {
         FieldFactory::make($moduleField)->uninstall();
     }
     foreach ($changeGroups['deleted_fields'] as $field) {
         \DB::table(Field::getTableName())->where('id', $field['item_id'])->delete();
     }
 }
示例#10
0
 public function getModules()
 {
     $modules = Module::all() ?: new Collection();
     return \Response::json($this->apiResponse->setContent($modules->toArray())->toArray());
 }
示例#11
0
 /**
  * Gets All Available Parent Modules
  *
  * @param int $id
  *
  * @return Object
  */
 public function getParentModules($id = null)
 {
     if (is_numeric($id)) {
         return Module::roots()->where('id', '!=', $id)->get();
     }
     return Module::roots()->get();
 }
 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));
 }