View file upload/src/addons/AddonsLab/Core/Xf2/Service/ContentCreator.php

File size: 5.91Kb
<?php
/** 
This software is furnished under a license and may be used and copied
only  in  accordance  with  the  terms  of such  license and with the
inclusion of the above copyright notice.  This software  or any other
copies thereof may not be provided or otherwise made available to any
other person.  No title to and  ownership of the  software is  hereby
transferred.                                                         
                                                                     
You may not reverse  engineer, decompile, defeat  license  encryption
mechanisms, or  disassemble this software product or software product
license.  AddonsLab may terminate this license if you don't comply with
any of these terms and conditions.  In such event,  licensee  agrees 
to return licensor  or destroy  all copies of software  upon termination 
of the license.
*/


namespace AddonsLab\Core\Xf2\Service;

use XF\Entity\AbstractField;
use XF\Entity\AbstractNode;
use XF\Entity\Post;
use XF\Mvc\Entity\Entity;

class ContentCreator
{
    /**
     * Creates a node with a title, if it does not exist.
     * @param $nodeTitle
     * @param array $nodeInfo
     * @param array $dataInfo
     * @return null|\XF\Entity\Node|\XF\Mvc\Entity\Entity
     * @throws \Exception
     * @throws \XF\PrintableException
     */
    public function assertNode($nodeTitle, $nodeInfo = array(), $dataInfo = array())
    {
        $node = \XF::finder('XF:Node')->where('title', $nodeTitle)->fetchOne();
        if ($node === null)
        {
            $node = \XF::em()->create('XF:Node');
            $node->title = $nodeTitle;
            $node->node_type_id = 'Forum';
            $node->display_order = 999;
            $node->parent_node_id = 0;
            $node->description = '';

            $node->bulkSet($nodeInfo);

            /** @var AbstractNode $data */
            $data = $node->getDataRelationOrDefault();

            $data->allow_posting = 1;
            $data->bulkSet($dataInfo);

            $node->addCascadedSave($data);
            $node->save();
        }

        return $node;
    }

    public function attachFileToPost(Post $post, $filePath)
    {
        return $this->attachFile($post, 'post', $filePath);
    }

    public function attachFile(Entity $entity, $contentType, $filePath)
    {
        $handler = \XF::repository('XF:Attachment')->getAttachmentHandler($contentType);
        $hash = md5(mt_rand());
        $manipulator = new \XF\Attachment\Manipulator($handler, \XF::repository('XF:Attachment'), array(), $hash);
        $upload = $this->request->getFile('upload', false, false);
    }


    /**
     * Ensures custom fields with the info provided are created
     * @param array $fields field_id=>field_info mapping
     * @param array $nodes The nodes to associate the created fields with
     * @param int $displayOrder
     */
    public function assertThreadCustomFields(array $fields, array $nodes, $displayOrder = 1)
    {
        $this->assertCustomFields(
            $fields,
            $nodes,
            'XF:ThreadField',
            'XF:ForumField',
            $displayOrder
        );
    }

    public function deleteThreadCustomFields(array $fields)
    {
        $this->deleteCustomFields($fields, 'XF:ThreadField');
    }

    public function deleteCustomFields(array $fields, $fieldEntityName)
    {
        foreach ($fields AS $field_id => $fieldInfo)
        {
            $field = \XF::finder($fieldEntityName)->whereId($field_id)->fetchOne();

            if ($field)
            {
                $field->delete();
            }
        }
    }

    public function assertCustomFields(
        array $fields,
        array $fieldHolders,
        $fieldEntityName,
        $holderRepositoryName,
        $displayOrder = 1
    )
    {
        foreach ($fields AS $field_id => $fieldInfo)
        {
            $field = \XF::finder($fieldEntityName)->whereId($field_id)->fetchOne();

            if (!$field)
            {
                $field = $this->createCustomField($field_id, $fieldInfo, $fieldEntityName, $displayOrder * 10);
            }

            $categoryIds = array_map(function (Entity $entity)
            {
                return $entity->getEntityId();
            }, $fieldHolders);
            
            $this->associateFieldWithHolder(
                $holderRepositoryName,
                $field,
                $categoryIds
            );
            $displayOrder++;
        }
    }

    public function associateFieldWithHolder($repositoryShortName, $field, $categoryIds)
    {
        /** @var \XF\Repository\AbstractFieldMap $repo */
        $repo = \XF::repository($repositoryShortName);
        $repo->updateFieldAssociations($field, $categoryIds);
    }

    public function createCustomField($field_id, $fieldInfo, $fieldEntityName, $displayOrder)
    {
        /** @var AbstractField $field */
        $field = \XF::em()->create($fieldEntityName);
        $field->field_id = $field_id;
        $field->display_order = $displayOrder;
        /** @var \XF\Entity\Phrase $titlePhrase */
        $titlePhrase = $field->getMasterPhrase(true);
        $titlePhrase->phrase_text = $fieldInfo['title'];
        $field->field_type = $fieldInfo['field_type'];

        $descriptionPhrase = $field->getMasterPhrase(false);

        if (isset($fieldInfo['description']))
        {
            $descriptionPhrase->phrase_text = $fieldInfo['description'];
        }
        else
        {
            $descriptionPhrase->phrase_text = '';
        }

        if (isset($fieldInfo['match_type']))
        {
            $field->match_type = $fieldInfo['match_type'];
        }
        if (isset($fieldInfo['match_params']))
        {
            $field->match_params = $fieldInfo['match_params'];
        }
        if (isset($fieldInfo['field_choices']))
        {
            $field->field_choices = $fieldInfo['field_choices'];
        }

        $field->save();

        $titlePhrase->save();
        $descriptionPhrase->save();

        return $field;
    }
}