Application->getUnitOption('#file', 'Clones'); $clones[$event->MasterEvent->Prefix.'-file'] = Array ( 'ParentPrefix' => $event->MasterEvent->Prefix, ); $this->Application->setUnitOption('#file', 'Clones', $clones); } /** * Remembers user, who is created file record. Makes file primary if no other files are uploaded. * * @param kEvent $event */ function OnBeforeItemCreate(&$event) { parent::OnBeforeItemCreate($event); $object =& $event->getObject(); $this->resetPrimary($object); if (!$this->primaryFileFound($object)) { $object->SetDBField('IsPrimary', 1); $object->SetDBField('Status', STATUS_ACTIVE); } $object->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); } /** * Resets primary file mark when more then one file is marked as primary * * @param kEvent $event */ function OnBeforeItemUpdate(&$event) { $object =& $event->getObject(); $this->resetPrimary($object); if (!$object->GetDBField('FileName')) { $object->SetDBField('FileName', basename($object->GetDBField('FilePath'))); } } /** * Checks for primary file along all files with same main item * * @param kDBItem $object * @return int */ function primaryFileFound(&$object) { $sql = 'SELECT '.$object->IDField.' FROM '.$object->TableName.' WHERE (IsPrimary = 1) AND ('.$this->getParentClause($object).')'; return $this->Conn->GetOne($sql); } /** * Resets primary mark from all item files * * @param kDBItem $object */ function resetPrimary(&$object) { if (!$object->GetDBField('IsPrimary')) { return ; } $sql = 'UPDATE '.$object->TableName.' SET IsPrimary = 0 WHERE '.$this->getParentClause($object); $this->Conn->Query($sql); $object->SetDBField('Status', STATUS_ACTIVE); } /** * Makes selected file primary * * @param kEvent $event */ function OnSetPrimary(&$event) { $ids = $this->StoreSelectedIDs($event); if (!$ids) { return ; } $object =& $event->getObject( Array('skip_autoload' => true) ); $object->Load( array_shift($ids) ); $object->SetDBField('IsPrimary', 1); $object->Update(); $this->clearSelectedIDs($event); } /** * Don't allow to delete other user's messages * * @param kEvent $event */ function customProcessing(&$event, $type) { parent::customProcessing($event, $type); if ($event->Name == 'OnMassDelete' && $type == 'before') { $ids = $event->getEventParam('ids'); if ($ids) { $object =& $event->getObject(); /* @var $object kDBItem */ $primary_file_id = $this->primaryFileFound($object); if ($primary_file_id && ($primary_index = array_search($primary_file_id, $ids))) { $sql = 'SELECT COUNT(*) FROM '.$object->TableName.' WHERE (IsPrimary = 0) AND ('.$this->getParentClause($object).')'; $non_primary_found = $this->Conn->Query($sql); if ($non_primary_found) { // non-primary files found for same main item -> don't delete primary file until all non-primary files are deleted unset($ids[$primary_index]); } } $event->setEventParam('ids', $ids); } } } function SetCustomQuery(&$event) { parent::SetCustomQuery($event); $object =& $event->getObject(); if (!$this->Application->IsAdmin()) { $object->addFilter('active_filter', '%1$s.Status = '.STATUS_ACTIVE); } } /** * Returns sql clause, that links file to it's parent item * * @param kDBItem $object * @return string */ function getParentClause(&$object) { $parent_info = $object->getLinkedInfo($object->Special); return $parent_info['ForeignKey'].' = '.$parent_info['ParentId']; } } ?>