Ejemplo n.º 1
0
 /**
  * Generic check for whether dependancies exist for this object in the db schema
  *
  * can be overloaded/supplemented by the child class
  *
  * @access public
  * @param string $msg Error message returned
  * @param int Optional key index
  * @param array Optional array to compiles standard joins: format [label=>'Label',name=>'table name',idfield=>'field',joinfield=>'field']
  * @return true|false
  */
 function canDelete($oid = null, $joins = null)
 {
     $k = $this->_tbl_key;
     if ($oid) {
         $this->{$k} = intval($oid);
     }
     if (is_array($joins)) {
         $select = "{$k}";
         $join = "";
         foreach ($joins as $table) {
             $select .= ', COUNT(DISTINCT ' . $table['idfield'] . ') AS ' . $table['idfield'];
             $join .= ' LEFT JOIN ' . $table['name'] . ' ON ' . $table['joinfield'] . ' = ' . $k;
         }
         $query = 'SELECT ' . $select . ' FROM ' . $this->_tbl . $join . ' WHERE ' . $k . ' = ' . $this->_db->Quote($this->{$k}) . ' GROUP BY ' . $k;
         $this->_db->setQuery($query);
         if (!($obj = $this->_db->loadObject())) {
             $this->setError($this->_db->getErrorMsg());
             return false;
         }
         $msg = array();
         $i = 0;
         foreach ($joins as $table) {
             $k = $table['idfield'] . $i;
             if ($obj->{$k}) {
                 $msg[] = JText::_($table['label']);
             }
             $i++;
         }
         if (count($msg)) {
             $this->setError("noDeleteRecord" . ": " . implode(', ', $msg));
             return false;
         } else {
             return true;
         }
     }
     return true;
 }