      1: <?php
      2:
      3: // include the config file
      4: if(!isset($config)) {
      5:     require_once '../util/AlphaConfig.inc';
      6:     $config = AlphaConfig::getInstance();
      7:
      8:     require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
      9: }
     10:
     11: /**
     12:  *
     13:  * Controller used to edit TagObjects related to the BO indicated in the supplied
     14:  * GET vars (bo and oid).
     15:  *
     16:  * @package alpha::controller
     17:  * @since 1.0
     18:  * @author John Collins <dev@alphaframework.org>
     19:  * @version $Id: EditTags.php 1548 2012-07-29 17:07:07Z alphadevx $
     20:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     21:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     22:  * All rights reserved.
     23:  *
     24:  * <pre>
     25:  * Redistribution and use in source and binary forms, with or
     26:  * without modification, are permitted provided that the
     27:  * following conditions are met:
     28:  *
     29:  * * Redistributions of source code must retain the above
     30:  *   copyright notice, this list of conditions and the
     31:  *   following disclaimer.
     32:  * * Redistributions in binary form must reproduce the above
     33:  *   copyright notice, this list of conditions and the
     34:  *   following disclaimer in the documentation and/or other
     35:  *   materials provided with the distribution.
     36:  * * Neither the name of the Alpha Framework nor the names
     37:  *   of its contributors may be used to endorse or promote
     38:  *   products derived from this software without specific
     39:  *   prior written permission.
     40:  *
     41:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     42:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     43:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     44:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     45:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     46:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     47:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     48:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     49:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     50:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     51:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     52:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     53:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     54:  * </pre>
     55:  *
     56:  */
     57: class EditTags extends Edit implements AlphaControllerInterface {
     58:     /**
     59:      * Trace logger
     60:      *
     61:      * @var Logger
     62:      * @since 1.0
     63:      */
     64:     private static $logger = null;
     65:
     66:     /**
     67:      * constructor to set up the object
     68:      *
     69:      * @since 1.0
     70:      */
     71:     public function __construct() {
     72:         self::$logger = new Logger('EditTags');
     73:         self::$logger->debug('>>__construct()');
     74:
     75:         // ensure that the super class constructor is called, indicating the rights group
     76:         parent::__construct('Admin');
     77:
     78:         // set up the title and meta details
     79:         $this->setTitle('Editing Tags');
     80:         $this->setDescription('Page to edit tags.');
     81:         $this->setKeywords('edit,tags');
     82:
     83:         $this->BO = new TagObject();
     84:
     85:         self::$logger->debug('<<__construct');
     86:     }
     87:
     88:     /**
     89:      * Handle GET requests
     90:      *
     91:      * @param array $params
     92:      * @throws IllegalArguementException
     93:      * @since 1.0
     94:      * @throws FileNotFoundException
     95:      */
     96:     public function doGET($params) {
     97:         self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
     98:
     99:         global $config;
    100:
    101:         echo AlphaView::displayPageHead($this);
    102:
    103:         // ensure that a bo is provided
    104:         if (isset($params['bo']))
    105:             $BOName = $params['bo'];
    106:         else
    107:             throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    108:
    109:         // ensure that a OID is provided
    110:         if (isset($params['oid']))
    111:             $BOoid = $params['oid'];
    112:         else
    113:             throw new IllegalArguementException('Could not load the tag objects as an oid was not supplied!');
    114:
    115:         try {
    116:             AlphaDAO::loadClassDef($BOName);
    117:             $this->BO = new $BOName;
    118:             $this->BO->load($BOoid);
    119:
    120:             $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
    121:
    122:             AlphaDAO::disconnect();
    123:
    124:             echo '<table cols="3" class="edit_view">';
    125:             echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST">';
    126:             echo '<tr><td colspan="3"><h3>The following tags were found:</h3></td></tr>';
    127:
    128:             foreach($tags as $tag) {
    129:                 echo '<tr><td>';
    130:                 $labels = $tag->getDataLabels();
    131:                 echo $labels['content'];
    132:                 echo '</td><td>';
    133:
    134:                 $temp = new StringBox($tag->getPropObject('content'), $labels['content'], 'content_'.$tag->getID(), '');
    135:                 echo $temp->render(false);
    136:                 echo '</td><td>';
    137:
    138:                 $js = "$('#dialogDiv').text('Are you sure you wish to delete this tag?');
    139:                             $('#dialogDiv').dialog({
    140:                             buttons: {
    141:                                 'OK': function(event, ui) {
    142:                                     $('#deleteOID').attr('value', '".$tag->getID()."');
    143:                                     $('#deleteForm').submit();
    144:                                 },
    145:                                 'Cancel': function(event, ui) {
    146:                                     $(this).dialog('close');
    147:                                 }
    148:                             }
    149:                         })
    150:                         $('#dialogDiv').dialog('open');
    151:                         return false;";
    152:                 $button = new Button($js, "Delete", "delete".$tag->getID()."But");
    153:                 echo $button->render().'</td></tr>';
    154:             }
    155:
    156:             echo '<tr><td colspan="3"><h3>Add a new tag:</h3></td></tr>';
    157:             echo '<tr><td>';
    158:             echo 'New tag';
    159:             echo '</td><td>';
    160:             $temp = new StringBox(new String(), 'New tag', 'new_value', '');
    161:             echo $temp->render(false);
    162:             echo '</td><td></td></tr>';
    163:
    164:             echo '<tr><td colspan="3">';
    165:
    166:             $temp = new Button('submit', 'Save', 'saveBut');
    167:             echo $temp->render();
    168:             echo '&nbsp;&nbsp;';
    169:             $temp = new Button("document.location = '".FrontController::generateSecureURL('act=Edit&bo='.$params['bo'].'&oid='.$params['oid'])."'", 'Back to Object', 'cancelBut');
    170:             echo $temp->render();
    171:             echo '</td></tr>';
    172:
    173:             echo AlphaView::renderSecurityFields();
    174:
    175:             echo '</form></table>';
    176:
    177:             echo AlphaView::renderDeleteForm();
    178:
    179:         }catch(BONotFoundException $e) {
    180:             $msg = 'Unable to load the BO of id ['.$params['oid'].'], error was ['.$e->getMessage().']';
    181:             self::$logger->error($msg);
    182:             throw new FileNotFoundException($msg);
    183:         }
    184:
    185:         echo AlphaView::displayPageFoot($this);
    186:
    187:         self::$logger->debug('<<doGET');
    188:     }
    189:
    190:     /**
    191:      * Handle POST requests
    192:      *
    193:      * @param array $params
    194:      * @throws SecurityException
    195:      * @throws IllegalArguementException
    196:      * @since 1.0
    197:      */
    198:     public function doPOST($params) {
    199:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    200:
    201:         try {
    202:             // check the hidden security fields before accepting the form POST data
    203:             if(!$this->checkSecurityFields())
    204:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    205:
    206:             // ensure that a bo is provided
    207:             if (isset($params['bo']))
    208:                 $BOName = $params['bo'];
    209:             else
    210:                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    211:
    212:             // ensure that a OID is provided
    213:             if (isset($params['oid']))
    214:                 $BOoid = $params['oid'];
    215:             else
    216:                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    217:
    218:             if (isset($params['saveBut'])) {
    219:                 try {
    220:                     AlphaDAO::loadClassDef($BOName);
    221:                     $this->BO = new $BOName;
    222:                     $this->BO->load($BOoid);
    223:
    224:                     $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
    225:
    226:                     AlphaDAO::begin();
    227:
    228:                     foreach ($tags as $tag) {
    229:                         $tag->set('content', TagObject::cleanTagContent($params['content_'.$tag->getID()]));
    230:                         $tag->save();
    231:                     }
    232:
    233:                     // handle new tag if posted
    234:                     if(isset($params['new_value']) && trim($params['new_value']) != '') {
    235:                         $newTag = new TagObject();
    236:                         $newTag->set('content', TagObject::cleanTagContent($params['new_value']));
    237:                         $newTag->set('taggedOID', $BOoid);
    238:                         $newTag->set('taggedClass', $BOName);
    239:                         $newTag->save();
    240:                     }
    241:
    242:                     AlphaDAO::commit();
    243:
    244:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
    245:
    246:                     $this->doGET($params);
    247:                 }catch (ValidationException $e) {
    248:                     /*
    249:                      * The unique key has most-likely been violated because this BO is already tagged with this
    250:                      * value.
    251:                      */
    252:                     AlphaDAO::rollback();
    253:
    254:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved due to duplicate tag values, please try again.'));
    255:
    256:                     $this->doGET($params);
    257:                 }catch (FailedSaveException $e) {
    258:                     self::$logger->error('Unable to save the tags of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    259:                     AlphaDAO::rollback();
    260:
    261:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved, please check the application logs.'));
    262:
    263:                     $this->doGET($params);
    264:                 }
    265:
    266:                 AlphaDAO::disconnect();
    267:             }
    268:
    269:             if (!empty($params['deleteOID'])) {
    270:                 try {
    271:                     AlphaDAO::loadClassDef($BOName);
    272:                     $this->BO = new $BOName;
    273:                     $this->BO->load($BOoid);
    274:
    275:                     $tag = new TagObject();
    276:                     $tag->load($params['deleteOID']);
    277:                     $content = $tag->get('content');
    278:
    279:                     AlphaDAO::begin();
    280:
    281:                     $tag->delete();
    282:
    283:                     AlphaDAO::commit();
    284:
    285:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' deleted successfully.'));
    286:
    287:                     $this->doGET($params);
    288:                 }catch(AlphaException $e) {
    289:                     self::$logger->error('Unable to delete the tag of id ['.$params['deleteOID'].'], error was ['.$e->getMessage().']');
    290:                     AlphaDAO::rollback();
    291:
    292:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' not deleted, please check the application logs.'));
    293:
    294:                     $this->doGET($params);
    295:                 }
    296:
    297:                 AlphaDAO::disconnect();
    298:             }
    299:         }catch(SecurityException $e) {
    300:
    301:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    302:
    303:             self::$logger->warn($e->getMessage());
    304:         }catch(IllegalArguementException $e) {
    305:             self::$logger->error($e->getMessage());
    306:         }catch(BONotFoundException $e) {
    307:             self::$logger->warn($e->getMessage());
    308:
    309:             $this->setStatusMessage(AlphaView::displayErrorMessage('Failed to load the requested item from the database!'));
    310:         }
    311:
    312:         self::$logger->debug('<<doPOST');
    313:     }
    314:
    315:     /**
    316:      * Using this callback to blank the new_value field when the page loads, regardless of anything being posted
    317:      *
    318:      * @return string
    319:      * @since 1.0
    320:      */
    321:     public function during_displayPageHead_callback() {
    322:         $html = '';
    323:         $html .= '<script language="javascript">';
    324:         $html .= 'function clearNewField() {';
    325:         $html .= '  document.getElementById("new_value").value = "";';
    326:         $html .= '}';
    327:         $html .= 'addOnloadEvent(clearNewField);';
    328:         $html .= '</script>';
    329:         return $html;
    330:     }
    331: }
    332:
    333: // now build the new controller if this file is called directly
    334: if ('EditTags.php' == basename($_SERVER['PHP_SELF'])) {
    335:     $controller = new EditTags();
    336:
    337:     if(!empty($_POST)) {
    338:         $controller->doPOST($_REQUEST);
    339:     }else{
    340:         $controller->doGET($_GET);
    341:     }
    342: }
    343:
    344: ?>