Exemple #1
0
 public function actionAddbookmark($id)
 {
     $app = Yii::app();
     $header = 'Добавление закладки';
     if ($app->user->isGuest) {
         $this->render('bookmark-form-guest', array('app' => $app, 'header' => $header, 'header' => $header));
     } else {
         $model = new BookmarkForm();
         $model->user_id = $app->user->id;
         $model->section_id = $id;
         if ($model->section_id == 0) {
             $cancel_url = $this->createUrl('my/index');
         } else {
             $cancel_url = $this->createUrl('my/section', array('id' => $model->section_id));
         }
         $icons_list = Bookmarks::model()->getBookMarkIconsList();
         if (isset($_POST['BookmarkForm'])) {
             $model->attributes = $_POST['BookmarkForm'];
             if ($model->validate()) {
                 $bookmark = new Bookmarks();
                 $bookmark->attributes = $model->attributes;
                 $bookmark->save();
                 Yii::app()->user->setFlash('success', 'Сохранено');
                 //return $this->redirect($this->createUrl('/my/section', array('id'=>$id)));
                 return $this->redirect(array('my/section', 'id' => $id));
             }
             //echo'<pre>';print_r($model);echo'</pre>';die;
         }
         $this->render('bookmark-form', array('app' => $app, 'model' => $model, 'header' => $header, 'cancel_url' => $cancel_url, 'icons_list' => $icons_list));
     }
 }
 public function actionCreate()
 {
     $token = Yii::app()->request->getParam('token');
     $url = Yii::app()->request->getParam('url');
     $title = Yii::app()->request->getParam('title');
     $list = Yii::app()->request->getParam('list');
     $user = Users::model()->find('`key` = :key', ['key' => $token]);
     if ($user === null) {
         $this->renderJSON(['is_error' => true, 'message' => 'Invalid User Token']);
     }
     if (Yii::app()->request->getPost('token') !== $token) {
         $this->renderJSON("Only accepts POST requests");
     }
     $urlValidator = new CUrlValidator();
     if (!$urlValidator->validateValue($url)) {
         $this->renderJSON(['is_error' => true, 'data' => '', 'message' => 'Not valid URL']);
     }
     $bookmark = new Bookmarks();
     if (!empty($list)) {
         $list = $user->lists(['condition' => 'name = :name', 'params' => ['name' => $list]]);
         if (empty($list)) {
             $this->renderJSON(['is_error' => true, 'data' => '', 'message' => 'Not valid list']);
         } else {
             $bookmark->list_id = $list[0]->id;
         }
     }
     if (!empty($title)) {
         $bookmark->title = $title;
     } else {
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $url);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
         $body = curl_exec($curl);
         curl_close($curl);
         if ($body !== false && preg_match('#<title>([^<]+)#i', $body, $match)) {
             $bookmark->title = $match[1];
         }
     }
     $bookmark->user_id = $user->id;
     $bookmark->url = $url;
     $bookmark->save();
     $this->renderJSON(['is_error' => false, 'data' => ['id' => (int) $bookmark->id, 'title' => $bookmark->title, 'list' => $bookmark->list ? $bookmark->list->name : null, 'url' => $bookmark->url], 'message' => 'Success']);
 }
 public function actionSave_bookmark()
 {
     if (Yii::app()->user->isGuest) {
         $this->redirect($this->createAbsoluteUrl('base'));
     }
     if (Yii::app()->request->isAjaxRequest) {
         if (isset($_POST['Bookmarks']['idArticle']) && isset($_POST['Bookmarks']['idMenu'])) {
             $model = new Bookmarks();
             $model->attributes = $_POST['Bookmarks'];
             $model->idUser = Yii::app()->user->id;
             if ($model->save()) {
                 echo CJSON::encode(array('idBookmarks' => $model->idBookmarks));
                 exit;
             } else {
                 echo CJSON::encode(array('error' => 'ok'));
                 exit;
             }
         }
     }
 }
 public function actionAddBySuffix()
 {
     $url = mb_substr(Yii::app()->request->requestUri, 1);
     if (!in_array(mb_substr($url, 0, 7), ['http://', 'https:/'], true)) {
         $url = 'http://' . $url;
     }
     if (mb_strlen($url)) {
         $bookmark = new Bookmarks();
         $bookmark->user_id = Yii::app()->user->id;
         $bookmark->url = $url;
         $bookmark->title = $url;
         if (($host = parse_url($url, PHP_URL_HOST)) !== null) {
             $bookmark->title = $host;
             $curl = curl_init();
             curl_setopt($curl, CURLOPT_URL, $url);
             curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
             curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
             $body = curl_exec($curl);
             curl_close($curl);
             if ($body !== false && preg_match('#<title>([^<]+)#i', $body, $match)) {
                 $bookmark->title = $match[1];
             }
         }
         $subdomain = isset($_SERVER['SUBDOMAIN']) ? $_SERVER['SUBDOMAIN'] : '';
         if (mb_strlen($subdomain)) {
             $list = Yii::app()->user->model->lists(['condition' => 'name = :name', 'params' => ['name' => $subdomain]]);
             if (count($list) == 0) {
                 $list = new Lists();
                 $list->user_id = Yii::app()->user->id;
                 $list->name = $subdomain;
                 $list->save();
             } else {
                 $list = $list[0];
             }
             $bookmark->list_id = $list->id;
         }
         $bookmark->save();
     }
     $this->redirect(['index/index']);
 }
 public function addElem($owner, $content, $type)
 {
     if (!$this->exists(array('condition' => 'owner_id=' . $owner . ' AND content_id=' . $content . ' AND type=' . $type))) {
         $newItem = new Bookmarks();
         $newItem->owner_id = $owner;
         $newItem->content_id = $content;
         $newItem->type = $type;
         $newItem->added = time();
         $newItem->save();
     }
 }