Index: trunk/core/units/general/cat_dbitem.php =================================================================== diff -u -r3282 -r3543 --- trunk/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 3282) +++ trunk/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 3543) @@ -4,16 +4,23 @@ var $CustomFields = Array(); + /** + * Category path, needed for import + * + * @var Array + */ + var $CategoryPath = Array(); + function Init($prefix, $special, $event_params = null) { parent::Init($prefix, $special, $event_params); $item_type = $this->Application->getUnitOption($this->Prefix, 'ItemType'); - $sql = 'SELECT CustomFieldId, FieldName FROM '.TABLE_PREFIX.'CustomField WHERE Type = %s'; - $this->CustomFields = $this->Conn->GetCol( sprintf($sql, $item_type), 'FieldName' ); + $sql = 'SELECT CustomFieldId, FieldName FROM '.TABLE_PREFIX.'CustomField WHERE Type = '.$item_type; + $this->CustomFields = $this->Conn->GetCol($sql, 'FieldName'); } - function Create() + function Create($force_id=false, $system_create=false) { if (!$this->Validate()) return false; @@ -23,7 +30,7 @@ $this->SetDBField('CreatedById', $this->Application->GetVar('u_id')); $this->generateFilename(); - $ret = parent::Create(); + $ret = parent::Create($force_id, $system_create); if($ret) { if ( kTempTablesHandler::IsTempTable($this->TableName) ) { @@ -40,15 +47,15 @@ return $ret; } - function Update($id=null) + function Update($id=null, $system_update=false) { $this->checkFilename(); $this->VirtualFields['ResourceId'] = true; $this->SetDBField('Modified', adodb_mktime() ); $this->SetDBField('ModifiedById', $this->Application->GetVar('u_id')); $this->generateFilename(); - return parent::Update($id); + return parent::Update($id, $system_update); } function checkFilename() @@ -270,6 +277,78 @@ if ( $name != $this->GetDBField('Filename') ) $this->SetDBField('Filename', $name); } + + /** + * Check if value is set for required field + * + * @param string $field field name + * @param Array $params field options from config + * @return bool + * @access private + */ + function ValidateRequired($field, $params) + { + $res = true; + $error_field = isset($params['error_field']) ? $params['error_field'] : $field; + if ( getArrayValue($params,'required') ) + { + if (getArrayValue($params, 'formatter') == 'kUploadFormatter') + { + $value = $this->GetDBField($field); + $res = is_array($value) && $value['size'] ? true : false; + } + else { + $res = ( (string) $this->FieldValues[$field] != ''); + } + } + if (!$res) $this->FieldErrors[$error_field]['pseudo'] = 'required'; + return $res; + } + + /** + * Adds item to other category + * + * @param int $category_id + * @param bool $is_primary + */ + function assignToCategory($category_id, $is_primary = false) + { + $check_sql = ' SELECT CategoryId + FROM '.TABLE_PREFIX.'CategoryItems + WHERE (ItemResourceId = '.$this->GetDBField('ResourceId').') AND (PrimaryCat = 1)'; + $primary_category_id = $this->Conn->GetOne($check_sql); + if (!$primary_category_id) $is_primary = true; + + if ($primary_category_id == $category_id) return ; + + $sql = 'INSERT INTO '.TABLE_PREFIX.'CategoryItems (CategoryId,ItemResourceId,PrimaryCat) VALUES (%s,%s,%s)'; + $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId'), $is_primary ? 1 : 0) ); + } + + /** + * Removes item from category specified + * + * @param int $category_id + */ + function removeFromCategory($category_id) + { + $sql = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems WHERE (CategoryId = %s) AND (ItemResourceId = %s)'; + $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId')) ); + } + + /** + * Returns list of columns, that could exist in imported file + * + * @return Array + */ + function getPossibleExportColumns() + { + static $columns = null; + if (!is_array($columns)) { + $columns = array_merge($this->Fields['AvailableColumns']['options'], $this->Fields['ExportColumns']['options']); + } + return $columns; + } } ?> \ No newline at end of file