Source of file Image.php
Size: 3,077 Bytes - Last Modified: 2021-12-23T10:48:10+00:00
/var/www/docs.ssmods.com/process/src/src/model/Image.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | <?php namespace Swordfox\Shopify\Model; use GuzzleHttp\Client; use SilverStripe\Assets\Folder; use SilverStripe\Assets\Storage\DBFile; use SilverStripe\Forms\ReadonlyField; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Security; use Swordfox\Shopify\Task\Import; /** * Class Image * @package Swordfox\Shopify\Model * * @property int Sort * @property string ShopifyID * @property string OriginalSrc * * @method Product Product */ class ShopifyImage extends DataObject { private static $table_name = 'ShopifyImage'; private static $db = [ 'Sort' => 'Int', 'ShopifyID' => 'Varchar', 'OriginalSrc' => 'Varchar' ]; private static $data_map = [ 'id' => 'ShopifyID', 'alt' => 'Title', 'position' => 'Sort', 'src' => 'OriginalSrc', 'created_at' => 'Created', 'updated_at' => 'LastEdited' ]; private static $has_one = [ 'Product' => Product::class ]; private static $has_many = [ 'Variants' => ProductVariant::class ]; private static $indexes = [ 'ShopifyID' => true ]; private static $summary_fields = [ 'Title', 'ShopifyID' ]; public function getCMSFields() { $fields = parent::getCMSFields(); return $fields; } /** * Creates a new Shopify Image from the given data * * @param $shopifyImage * @return Image * @throws \SilverStripe\ORM\ValidationException * @throws \GuzzleHttp\Exception\GuzzleException */ public static function findOrMakeFromShopifyData($shopifyImage) { if (!$image = self::getByShopifyID($shopifyImage->id)) { $image = self::create(); } $map = self::config()->get('data_map'); Import::loop_map($map, $image, $shopifyImage); // import the image if the source has changed if ($image->isChanged('OriginalSrc', DataObject::CHANGE_VALUE)) { $folder = isset($shopifyImage->product_id) ? $shopifyImage->product_id : 'collection'; //$image->downloadImage($image->OriginalSrc, "shopify/$folder"); } $image->write(); return $image; } public static function getByShopifyID($shopifyId) { return DataObject::get_one(self::class, ['ShopifyID' => $shopifyId]); } /** * Download the image from the shopify CDN * * @param $src * @param $folder * @throws \GuzzleHttp\Exception\GuzzleException */ private function downloadImage($src, $folder) { $client = new Client(['http_errors' => false]); $request = $client->request('GET', $src); $folder = Folder::find_or_make($folder); $sourcePath = pathinfo($src); $fileName = explode('?', $sourcePath['basename'])[0]; $this->setFromString($request->getBody()->getContents(), $fileName); $this->ParentID = $folder->ID; $this->OwnerID = ($user = Security::getCurrentUser()) ? $user->ID : 0; $this->publishFile(); } } |