2015-06-10, 23:43:37
Hi Rico,
thank you very much for your report, it will definitely help me to improve some things!
I have little time, so I am being brief.
Unfortunately that what you plan to do, is not quite so simple since you are trying to access one of the „Model“ -Method, which is called within the „ImBackend“ class. It’s possible but a little inconvenient.
First off, create a new plugin/file named „im.extender.php“ for example, in „/plugins/„ directory. Then you simply add the following code in this file:
As the final step activate new plugin in „GetSimple“ Plugin menu.
A big thanks again!
thank you very much for your report, it will definitely help me to improve some things!
(2015-06-10, 08:51:55)Rico Wrote: And now the question. I've been reading the API reference. I'm not sure this is the shorter or the most efficient way to create a custom field... but it works:
PHP Code:$manager = new IManager();
$fields = $manager->getFieldsClass();
//Create a XML fields file for category with id=2 if it doesn't exist:
if (!$fields->fieldsExists(2)) {$fields->createFields(2);}
$fields->init(2);
//Create a custom text field "my_field_name" in the category with id=2 if it doesn't exist:
$my_field = $fields->getField('name=my_field_name');
if(!$my_field) {
$newField = $manager->newField(2);
$newField->name = 'my_field_name';
$newField->type = 'text';
$newField->save();
}
Where and in what file can I write this code (or similar) if I want certain custom fields to appear by default in each new category created from the back-end?
That's all!
I have little time, so I am being brief.
Unfortunately that what you plan to do, is not quite so simple since you are trying to access one of the „Model“ -Method, which is called within the „ImBackend“ class. It’s possible but a little inconvenient.
First off, create a new plugin/file named „im.extender.php“ for example, in „/plugins/„ directory. Then you simply add the following code in this file:
PHP Code:
<?php
$thisfile = basename(__FILE__, '.php');
register_plugin(
$thisfile,
'Your plugin name',
'0.1',
'Your name',
'your website url',
'An ItemManger backend extension',
'',
''
);
add_action('ImActivated', 'action');
function action($manager) {
if(!class_exists('ImBackend'))
{
return 'Error: ImBackend class does not exist!';
}
class BackendExtended extends ImBackend
{
protected function callModelMethod($method, $args)
{
if($method == 'createCategoryByName')
{
/* Here you can add a new method call (instead of original "createCategoryByName"),
something like that, for example:
*/
return $this->myCreateCategory($args[0], $args[1]);
} else
{
$multiargs = array('deleteItem');
if($method == 'deleteItem' || $method == 'deleteCategory' || $method == 'updateCategory')
{
return $this->manager->{$method}($args[0], $args[1]);
}
return $this->manager->{$method}($args);
}
}
/* Here put your own "Model" method to save Category */
protected function myCreateCategory($cat, $refresh=false)
{
/* Ok, let's copy and paste the code from the original "createCategoryByName" method
located in the "im.model.class.php" file here. Next we'll modify a little the code.
NOTE: You must use "$this->manager" instead of using just "$this->", because you are now in
"ImBackend" and not in "ImModel" class
*/
if(empty($cat)) return false;
if(!is_string($cat)) return false;
if(false !== strpos($cat, '='))
{
$data = explode('=', $cat, 2);
$key = strtolower(trim($data[0]));
$val = trim($data[1]);
if(false !== strpos($key, ' '))
return false;
if($key != 'name')
return false;
$cat = $val;
}
$config = $this->manager->config;
if(strlen($cat) > $config->common->maxcatname)
{
ImMsgReporter::setClause('err_category_name_length', array('count' => $config->common->maxcatname));
return false;
}
// CHECK here category name
$new_cat = new Category();
$new_cat->set('name', str_replace('"', '\'', $cat));
$new_cat->slug = ImModel::toAscii($cat);
// do not save category if name already exists
if(!$this->manager->category->getCategory('name='.safe_slash_html($new_cat->name)))
{
$new_cat->save();
/******* And here is the place for pasting your code: ********/
$fields = $this->manager->getFieldsClass();
// Create the fields file for "each" new category:
$cat_id = $new_cat->get('id');
if(!$fields->fieldsExists($cat_id))
{
$fields->createFields($cat_id);
$fields->init($cat_id);
// Create a custom text field "my_field_name" in the new category if it doesn't exist:
$my_field = $fields->getField('name=my_field_name');
if(!$my_field) {
$newField = $this->manager->newField($cat_id);
$newField->name = 'my_field_name';
$newField->label = 'Your field label';
$newField->position = 1;
$newField->type = 'text';
$newField->save();
}
}
} else
{
ImMsgReporter::setClause('err_category_name_exists', array());
return false;
}
// reinitialize categories
if($refresh) $this->manager->category->init();
ImMsgReporter::setClause('successfull_category_created', array(
'category' => safe_slash_html($new_cat->name))
);
return true;
}
}
$newbackend = new BackendExtended($manager);
$manager->setBackend($newbackend);
}
As the final step activate new plugin in „GetSimple“ Plugin menu.
A big thanks again!