      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 1757 2014-04-09 22:18:37Z alphadevx $
     20:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     21:  * @copyright Copyright (c) 2014, 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:         $message = $this->getStatusMessage();
    104:         if(!empty($message))
    105:             echo $message;
    106:
    107:         // ensure that a bo is provided
    108:         if (isset($params['bo']))
    109:             $BOName = $params['bo'];
    110:         else
    111:             throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    112:
    113:         // ensure that a OID is provided
    114:         if (isset($params['oid']))
    115:             $BOoid = $params['oid'];
    116:         else
    117:             throw new IllegalArguementException('Could not load the tag objects as an oid was not supplied!');
    118:
    119:         try {
    120:             AlphaDAO::loadClassDef($BOName);
    121:             $this->BO = new $BOName;
    122:             $this->BO->load($BOoid);
    123:
    124:             $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
    125:
    126:             AlphaDAO::disconnect();
    127:
    128:             echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST" accept-charset="UTF-8">';
    129:             echo '<h3>The following tags were found:</h3>';
    130:
    131:             foreach($tags as $tag) {
    132:                 $labels = $tag->getDataLabels();
    133:
    134:                 $temp = new StringBox($tag->getPropObject('content'), $labels['content'], 'content_'.$tag->getID(), '');
    135:                 echo $temp->render(false);
    136:
    137:                 $js = "if(window.jQuery) {
    138:                     BootstrapDialog.show({
    139:                         title: 'Confirmation',
    140:                         message: 'Are you sure you wish to delete this tag?',
    141:                         buttons: [
    142:                             {
    143:                                 icon: 'glyphicon glyphicon-remove',
    144:                                 label: 'Cancel',
    145:                                 cssClass: 'btn btn-default btn-xs',
    146:                                 action: function(dialogItself){
    147:                                     dialogItself.close();
    148:                                 }
    149:                             },
    150:                             {
    151:                                 icon: 'glyphicon glyphicon-ok',
    152:                                 label: 'Okay',
    153:                                 cssClass: 'btn btn-default btn-xs',
    154:                                 action: function(dialogItself) {
    155:                                     $('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$tag->getID()."');
    156:                                     $('#deleteForm').submit();
    157:                                     dialogItself.close();
    158:                                 }
    159:                             }
    160:                         ]
    161:                     });
    162:                 }";
    163:                 $button = new Button($js, "Delete", "delete".$tag->getID()."But");
    164:                 echo $button->render();
    165:             }
    166:
    167:             echo '<h3>Add a new tag:</h3>';
    168:
    169:             $temp = new StringBox(new String(), 'New tag', 'new_value', '');
    170:             echo $temp->render(false);
    171:
    172:
    173:             $temp = new Button('submit', 'Save', 'saveBut');
    174:             echo $temp->render();
    175:             echo '&nbsp;&nbsp;';
    176:             $temp = new Button("document.location = '".FrontController::generateSecureURL('act=Edit&bo='.$params['bo'].'&oid='.$params['oid'])."'", 'Back to Object', 'cancelBut');
    177:             echo $temp->render();
    178:
    179:             echo AlphaView::renderSecurityFields();
    180:
    181:             echo '</form>';
    182:
    183:             echo AlphaView::renderDeleteForm();
    184:
    185:         }catch(BONotFoundException $e) {
    186:             $msg = 'Unable to load the BO of id ['.$params['oid'].'], error was ['.$e->getMessage().']';
    187:             self::$logger->error($msg);
    188:             throw new FileNotFoundException($msg);
    189:         }
    190:
    191:         echo AlphaView::displayPageFoot($this);
    192:
    193:         self::$logger->debug('<<doGET');
    194:     }
    195:
    196:     /**
    197:      * Handle POST requests
    198:      *
    199:      * @param array $params
    200:      * @throws SecurityException
    201:      * @throws IllegalArguementException
    202:      * @since 1.0
    203:      */
    204:     public function doPOST($params) {
    205:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    206:
    207:         try {
    208:             // check the hidden security fields before accepting the form POST data
    209:             if(!$this->checkSecurityFields())
    210:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    211:
    212:             // ensure that a bo is provided
    213:             if (isset($params['bo']))
    214:                 $BOName = $params['bo'];
    215:             else
    216:                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    217:
    218:             // ensure that a OID is provided
    219:             if (isset($params['oid']))
    220:                 $BOoid = $params['oid'];
    221:             else
    222:                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    223:
    224:             if (isset($params['saveBut'])) {
    225:                 try {
    226:                     AlphaDAO::loadClassDef($BOName);
    227:                     $this->BO = new $BOName;
    228:                     $this->BO->load($BOoid);
    229:
    230:                     $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
    231:
    232:                     AlphaDAO::begin();
    233:
    234:                     foreach ($tags as $tag) {
    235:                         $tag->set('content', TagObject::cleanTagContent($params['content_'.$tag->getID()]));
    236:                         $tag->save();
    237:                         self::$logger->action('Saved tag '.$tag->get('content').' on '.$BOName.' instance with OID '.$BOoid);
    238:                     }
    239:
    240:                     // handle new tag if posted
    241:                     if(isset($params['new_value']) && trim($params['new_value']) != '') {
    242:                         $newTag = new TagObject();
    243:                         $newTag->set('content', TagObject::cleanTagContent($params['new_value']));
    244:                         $newTag->set('taggedOID', $BOoid);
    245:                         $newTag->set('taggedClass', $BOName);
    246:                         $newTag->save();
    247:                         self::$logger->action('Created a new tag '.$newTag->get('content').' on '.$BOName.' instance with OID '.$BOoid);
    248:                     }
    249:
    250:                     AlphaDAO::commit();
    251:
    252:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
    253:
    254:                     $this->doGET($params);
    255:                 }catch (ValidationException $e) {
    256:                     /*
    257:                      * The unique key has most-likely been violated because this BO is already tagged with this
    258:                      * value.
    259:                      */
    260:                     AlphaDAO::rollback();
    261:
    262:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved due to duplicate tag values, please try again.'));
    263:
    264:                     $this->doGET($params);
    265:                 }catch (FailedSaveException $e) {
    266:                     self::$logger->error('Unable to save the tags of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    267:                     AlphaDAO::rollback();
    268:
    269:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved, please check the application logs.'));
    270:
    271:                     $this->doGET($params);
    272:                 }
    273:
    274:                 AlphaDAO::disconnect();
    275:             }
    276:
    277:             if (!empty($params['deleteOID'])) {
    278:                 try {
    279:                     AlphaDAO::loadClassDef($BOName);
    280:                     $this->BO = new $BOName;
    281:                     $this->BO->load($BOoid);
    282:
    283:                     $tag = new TagObject();
    284:                     $tag->load($params['deleteOID']);
    285:                     $content = $tag->get('content');
    286:
    287:                     AlphaDAO::begin();
    288:
    289:                     $tag->delete();
    290:
    291:                     self::$logger->action('Deleted tag '.$content.' on '.$BOName.' instance with OID '.$BOoid);
    292:
    293:                     AlphaDAO::commit();
    294:
    295:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' deleted successfully.'));
    296:
    297:                     $this->doGET($params);
    298:                 }catch(AlphaException $e) {
    299:                     self::$logger->error('Unable to delete the tag of id ['.$params['deleteOID'].'], error was ['.$e->getMessage().']');
    300:                     AlphaDAO::rollback();
    301:
    302:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' not deleted, please check the application logs.'));
    303:
    304:                     $this->doGET($params);
    305:                 }
    306:
    307:                 AlphaDAO::disconnect();
    308:             }
    309:         }catch(SecurityException $e) {
    310:
    311:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    312:
    313:             self::$logger->warn($e->getMessage());
    314:         }catch(IllegalArguementException $e) {
    315:             self::$logger->error($e->getMessage());
    316:         }catch(BONotFoundException $e) {
    317:             self::$logger->warn($e->getMessage());
    318:
    319:             $this->setStatusMessage(AlphaView::displayErrorMessage('Failed to load the requested item from the database!'));
    320:         }
    321:
    322:         self::$logger->debug('<<doPOST');
    323:     }
    324: }
    325:
    326: // now build the new controller if this file is called directly
    327: if ('EditTags.php' == basename($_SERVER['PHP_SELF'])) {
    328:     $controller = new EditTags();
    329:
    330:     if(!empty($_POST)) {
    331:         $controller->doPOST($_REQUEST);
    332:     }else{
    333:         $controller->doGET($_GET);
    334:     }
    335: }
    336:
    337: ?>