ここの情報は古いです。ご理解頂いた上でお取り扱いください。

source: OpenPNE3/trunk/lib/form/CommunityFileForm.class.php @ 10980

Last change on this file since 10980 was 10980, checked in by ShogoKawahara, 12 years ago

#3705:added ability to delete community image

File size: 2.4 KB
Line 
1<?php
2
3/**
4 * This file is part of the OpenPNE package.
5 * (c) OpenPNE Project (http://www.openpne.jp/)
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file and the NOTICE file that were distributed with this source code.
9 */
10
11/**
12 * Community file form.
13 *
14 * @package    OpenPNE
15 * @subpackage form
16 * @author     Shogo Kawahara <kawahara@tejimaya.com>
17 */
18
19class CommunityFileForm extends sfForm
20{
21  protected
22    $community;
23
24  public function __construct($defaults = array(), $options = array(), $CSRFSecret = null)
25  {
26    return parent::__construct($defaults, $options, false);
27  }
28
29  public function configure()
30  {
31    $this->setCommunity($this->getOption('community'));
32
33    $options = array(
34      'file_src'     => '',
35      'is_image'     => true,
36      'with_delete'  => true,
37      'delete_label' => sfContext::getInstance()->getI18N()->__('Remove the current photo')
38    );
39
40    if (!$this->community->isNew() && $this->community->getFileId())
41    {
42      sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
43      $options['edit_mode'] = true;
44      $options['template'] = get_partial('default/formEditImage', array('image' => $this->community));
45      $this->setValidator('file_delete', new sfValidatorBoolean(array('required' => false)));
46    }
47    else
48    {
49      $options['edit_mode'] = false;
50    }
51
52    $this->setWidget('file', new sfWidgetFormInputFileEditable($options, array('size' => 40)));
53    $this->setValidator('file', new opValidatorImageFile(array('required' => false)));
54
55    $this->widgetSchema->setLabel('file', 'Photo');
56
57    $this->widgetSchema->setNameFormat('community_file[%s]');
58  }
59
60  public function setCommunity($community)
61  {
62    if (!($community instanceof Community))
63    {
64      $community = new Community();
65    }
66    $this->community = $community;
67  }
68
69  public function save()
70  {
71    if ($this->getValue('file'))
72    {
73      if ($this->community->getFile())
74      {
75        $this->community->getFile()->delete(); 
76      }
77
78      $file = new File();
79      $file->setFromValidatedFile($this->getValue('file'));
80      $file->setName('c_'.$this->community->getId().'_'.$file->getName());
81
82      $this->community->setFile($file);
83    }
84    elseif ($this->getValue('file_delete'))
85    {
86      $this->community->getFile()->delete();
87      $this->community->setFile(null);
88    }
89    else
90    {
91      return;
92    }
93
94    $this->community->save();
95  }
96}
Note: See TracBrowser for help on using the repository browser.