actions.class.php 6.14 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

require_once dirname(__FILE__).'/../lib/videoGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/videoGeneratorHelper.class.php';

/**
 * video actions.
 *
 * @package    sf
 * @subpackage video
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class videoActions extends autoVideoActions
{
    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
            try
            {
                $video = $form->save();

                $video_custom = $request->getParameter('video_custom');
                if(count($video_custom['tags']) > 0)
                {

                    foreach($video_custom['tags'] as $tag_id)
                    {
                        $newProductTags = new VideoTags();
                        $newProductTags->setVideoId($video->getId());
                        $newProductTags->setTagId($tag_id);
                        $newProductTags->save();
                    }

                }
            }
            catch (Doctrine_Validator_Exception $e)
            {
                $errorStack = $form->getObject()->getErrorStack();
                $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
                foreach ($errorStack as $field => $errors)
                {
                    $message .= "$field (" . implode(", ", $errors) . "), ";
                }
                $message = trim($message, ', ');
                $this->getUser()->setFlash('error', $message);
                return sfView::SUCCESS;
            }
            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $video)));
            if ($request->hasParameter('_save_and_add'))
            {
                $this->getUser()->setFlash('notice', $notice.' You can add another one below.');
                $this->redirect('@video_new');
            }
            elseif ($request->hasParameter('_save_and_list'))
            {
                $this->redirect('@video');
            }
            else
            {
                if(!$request->isXmlHttpRequest())
                {
                    $this->getUser()->setFlash('notice', $notice);
                    $this->redirect($request->hasParameter('return') ? $request->getParameter('return') : array('sf_route' => 'video_edit', 'sf_subject' => $video));
                }
            }
        }
        else
        {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
            $this->getResponse()->addHttpMeta('Sf-Form-Error', '1', true);
        }
    }

    public function executeGdata(sfWebRequest $request)
    {
        if($request->getParameter('type') == 'youtube')
        {

            $ch = curl_init('https://www.googleapis.com/youtube/v3/videos?id=' . $request->getParameter("id") . '&key=AIzaSyACY3IFamw5LkQFiIVww38beV7O7_oHNhM&part=snippet');
            curl_setopt($ch, CURLOPT_USERAGENT, 'IE20');
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
            $text = curl_exec($ch);
            $json = json_decode($text, true);



            if(is_array($json))
            {


                $ch = curl_init($json['items'][0]['snippet']['thumbnails']['high']['url']);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
                $content = curl_exec($ch);
                curl_close($ch);
                if (file_exists('../upload/' . $request->getParameter('id') . '.jpg')) {
                    unlink('../upload/' . $request->getParameter('id') . '.jpg');
                }
                $fp = fopen('../upload/' . $request->getParameter('id') . '.jpg','x');
                fwrite($fp, $content);
                fclose($fp);

                $request->setParameter('key', 'video_yt');
                $request->setParameter('file', $request->getParameter('id') . '.jpg');
                $resize = Page::resize($request, true);
                echo json_encode(array('title' => $json['items'][0]['snippet']['title'], 'image' => $resize));
            }
        }
        elseif($request->getParameter('type') == 'vk')
        {

            $ch = curl_init('https://vk.com/video_ext.php?' . str_replace(array(':', ';'), array('=', '&'), $request->getParameter('id')));
            curl_setopt($ch, CURLOPT_USERAGENT, 'IE20');
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
            $page = curl_exec($ch);

            preg_match('/<video id="video_player" poster="(.*)" preload="none"/', $page, $matches);


            $ch = curl_init($matches[1]);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
            $content = curl_exec($ch);
            curl_close($ch);
            if (file_exists('../upload/' . $request->getParameter('id') . '.jpg')) {
                unlink('../upload/' . $request->getParameter('id') . '.jpg');
            }
            $fp = fopen('../upload/' . $request->getParameter('id') . '.jpg','x');
            fwrite($fp, $content);
            fclose($fp);



            $request->setParameter('key', 'video_yt');
            $request->setParameter('file', $request->getParameter('id') . '.jpg');
            $resize = Page::resize($request, true);

            //preg_match('/var video_title = \'(.*)\';/', $page, $matches);

            echo json_encode(array('image' => $resize));
        }
        return sfView::NONE;
    }

    protected function buildQuery()
    {
        $query = parent::buildQuery();
        $query->leftJoin("r.Translation t");
        return $query;
    }
}